File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change 31
31
| 58| [ 最后一个单词的长度] ( https://leetcode.cn/problems/length-of-last-word/ ) | [ JavaScript] ( ./algorithms/length-of-last-word.js ) | Easy|
32
32
| 53| [ 最大子数组和] ( https://leetcode.cn/problems/maximum-subarray/ ) | [ JavaScript] ( ./algorithms/maximum-subarray.js ) | Easy|
33
33
| 66| [ 加一] ( https://leetcode-cn.com/problems/plus-one/ ) | [ JavaScript] ( ./algorithms/plus-one.js ) | Easy|
34
+ | 69| [ x 的平方根 ] ( https://leetcode.cn/problems/sqrtx/ ) | [ JavaScript] ( ./algorithms/sqrtx.js ) | Easy|
34
35
| 70| [ 爬楼梯] ( https://leetcode.cn/problems/climbing-stairs/ ) | [ JavaScript] ( ./algorithms/climbing-stairs.js ) | Easy|
35
36
| 73| [ 矩阵置零] ( https://leetcode.cn/problems/set-matrix-zeroes/ ) | [ JavaScript] ( ./algorithms/set-matrix-zeroes.js ) | Medium|
36
37
| 82| [ 删除排序链表中的重复元素 II] ( https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii/ ) | [ JavaScript] ( ./algorithms/remove-duplicates-from-sorted-list-ii.js ) | Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number } x
3
+ * @return {number }
4
+ */
5
+ var mySqrt = function ( x ) {
6
+ if ( x <= 1 ) return x ;
7
+
8
+ let left = 1 ;
9
+ let right = x ;
10
+
11
+ // 1,2,3,4,5,6,7,8,9,10
12
+
13
+ while ( left <= right ) {
14
+ let middle = ( left + right ) >> 1 ;
15
+ if ( middle > x / middle ) {
16
+ right = middle - 1 ;
17
+ } else if ( middle < x / middle ) {
18
+ left = middle + 1 ;
19
+ } else {
20
+ return middle ;
21
+ }
22
+ }
23
+
24
+ return right ;
25
+ } ;
You can’t perform that action at this time.
0 commit comments