File tree 1 file changed +21
-0
lines changed
1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -495,6 +495,27 @@ func RightBound(nums []int, target int) (index int) {
495
495
* 此方法在迭代終止時,left 和 right 指針指向相鄰位置。
496
496
* 此方法會排除最後一個可能的候選解,當要求嚴格小於或大於某個值時可能更合適。
497
497
* 在一些情況下,可能會更高效,因為在每次循環中只需要比較一次 left 和 right,而不需要再處理當兩者相等時的情況。
498
+ ```go
499
+ func search(nums []int , target int ) int {
500
+ left, right := 0 , len (nums)- 1
501
+
502
+ for left < right {
503
+ mid := int (uint(right+ left)>> 1 )
504
+ if nums[mid]== target{
505
+ return mid
506
+ }else if (nums[mid]< target){
507
+ left = mid+ 1
508
+ }else {
509
+ right = mid- 1
510
+ }
511
+ }
512
+ if left == right && nums[left] == target {
513
+ return left
514
+ }
515
+ return - 1
516
+ }
517
+ ```
518
+
498
519
499
520
| No. | Title | Solution | Difficulty | Time | Space | Topic |
500
521
| -------------------------------------------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------- - :| :---------------------------------------------------------------------------------------------------------------------------------- :| ------------ | -------------------------------------- - | ------------------------------ - | -------------- - |
You can’t perform that action at this time.
0 commit comments