Skip to content

Commit 1d110ea

Browse files
committed
Add #69 Sqrt(x) Java Solution
1 parent 7288899 commit 1d110ea

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

README-zh.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@
433433
| 73 | [Set Matrix Zeroes](https://leetcode-cn.com/problems/set-matrix-zeroes/) | [Python3](./algorithm/python/73_set_matrix_zeroes.py), [Java](./algorithm/java/73_set_matrix_zeroes.java) | 中等 | 数组 |
434434
| 71 | [Simplify Path](https://leetcode-cn.com/problems/simplify-path/) | [Python3](./algorithm/python/71_simplify_path.py) | 中等 | 栈, 字符串 |
435435
| 70 | [Climbing Stairs](https://leetcode-cn.com/problems/climbing-stairs/) | [Python3](./algorithm/python/70_climbing_stairs.py), [Java](./algorithm/java/70_climbing_stairs.java) | 简单 | 动态规划 |
436-
| 69 | [Sqrt(x)](https://leetcode-cn.com/problems/sqrtx/) | [Python3](./algorithm/python/69_Sqrt(x).py) | 简单 | 数学, 二分查找 |
436+
| 69 | [Sqrt(x)](https://leetcode-cn.com/problems/sqrtx/) | [Python3](./algorithm/python/69_Sqrt(x).py), [Java](./algorithm/java/69_Sqrt(x).java) | 简单 | 数学, 二分查找 |
437437
| 67 | [Add Binary](https://leetcode-cn.com/problems/add-binary/) | [Python3](./algorithm/python/67_add_binary.py) | 简单 | 数学, 字符串 |
438438
| 66 | [Plus One](https://leetcode-cn.com/problems/plus-one/) | [Python3](./algorithm/python/66_plus_one.py), [Java](./algorithm/java/66_plus_one.java) | 简单 | 数组 |
439439
| 64 | [Minimum Path Sum](https://leetcode-cn.com/problems/minimum-path-sum/) | [Python3](./algorithm/python/64_minimum_path_sum.py) | 中等 | 数组, 动态规划 |

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ If you have any questions or advices, please discuss them in [Issues](https://gi
432432
| 73 | [Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) | [Python3](./algorithm/python/73_set_matrix_zeroes.py), [Java](./algorithm/java/73_set_matrix_zeroes.java) | Medium | Array |
433433
| 71 | [Simplify Path](https://leetcode.com/problems/simplify-path/) | [Python3](./algorithm/python/71_simplify_path.py) | Medium | Stack, String |
434434
| 70 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [Python3](./algorithm/python/70_climbing_stairs.py), [Java](./algorithm/java/70_climbing_stairs.java) | Easy | Dynamic Programming |
435-
| 69 | [Sqrt(x)](https://leetcode.com/problems/sqrtx/) | [Python3](./algorithm/python/69_Sqrt(x).py) | Easy | Math, Binary Search |
435+
| 69 | [Sqrt(x)](https://leetcode.com/problems/sqrtx/) | [Python3](./algorithm/python/69_Sqrt(x).py), [Java](./algorithm/java/69_Sqrt(x).java) | Easy | Math, Binary Search |
436436
| 67 | [Add Binary](https://leetcode.com/problems/add-binary/) | [Python3](./algorithm/python/67_add_binary.py) | Easy | Math, String |
437437
| 66 | [Plus One](https://leetcode.com/problems/plus-one/) | [Python3](./algorithm/python/66_plus_one.py), [Java](./algorithm/java/66_plus_one.java) | Easy | Array |
438438
| 64 | [Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/) | [Python3](./algorithm/python/64_minimum_path_sum.py) | Medium | Array, Dynamic Programming |

algorithm/java/69_Sqrt(x).java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public int mySqrt(int x) {
3+
if (x == 0 || x == 1) {
4+
return x;
5+
}
6+
int left = 1;
7+
int right = x;
8+
int ans = -1;
9+
int mid;
10+
while (left < right) {
11+
mid = left + (right - left) / 2;
12+
13+
if (mid+1 <= x / (mid+1)) {
14+
left = mid;
15+
} else if (mid > x / mid) {
16+
right = mid;
17+
} else {
18+
ans = mid;
19+
break;
20+
}
21+
}
22+
return ans;
23+
}
24+
}
25+
26+
/**
27+
* This is my personal record of solving Leetcode Problems.
28+
* If you have any questions, please discuss them in [Issues](https://github.com/mengxinayan/leetcode/issues).
29+
* Copyright (C) 2020-2021 mengxinayan
30+
*
31+
* This program is free software: you can redistribute it and/or modify
32+
* it under the terms of the GNU General Public License as published by
33+
* the Free Software Foundation, either version 3 of the License, or
34+
* (at your option) any later version.
35+
*
36+
* This program is distributed in the hope that it will be useful,
37+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
38+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39+
* GNU General Public License for more details.
40+
*
41+
* You should have received a copy of the GNU General Public License
42+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
43+
*/

0 commit comments

Comments
 (0)