- Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha0400_nth_digit.rs
46 lines (36 loc) · 985 Bytes
/
a0400_nth_digit.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* [0400] nth-digit
*/
pubstructSolution{}
// solution impl starts here
implSolution{
pubfnfind_nth_digit(n:i32) -> i32{
letmut n:u32 = n asu32;
letmut x:u32 = 0;
letmut right:u32 = 0;
// 确定数字区间
while n > right {
x += 1;
n -= right;
right = 10u32.pow(x - 1)* x *9;
}
// 取出数字
((10u32.pow(x - 1) + (n - 1) / x) / 10u32.pow(if n % x > 0{ x - n % x }else{0}) % 10)
asi32
}
}
// solution impl ends here
// solution tests starts here
#[cfg(test)]
mod tests {
usesuper::*;
#[test]
fntest_case0(){
assert_eq!(Solution::find_nth_digit(3),3);
assert_eq!(Solution::find_nth_digit(10),1);
assert_eq!(Solution::find_nth_digit(11),0);
assert_eq!(Solution::find_nth_digit(12),1);
assert_eq!(Solution::find_nth_digit(1000),3);
}
}
// solution tests ends here