-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
34. Find First and Last Position of Element in Sorted Array/findBoth/findBoth.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package findboth | ||
|
||
func bisecLeft(nums []int, target int) int { | ||
left, right := 0, len(nums) | ||
var mid int | ||
for left < right { | ||
mid = (left + right) / 2 | ||
if nums[mid] < target { | ||
left = mid + 1 | ||
} else { | ||
right = mid | ||
} | ||
} | ||
return left | ||
} | ||
|
||
func bisecRight(nums []int, target int) int { | ||
left, right := 0, len(nums) | ||
var mid int | ||
for left < right { | ||
mid = (left + right) / 2 | ||
if nums[mid] <= target { | ||
left = mid + 1 | ||
} else { | ||
right = mid | ||
} | ||
} | ||
return left | ||
} | ||
|
||
func searchRange(nums []int, target int) []int { | ||
|
||
left := bisecLeft(nums, target) | ||
if left == len(nums) || | ||
nums[left] != target { | ||
return []int{-1, -1} | ||
} | ||
right := bisecRight(nums[left+1:], target) | ||
return []int{left, left + right} | ||
} |
11 changes: 11 additions & 0 deletions
11
34. Find First and Last Position of Element in Sorted Array/main.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from bisect import bisect, bisect_left | ||
|
||
|
||
class Solution: | ||
def searchRange(self, nums: List[int], target: int) -> List[int]: | ||
left = bisect_left(nums, target) | ||
if left != len(nums) and nums[left] == target: | ||
right = bisect(nums, target, left) | ||
return left, right - 1 | ||
else: | ||
return -1, -1 |
31 changes: 31 additions & 0 deletions
31
34. Find First and Last Position of Element in Sorted Array/onlyFindLeft/findLeftSoln.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package findleft | ||
|
||
/* merely 1 function. merely find left | ||
cannot judge in besecLeft() whether nums[left]==target | ||
because we search target+1 to get the right position | ||
*/ | ||
|
||
func bisecLeft(nums []int, target int) int { | ||
// 这个函数相当于 python中的bisect.bisect_left | ||
left, right := 0, len(nums) | ||
var mid int | ||
for left < right { | ||
mid = (left + right) / 2 | ||
if nums[mid] < target { | ||
left = mid + 1 | ||
} else { | ||
right = mid | ||
} | ||
} | ||
return left | ||
} | ||
|
||
func searchRange(nums []int, target int) []int { | ||
left := bisecLeft(nums, target) | ||
if left == len(nums) || | ||
nums[left] != target { | ||
return []int{-1, -1} | ||
} | ||
right := bisecLeft(nums[left+1:], target+1) | ||
return []int{left, left + right} | ||
} |