其他分享
首页 > 其他分享> > LeetCode每日一题(Nth Digit)

LeetCode每日一题(Nth Digit)

作者:互联网

Given an integer n, return the nth digit of the infinite integer sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …].

Example 1:

Input: n = 3
Output: 3

Example 2:

Input: n = 11
Output: 0

Explanation: The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, … is a 0, which is part of the number 10.

Constraints:


注意每次生成新的范围时, curr=next+1, 如果不+1 那就会导致范围有交叠


impl Solution {
    pub fn find_nth_digit(n: i32) -> i32 {
        if n < 10 {
            return n;
        }
        let mut i = 0;
        let mut curr = 0_i64;
        let mut next = 9_i64;
        while next < n as i64 {
            i += 1;
            curr = next + 1;
            next += 9 * 10_i64.pow(i as u32) * (i + 1);
        }
        let start = 10_i64.pow(i as u32);
        let offset = (n as i64 - curr) / (i + 1);
        let index = (n as i64 - curr) % (i + 1);
        (start + offset)
            .to_string()
            .chars()
            .nth(index as usize)
            .unwrap()
            .to_string()
            .parse::<i32>()
            .unwrap()
    }
}

标签:10,Digit,curr,mut,next,i64,Nth,let,LeetCode
来源: https://blog.csdn.net/wangjun861205/article/details/121258857