File tree Expand file tree Collapse file tree 2 files changed +10
-9
lines changed
Expand file tree Collapse file tree 2 files changed +10
-9
lines changed Original file line number Diff line number Diff line change 1+ .idea
Original file line number Diff line number Diff line change @@ -1569,21 +1569,21 @@ def node(l1, l2):
15691569``` python
15701570
15711571# coding:utf-8
1572- def binary_search (list ,item ):
1572+ def binary_search (list , item ):
15731573 low = 0
1574- high = len (list )- 1
1575- while low<= high:
1576- mid = (low+ high) / 2
1574+ high = len (list ) - 1
1575+ while low <= high:
1576+ mid = (high - low) / 2 + low # 避免( high + low) / 2溢出
15771577 guess = list[mid]
1578- if guess> item:
1579- high = mid- 1
1580- elif guess< item:
1581- low = mid+ 1
1578+ if guess > item:
1579+ high = mid - 1
1580+ elif guess < item:
1581+ low = mid + 1
15821582 else :
15831583 return mid
15841584 return None
15851585mylist = [1 ,3 ,5 ,7 ,9 ]
1586- print binary_search(mylist,3 )
1586+ print binary_search(mylist, 3 )
15871587
15881588```
15891589
You can’t perform that action at this time.
0 commit comments