Skip to content

Commit ca4b5a3

Browse files
refactor 440
1 parent c2cb413 commit ca4b5a3

File tree

1 file changed

+26
-23
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+26
-23
lines changed

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

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,35 @@
2020
*/
2121
public class _440 {
2222

23-
/**credit: https://discuss.leetcode.com/topic/64624/concise-easy-to-understand-java-5ms-solution-with-explaination/2*/
24-
public int findKthNumber(int n, int k) {
25-
int curr = 1;
26-
k = k - 1;
27-
while (k > 0) {
28-
int steps = calSteps(n, curr, curr + 1);
29-
if (steps <= k) {
30-
curr += 1;
31-
k -= steps;
32-
} else {
33-
curr *= 10;
34-
k -= 1;
23+
public static class Solution1 {
24+
/**
25+
* credit: https://discuss.leetcode.com/topic/64624/concise-easy-to-understand-java-5ms-solution-with-explaination/2
26+
*/
27+
public int findKthNumber(int n, int k) {
28+
int curr = 1;
29+
k = k - 1;
30+
while (k > 0) {
31+
int steps = calSteps(n, curr, curr + 1);
32+
if (steps <= k) {
33+
curr += 1;
34+
k -= steps;
35+
} else {
36+
curr *= 10;
37+
k -= 1;
38+
}
3539
}
40+
return curr;
3641
}
37-
return curr;
38-
}
3942

40-
//use long in case of overflow
41-
public int calSteps(int n, long n1, long n2) {
42-
int steps = 0;
43-
while (n1 <= n) {
44-
steps += Math.min(n + 1, n2) - n1;
45-
n1 *= 10;
46-
n2 *= 10;
43+
//use long in case of overflow
44+
public int calSteps(int n, long n1, long n2) {
45+
int steps = 0;
46+
while (n1 <= n) {
47+
steps += Math.min(n + 1, n2) - n1;
48+
n1 *= 10;
49+
n2 *= 10;
50+
}
51+
return steps;
4752
}
48-
return steps;
4953
}
50-
5154
}

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