Leetcode 400 Solution

This article provides solution to leetcode question 400 (nth-digit)

https://leetcode.com/problems/nth-digit

Solution

class Solution { public: int findNthDigit(int n) { n--; int64_t base = 1; int64_t curr_cnt = 9; int digit = 1;
while (n > curr_cnt * digit) { n -= curr_cnt * digit; base *= 10; curr_cnt *= 10; digit++; }
int target_num = base + n / digit; string target_num_str = to_string(target_num); return target_num_str[n % digit] - '0'; } };