Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions code@xukaixuan/basic_theory/binary_search.js
Original file line number Diff line number Diff line change
@@ -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]<target)return mid
high = mid -1
}
}
return -1
}

// 查找最后一个相等的数

const binaryFindLast = (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 ===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]<target) return mid
high = mid -1
}else {
low = mid +1
}
}
return -1
}

// 查找最后一个小于等于给定值得元素
const binaryFindLastSmall = (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){
high = mid -1
}else {
if (mid ===sortedArr.length-1 ||sortedArr[mid+1]>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}`)
Original file line number Diff line number Diff line change
@@ -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




27 changes: 27 additions & 0 deletions code@xukaixuan/demo 33 binary_search/readme.md
Original file line number Diff line number Diff line change
@@ -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
```
Original file line number Diff line number Diff line change
@@ -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]






26 changes: 26 additions & 0 deletions code@xukaixuan/demo 34 binary_search/readme.md
Original file line number Diff line number Diff line change
@@ -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
```