Skip to content

Commit 09b9a79

Browse files
authored
Update 440-k-th-smallest-in-lexicographical-order.js
1 parent a2fe75a commit 09b9a79

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

440-k-th-smallest-in-lexicographical-order.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,28 @@
33
* @param {number} k
44
* @return {number}
55
*/
6-
const findKthNumber = function(n, k) {
6+
const findKthNumber = function (n, k) {
77
let curr = 1
88
k = k - 1
99
while (k > 0) {
1010
let steps = calSteps(n, curr, curr + 1)
1111
if (steps <= k) {
12-
curr++
12+
curr += 1
1313
k -= steps
1414
} else {
1515
curr *= 10
16-
k--
16+
k -= 1
1717
}
1818
}
1919
return curr
20-
}
2120

22-
//use long in case of overflow
23-
function calSteps(n, n1, n2) {
24-
let steps = 0
25-
while (n1 <= n) {
26-
steps += Math.min(n + 1, n2) - n1
27-
n1 *= 10
28-
n2 *= 10
21+
function calSteps(n, n1, n2) {
22+
let steps = 0
23+
while (n1 <= n) {
24+
steps += Math.min(n + 1, n2) - n1
25+
n1 *= 10
26+
n2 *= 10
27+
}
28+
return steps
2929
}
30-
return steps
3130
}

0 commit comments

Comments
 (0)