leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0440.cpp (605B)


0 class Solution { 1 static int count(int n, long p1, long p2) { 2 int steps = 0; 3 while (p1 <= n) { 4 5 steps += min((long)(n + 1), p2) - p1; 6 p1 *= 10, p2 *= 10; 7 } 8 9 return steps; 10 } 11 12 public: 13 int findKthNumber(int n, int k) const { 14 int crnt = 1; 15 k--; 16 17 while (k > 0) { 18 int step = count(n, crnt, crnt + 1); 19 if (step <= k) { 20 crnt++; 21 k -= step; 22 } else { 23 crnt *= 10; 24 k--; 25 } 26 } 27 28 return crnt; 29 } 30 };