1
2
3
4
5
6
7
8
9
10
11

left, right =0, len(array) - 1
while left <= right:
mid = left + (right - left)/2
if array[mid] == target:
//find the target
break or return result
elif array[mid] < target:
left = mid + 1
else:
right = mid - 1

problem

Implement int sqrt(int x).

approach1

单调递增可以用二分法

1
2


  • time:O()
  • space:O()

approach2

1
2


  • time:O()
  • space:O()

approach3

1
2


  • time:O()
  • space:O()

summery


Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×