diff --git a/lessons/binarysearch.java b/lessons/binarysearch.java new file mode 100644 index 00000000..e9d44dee --- /dev/null +++ b/lessons/binarysearch.java @@ -0,0 +1,27 @@ +// CODE: + +class Solution { + public int search(int[] nums, int target) { + +int start = 0; + int end = nums.length - 1; + while(start <= end) { + int mid = start + (end - start)/2; + if(nums[mid] == target) { + return mid; + } + else if(target < nums[mid]) { + end = mid - 1; + } + else { + start = mid + 1; + } + } + return -1; + } +} + + +// Input: nums = [-1,0,3,5,9,12], target = 9 +// Output: 4 +// Explanation: 9 exists in nums and its index is 4 diff --git a/lessons/binarysearch.md b/lessons/binarysearch.md deleted file mode 100644 index 081486ed..00000000 --- a/lessons/binarysearch.md +++ /dev/null @@ -1,7 +0,0 @@ ---- -path: "/binarysearch" -title: "Binary Search" -order: "5B" -section: "Searching & Sorting" -description: "learn Searching algorithms" ----