From 4121ac5deb0c89787ffd2cb1dd5cf54f0d14b0a4 Mon Sep 17 00:00:00 2001 From: Kaixuan Xu Date: Sat, 4 May 2019 16:22:50 +0800 Subject: [PATCH 1/2] add binary search for special scenery --- code@xukaixuan/basic_theory/binary_search.js | 88 ++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 code@xukaixuan/basic_theory/binary_search.js diff --git a/code@xukaixuan/basic_theory/binary_search.js b/code@xukaixuan/basic_theory/binary_search.js new file mode 100644 index 0000000..a46d846 --- /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]{ + 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 From 6fcec96b571a40f1971e3ba43c48febedc8f73ef Mon Sep 17 00:00:00 2001 From: Kaixuan Xu Date: Mon, 6 May 2019 22:25:07 +0800 Subject: [PATCH 2/2] add demo for binary_search and refine the basic threoy for bianary_search --- code@xukaixuan/basic_theory/binary_search.js | 4 +- .../33.search-in-rotated-sorted-array.py | 34 ++++++++++ .../demo 33 binary_search/readme.md | 27 ++++++++ ...ast-position-of-element-in-sorted-array.py | 65 +++++++++++++++++++ .../demo 34 binary_search/readme.md | 26 ++++++++ 5 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 code@xukaixuan/demo 33 binary_search/33.search-in-rotated-sorted-array.py create mode 100644 code@xukaixuan/demo 33 binary_search/readme.md create mode 100644 code@xukaixuan/demo 34 binary_search/34.find-first-and-last-position-of-element-in-sorted-array.py create mode 100644 code@xukaixuan/demo 34 binary_search/readme.md diff --git a/code@xukaixuan/basic_theory/binary_search.js b/code@xukaixuan/basic_theory/binary_search.js index a46d846..90f3b94 100644 --- a/code@xukaixuan/basic_theory/binary_search.js +++ b/code@xukaixuan/basic_theory/binary_search.js @@ -49,7 +49,7 @@ const binaryFindFiRstBig = (sortedArr,target) =>{ 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){ + while(low <= high){ const mid = Math.floor((low+high)/2) if (sortedArr[mid] 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 +``` +