Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit158486d

Browse files
committed
Revert "👌 IMPROVE: use bit operations faster"
This reverts commitab5c884.
1 parent80a0851 commit158486d

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

‎basic_algorithm/binary_search.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func search(nums []int, target int) int {
2727
end:=len(nums) -1
2828
// 2、处理for循环
2929
for start+1 < end {
30-
mid:= start + ((end-start)>>1)
30+
mid:= start + (end-start)/2
3131
// 3、比较a[mid]和target值
3232
if nums[mid] == target {
3333
end = mid
@@ -64,7 +64,7 @@ func search(nums []int, target int) int {
6464
start:=0
6565
end:=len(nums) -1
6666
for start <= end {
67-
mid:= start + ((end-start)>>1)
67+
mid:= start + (end-start)/2
6868
if nums[mid] == target {
6969
return mid
7070
}elseif nums[mid] < target {
@@ -98,7 +98,7 @@ func searchRange (A []int, target int) []int {
9898
start:=0
9999
end:=len(A) -1
100100
for start+1 < end {
101-
mid:= start + ((end-start)>>1)
101+
mid:= start + (end-start)/2
102102
if A[mid] > target {
103103
end = mid
104104
}elseif A[mid] < target {
@@ -121,7 +121,7 @@ func searchRange (A []int, target int) []int {
121121
start =0
122122
end =len(A) -1
123123
for start+1 < end {
124-
mid:= start + ((end-start)>>1)
124+
mid:= start + (end-start)/2
125125
if A[mid] > target {
126126
end = mid
127127
}elseif A[mid] < target {
@@ -155,7 +155,7 @@ func searchInsert(nums []int, target int) int {
155155
start:=0
156156
end:=len(nums) -1
157157
for start+1 < end {
158-
mid:= start + ((end-start)>>1)
158+
mid:= start + (end-start)/2
159159
if nums[mid] == target {
160160
// 标记开始位置
161161
start = mid
@@ -194,7 +194,7 @@ func searchMatrix(matrix [][]int, target int) bool {
194194
start:=0
195195
end:= row*col -1
196196
for start+1 < end {
197-
mid:= start + ((end-start)>>1)
197+
mid:= start + (end-start)/2
198198
// 获取2纬数组对应值
199199
val:= matrix[mid/col][mid%col]
200200
if val > target {
@@ -252,7 +252,7 @@ func findMin(nums []int) int {
252252
end:=len(nums) -1
253253

254254
for start+1 < end {
255-
mid:= start + ((end-start)>>1)
255+
mid:= start + (end-start)/2
256256
// 最后一个元素值为target
257257
if nums[mid] <= nums[end] {
258258
end = mid
@@ -289,7 +289,7 @@ func findMin(nums []int) int {
289289
for start < end && nums[start] == nums[start+1] {
290290
start++
291291
}
292-
mid:= start + ((end-start)>>1)
292+
mid:= start + (end-start)/2
293293
// 中间元素和最后一个元素比较(判断中间点落在左边上升区,还是右边上升区)
294294
if nums[mid] <= nums[end] {
295295
end = mid
@@ -320,7 +320,7 @@ func search(nums []int, target int) int {
320320
start:=0
321321
end:=len(nums) -1
322322
for start+1 < end {
323-
mid:= start + ((end-start)>>1)
323+
mid:= start + (end-start)/2
324324
// 相等直接返回
325325
if nums[mid] == target {
326326
return mid
@@ -375,7 +375,7 @@ func search(nums []int, target int) bool {
375375
for start < end && nums[end] == nums[end-1] {
376376
end--
377377
}
378-
mid:= start + ((end-start)>>1)
378+
mid:= start + (end-start)/2
379379
// 相等直接返回
380380
if nums[mid] == target {
381381
returntrue

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp