nth-digit

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

public class Solution {
    public int findNthDigit(int n) {
        int k = 9;
        int b = 1;
        // 需要加上对b的限制
        while (b < 9 && n > k*b) {
            n -= k * b;
            k *= 10;
            b ++;
        }
        int rank = (n - 1) / b;
        int app = (n - 1) % b;
        int num = (int)Math.pow(10, b-1) + rank;
        for (int i=b-1; i>app; i--) {
            num /= 10;
        }
        return num % 10;
    }
}
时间: 2024-12-30 01:48:20

nth-digit的相关文章

(hdu step 4.1.5)find the nth digit(求S串中的第n个位置上是什么数字)

题目: find the nth digit Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 689 Accepted Submission(s): 244   Problem Description 假设:S1 = 1S2 = 12S3 = 123S4 = 1234.........S9 = 123456789S10 = 123456789

leetcode 400 Add to List 400. Nth Digit

Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note:n is positive and will fit within the range of a 32-bit signed integer (n < 231). Example 1: Input: 3 Output: 3 Example 2: Input: 11 Output: 0 Explanation

400. Nth Digit 第N位

Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note:n is positive and will fit within the range of a 32-bit signed integer (n < 231). Example 1: Input:3Output:3 Example 2: Input:11Output:0Explanation:The 11

400. Nth Digit(LeetCode)

Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note:n is positive and will fit within the range of a 32-bit signed integer (n < 231). Example 1: Input: 3 Output: 3 Example 2: Input: 11 Output: 0 Explanation

400. Nth Digit

Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n is positive and will fit within the range of a 32-bit signed integer (n < 231). Example 1: Input: Output: Example 2: Input: Output: Explanation: The 11

Leetcode: Nth Digit

Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n is positive and will fit within the range of a 32-bit signed integer (n < 231). Example 1: Input: 3 Output: 3 Example 2: Input: 11 Output: 0 Explanatio

[LeetCode] Nth Digit 第N位

Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n is positive and will fit within the range of a 32-bit signed integer (n < 231). Example 1: Input: 3 Output: 3 Example 2: Input: 11 Output: 0 Explanatio

hdu 1597 find the nth digit

find the nth digit Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8356    Accepted Submission(s): 2383 Problem Description 假设: S1 = 1 S2 = 12 S3 = 123 S4 = 1234 ......... S9 = 123456789 S10 =

find the nth digit(二分查找)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1597 find the nth digit Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9517    Accepted Submission(s): 2757 Problem Description 假设:S1 = 1S2 = 12S3

find the nth digit

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9518    Accepted Submission(s): 2758 Problem Description 假设: S1 = 1 S2 = 12 S3 = 123 S4 = 1234 ......... S9 = 123456789 S10 = 1234567891 S11 = 12