8000 refactor 400 · SurajKamble/Leetcode@d7c0728 · GitHub
[go: up one dir, main page]

Skip to content

Commit d7c0728

Browse files
refactor 400
1 parent ef093a9 commit d7c0728

File tree

2 files changed

+37
-39
lines changed

2 files changed

+37
-39
lines changed

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

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,28 @@
2525
*/
2626
public class _400 {
2727

28-
/**credit: https://discuss.leetcode.com/topic/59314/java-solution:
29-
*
30-
* 1. find the length of the number where the nth digit is from
31-
* 2. find the actual number where the nth digit is from
32-
* 3. find the nth digit and return
33-
* */
34-
public int findNthDigit(int n) {
35-
int len = 1;
36-
long count = 9;
37-
int start = 1;
38-
39-
while (n > len * count) {
40-
n -= len * count;
41-
len += 1;
42-
count *= 10;
43-
start *= 10;
28+
public static class Solution1 {
29+
/**
30+
* credit: https://discuss.leetcode.com/topic/59314/java-solution:
31+
*
32+
* 1. find the length of the number where the nth digit is from 2. find the actual number where
33+
* the nth digit is from 3. find the nth digit and return
34+
*/
35+
public int findNthDigit(int n) {
36+
int len = 1;
37+
long count = 9;
38+
int start = 1;
39+
40+
while (n > len * count) {
41+
n -= len * count;
42+
len += 1;
43+
count *= 10;
44+
start *= 10;
45+
}
46+
47+
start += (n - 1) / len;
48+
String s = Integer.toString(start);
49+
return Character.getNumericValue(s.charAt((n - 1) % len));
4450
}
45-
46-
start += (n - 1) / len;
47-
String s = Integer.toString(start);
48-
return Character.getNumericValue(s.charAt((n - 1) % len));
4951
}
50-
5152
}

src/test/java/com/fishercoder/_400Test.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,22 @@
66

77
import static junit.framework.Assert.assertEquals;
88

9-
/**
10-
* Created by fishercoder on 4/26/17.
11-
*/
129
public class _400Test {
13-
private static _400 test;
14-
private static int expected;
15-
private static int actual;
16-
private static int n;
10+
private static _400.Solution1 solution1;
11+
private static int expected;
12+
private static int actual;
13+
private static int n;
1714

18-
@BeforeClass
19-
public static void setup() {
20-
test = new _400();
21-
}
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _400.Solution1();
18+
}
2219

23-
@Test
24-
public void test1() {
25-
n = 11;
26-
expected = 0;
27-
actual = test.findNthDigit(n);
28-
assertEquals(expected, actual);
29-
}
20+
@Test
21+
public void test1() {
22+
n = 11;
23+
expected = 0;
24+
actual = solution1.findNthDigit(n);
25+
assertEquals(expected, actual);
26+
}
3027
}

0 commit comments

Comments
 (0)
0