Skip to content

Commit ef6ffbd

Browse files
refactor 69
1 parent fe62411 commit ef6ffbd

File tree

2 files changed

+33
-39
lines changed

2 files changed

+33
-39
lines changed

src/main/java/com/fishercoder/solutions/_69.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,32 @@
22

33
/**
44
* 69. Sqrt(x)
5+
*
56
* Implement int sqrt(int x).
67
* Compute and return the square root of x.
78
*/
89

910
public class _69 {
10-
public int mySqrt(int x) {
11-
long left = 0;
12-
long right = x / 2 + 1;
13-
while (left <= right) {
14-
long mid = left + (right - left) / 2;
15-
long result = mid * mid;
16-
if (result == (long) x) {
17-
return (int) mid;
18-
} else if (result > x) {
19-
right = mid - 1;
20-
} else {
21-
left = mid + 1;
11+
public static class Solution1 {
12+
/**A few key points:
13+
* 1. all variable use long type, otherwise overflow, just cast to int before returning
14+
* 2. left start from 0, not 1
15+
* 3. right start from x/2 + 1, not from x*/
16+
public int mySqrt(int x) {
17+
long left = 0;
18+
long right = x / 2 + 1;
19+
while (left <= right) {
20+
long mid = left + (right - left) / 2;
21+
long result = mid * mid;
22+
if (result == (long) x) {
23+
return (int) mid;
24+
} else if (result > x) {
25+
right = mid - 1;
26+
} else {
27+
left = mid + 1;
28+
}
2229
}
30+
return (int) right;
2331
}
24-
return (int) right;
2532
}
2633
}
Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,26 @@
11
package com.fishercoder;
22

33
import com.fishercoder.solutions._69;
4-
import org.junit.Before;
54
import org.junit.BeforeClass;
65
import org.junit.Test;
76

87
import static junit.framework.Assert.assertEquals;
98

10-
/**
11-
* Created by fishercoder on 1/25/17.
12-
*/
139
public class _69Test {
14-
private static _69 test;
15-
private static int expected;
16-
private static int actual;
17-
private static int input;
10+
private static _69.Solution1 solution1;
1811

19-
@BeforeClass
20-
public static void setup() {
21-
test = new _69();
22-
}
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _69.Solution1();
15+
}
2316

24-
@Before
25-
public void setupForEachTest() {
26-
expected = 0;
27-
actual = 0;
28-
input = 0;
29-
}
17+
@Test
18+
public void test1() {
19+
assertEquals(4, solution1.mySqrt(16));
20+
}
3021

31-
@Test
32-
public void test1() {
33-
expected = 4;
34-
input = 16;
35-
actual = test.mySqrt(input);
36-
assertEquals(expected, actual);
37-
38-
}
22+
@Test
23+
public void test2() {
24+
assertEquals(2, solution1.mySqrt(8));
25+
}
3926
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy