File tree Expand file tree Collapse file tree 2 files changed +37
-39
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 2 files changed +37
-39
lines changed Original file line number Diff line number Diff line change 25
25
*/
26
26
public class _400 {
27
27
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 ));
44
50
}
45
-
46
- start += (n - 1 ) / len ;
47
- String s = Integer .toString (start );
48
- return Character .getNumericValue (s .charAt ((n - 1 ) % len ));
49
51
}
50
-
51
52
}
Original file line number Diff line number Diff line change 6
6
7
7
import static junit .framework .Assert .assertEquals ;
8
8
9
- /**
10
- * Created by fishercoder on 4/26/17.
11
- */
12
9
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 ;
17
14
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
+ }
22
19
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
+ }
30
27
}
You can’t perform that action at this time.
0 commit comments