We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 39b3342 + bd7b643 commit 3183266Copy full SHA for 3183266
sqrt.java
@@ -0,0 +1,23 @@
1
+package com.company;
2
+//leetcode:https://leetcode.com/problems/sqrtx/
3
+public class Sqrt {
4
+ public static void main(String[] args) {
5
+ int ans = mySqrt(81);
6
+ System.out.println(ans);
7
+ }
8
+ static int mySqrt(int x) {
9
+ if (x == 0) return 0;
10
+ int start = 1, end = x;
11
+ while (start <= end) {
12
+ int mid = start + (end - start) / 2;
13
+ if (mid <= x / mid && (mid + 1) > x / (mid + 1))// Found the result
14
+ return mid;
15
+ else if (mid > x / mid)// Keep checking the left part
16
+ end = mid;
17
+ else {
18
+ start = mid + 1;// Keep checking the right part
19
20
21
+ return start;
22
23
+}
0 commit comments