Skip to content

Commit fdcfb07

Browse files
solves binary search
1 parent 043e29b commit fdcfb07

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# LeetCode Algorithms
22

3-
![problems-solved](https://img.shields.io/badge/Problems%20Solved-171/2081-1f425f.svg)
4-
![problems-solved-java](https://img.shields.io/badge/Java-171/2081-1abc9c.svg)
5-
![problems-solved-python](https://img.shields.io/badge/Python-171/2081-1abc9c.svg)
3+
![problems-solved](https://img.shields.io/badge/Problems%20Solved-172/2081-1f425f.svg)
4+
![problems-solved-java](https://img.shields.io/badge/Java-172/2081-1abc9c.svg)
5+
![problems-solved-python](https://img.shields.io/badge/Python-172/2081-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
77
[![cp](https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg)](https://github.com/anishLearnsToCode/competitive-programming)
88

@@ -192,7 +192,7 @@
192192
| 697 | [Degree of an Array](https://leetcode.com/problems/degree-of-an-array) | [![Java](assets/java.png)](src/DegreeOfAnArray.java) [![Python](assets/python.png)](python/degree_of_an_array.py) |
193193
| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree) | [![Java](assets/java.png)](src/SearchInBinarySearchTree.java) [![Python](assets/python.png)](python/search_in_binary_search_tree.py) |
194194
| 703 | [Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream) | [![Java](assets/java.png)](src/KthLargestElementInAStream.java) [![Python](assets/python.png)](python/k_th_largest_element_in_a_stream.py) |
195-
| 704 | [Binary Search](https://leetcode.com/problems/binary-search) | |
195+
| 704 | [Binary Search](https://leetcode.com/problems/binary-search) | [![Java](assets/java.png)](src/BinarySearch.java) [![Python](assets/python.png)](python/binary_search.py) |
196196
| 705 | [Design HashSet](https://leetcode.com/problems/design-hashset) | |
197197
| 706 | [Design HashMap](https://leetcode.com/problems/design-hashmap) | |
198198
| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case) | |

python/binary_search.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def search(self, array: List[int], target: int) -> int:
6+
left, right = 0, len(array) - 1
7+
while left <= right:
8+
middle = left + (right - left) // 2
9+
if array[middle] == target: return middle
10+
elif array[middle] > target: right = middle - 1
11+
else: left = middle + 1
12+
return -1

src/BinarySearch.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class BinarySearch {
2+
public int search(int[] array, int val) {
3+
int left = 0, right = array.length - 1, middle;
4+
while (left <= right) {
5+
middle = left + (right - left) / 2;
6+
if (array[middle] == val) return middle;
7+
else if (array[middle] > val) right = middle - 1;
8+
else left = middle + 1;
9+
}
10+
11+
return -1;
12+
}
13+
}

0 commit comments

Comments
 (0)