diff --git a/notes/剑指 offer 题解.md b/notes/剑指 offer 题解.md index 3fe9200a..22ad72b3 100644 --- a/notes/剑指 offer 题解.md +++ b/notes/剑指 offer 题解.md @@ -1410,17 +1410,15 @@ public int NumberOf1Between1AndN_Solution(int n) { ```java int digitAtIndex(int index) { - if (index >= 0) { - int digit = 1; - while (true) { - int amount = getAmountOfDigit(digit); - int totalAmount = amount * digit; - if (index < totalAmount) return digitAtIndex(index, digit); - index -= totalAmount; - digit++; - } + if (index < 0) return -1; + int digit = 1; + while (true) { + int amount = getAmountOfDigit(digit); + int totalAmount = amount * digit; + if (index < totalAmount) return digitAtIndex(index, digit); + index -= totalAmount; + digit++; } - return -1; } private int getAmountOfDigit(int digit) { @@ -1438,6 +1436,11 @@ private int beginNumber(int digits) { if (digits == 1) return 0; return (int) Math.pow(10, digits - 1); } + +public static void main(String[] args) { + Solution solution = new Solution(); + System.out.println(solution.digitAtIndex(1001)); +} ``` ## 45. 把数组排成最小的数