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):
1569
1569
``` python
1570
1570
1571
1571
# coding:utf-8
1572
- def binary_search (list ,item ):
1572
+ def binary_search (list , item ):
1573
1573
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溢出
1577
1577
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
1582
1582
else :
1583
1583
return mid
1584
1584
return None
1585
1585
mylist = [1 ,3 ,5 ,7 ,9 ]
1586
- print binary_search(mylist,3 )
1586
+ print binary_search(mylist, 3 )
1587
1587
1588
1588
```
1589
1589
You can’t perform that action at this time.
0 commit comments