diff --git a/code@xukaixuan/basic_theory/binary_search.js b/code@xukaixuan/basic_theory/binary_search.js new file mode 100644 index 0000000..90f3b94 --- /dev/null +++ b/code@xukaixuan/basic_theory/binary_search.js @@ -0,0 +1,88 @@ +/** + * 二分查找 + */ + + // 查找等于第一个给定的值 +const binaryFindFirst = (sortedArr,target) =>{ + if (sortedArr.length === 0) return -1 + let low = 0 + let high = sortedArr.length-1 + while(low <=high) { + const mid = Math.floor((low+high)/2) + + if (target < sortedArr[mid]){ + high = mid -1 + }else if (target> sortedArr[mid]){ + low = mid +1 + }else{ + if (mid ===0||sortedArr[mid-1]{ + if (sortedArr.length ===0) return -1 + let low =0 + let high = sortedArr.length-1 + while(low <=high){ + const mid = Math.floor((low+high)/2) + if (targetsortedArr[mid]){ + low = mid +1 + }else { + if (mid ===sortedArr.length -1||sortedArr[mid+1]>target) return mid + low = mid + 1 + } + } + return -1 +} + +// 查找第一个大于等于给定元素的值 +const binaryFindFiRstBig = (sortedArr,target) =>{ + if (sortedArr.length === 0) return -1 + let low = 0 + let high = sortedArr.length-1 + while(low< high){ + const mid = Math.floor((low+high)/2) + if (sortedArr[mid] >= target){ + if (mid ===0||sortedArr[mid-1]{ + if (sortedArr.length === 0) return -1 + let low = 0 + let high = sortedArr.length-1 + while(low <= high){ + const mid = Math.floor((low+high)/2) + if (sortedArr[mid]target) return mid + low = mid +1 + } + } +return -1 +} + +const arr = [1, 2, 3, 4, 4, 4, 4, 4, 6, 7, 8, 8, 9] +const first = biaryFindFirst(arr, 4) +console.log(`FindFirst: ${first}`) + +const last = biaryFindLast(arr, 4) +console.log(`FindLast: ${last}`) +const FisrtBig = biaryFindFistBig(arr, 5) +console.log(`FindFisrtBig: ${FisrtBig}`) +const LastSmall = biaryFindLastSmall(arr, 4) +console.log(`FindLastSmall: ${LastSmall}`) \ No newline at end of file diff --git a/code@xukaixuan/demo 33 binary_search/33.search-in-rotated-sorted-array.py b/code@xukaixuan/demo 33 binary_search/33.search-in-rotated-sorted-array.py new file mode 100644 index 0000000..f1c6f42 --- /dev/null +++ b/code@xukaixuan/demo 33 binary_search/33.search-in-rotated-sorted-array.py @@ -0,0 +1,34 @@ +# +# @lc app=leetcode id=33 lang=python3 +# +# [33] Search in Rotated Sorted Array +# +class Solution: + def search(self, nums: List[int], target: int) -> int: + def search(self, nums, target): + if not nums: + return -1 + + low, high = 0, len(nums) - 1 + + while low <= high: + mid = (low + high) / 2 + if target == nums[mid]: + return mid + + if nums[low] <= nums[mid]: + if nums[low] <= target <= nums[mid]: + high = mid - 1 + else: + low = mid + 1 + else: + if nums[mid] <= target <= nums[high]: + low = mid + 1 + else: + high = mid - 1 + + return -1 + + + + diff --git a/code@xukaixuan/demo 33 binary_search/readme.md b/code@xukaixuan/demo 33 binary_search/readme.md new file mode 100644 index 0000000..0e3e3f5 --- /dev/null +++ b/code@xukaixuan/demo 33 binary_search/readme.md @@ -0,0 +1,27 @@ +## Search in Rotated Sorted Array + +Tags + +array | binary-search + +## description +Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. + +(i.e.,``` [0,1,2,4,5,6,7]``` might become ```[4,5,6,7,0,1,2]```). + +You are given a target value to search. If found in the array return its index, otherwise return -1. + +You may assume no duplicate exists in the array. + +Your algorithm's runtime complexity must be in the order of ```O(log n)```. + +``` +Example 1: + +Input: nums = [4,5,6,7,0,1,2], target = 0 +Output: 4 +Example 2: + +Input: nums = [4,5,6,7,0,1,2], target = 3 +Output: -1 +``` \ No newline at end of file diff --git a/code@xukaixuan/demo 34 binary_search/34.find-first-and-last-position-of-element-in-sorted-array.py b/code@xukaixuan/demo 34 binary_search/34.find-first-and-last-position-of-element-in-sorted-array.py new file mode 100644 index 0000000..9043625 --- /dev/null +++ b/code@xukaixuan/demo 34 binary_search/34.find-first-and-last-position-of-element-in-sorted-array.py @@ -0,0 +1,65 @@ +# +# @lc app=leetcode id=34 lang=python +# +# [34] Find First and Last Position of Element in Sorted Array +# +class Solution(object): + def searchRange(self, nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: List[int] + """ + lo,hi = 0,len(nums)-1 + first,last = -1,-1 + while(lo <=hi): + mid = lo +( hi -lo)//2 + if target< nums[mid]: + hi = mid -1 + elif target > nums[mid]: + lo = mid + 1 + else : + if (mid == 0 or nums[mid-1] < target): + first = mid + hi = mid-1 + lo,hi = 0,len(nums)-1 + while(lo <= hi): + mid = lo +( hi -lo)//2 + if target< nums[mid]: + hi = mid -1 + elif target > nums[mid]: + lo = mid + 1 + else : + if (mid == len(nums)-1 or nums[mid+1] > target): + last = mid + lo = mid+1 + return [first,last] + + +## most voted + +def searchRange(self, nums, target): + def binarySearchLeft(A, x): + left, right = 0, len(A) - 1 + while left <= right: + mid = (left + right) / 2 + if x > A[mid]: left = mid + 1 + else: right = mid - 1 + return left + + def binarySearchRight(A, x): + left, right = 0, len(A) - 1 + while left <= right: + mid = (left + right) / 2 + if x >= A[mid]: left = mid + 1 + else: right = mid - 1 + return right + + left, right = binarySearchLeft(nums, target), binarySearchRight(nums, target) + return (left, right) if left <= right else [-1, -1] + + + + + + diff --git a/code@xukaixuan/demo 34 binary_search/readme.md b/code@xukaixuan/demo 34 binary_search/readme.md new file mode 100644 index 0000000..a575ace --- /dev/null +++ b/code@xukaixuan/demo 34 binary_search/readme.md @@ -0,0 +1,26 @@ +# Search Insert Position + + +## description +Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. + +You may assume no duplicates in the array. +``` +Example 1: + +Input: [1,3,5,6], 5 +Output: 2 +Example 2: + +Input: [1,3,5,6], 2 +Output: 1 +Example 3: + +Input: [1,3,5,6], 7 +Output: 4 +Example 4: + +Input: [1,3,5,6], 0 +Output: 0 +``` +