题解 | 数字序列中某一位的数字
难不是很难,就是很容易错,一定要多做调试!
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return int整型 */ public int findNthDigit (int n) { // write code here int nc = n; int k = 1; int bit = 0; int ibit = 1; if (nc < 10) { return n; } nc = nc - 9; bit = 9; while (nc > 0) { ibit = ibit * 10; k = k + 1; bit = k * ibit * 9; nc = nc - bit; } nc = nc + bit-1; int a = nc % k; int b = nc / k; int r = 0; r = ibit + b; // 将数字转换为字符串 String numberStr = Integer.toString(r); char digitChar = numberStr.charAt(a); return Character.getNumericValue(digitChar); } }