FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/java/400_Nth_Digit.java at master · jakehoare/leetcode · GitHub
jakehoare
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
30
Star
51
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
java
/
400_Nth_Digit.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
27 lines (23 loc) · 748 Bytes
Breadcrumbs
leetcode
/
java
/
400_Nth_Digit.java
Copy path
File metadata and controls
27 lines (23 loc) · 748 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
https://leetcode.com/problems/nth-digit/
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...
Find the length of the number containing the nth digit by removing 9 digits of length 1, 90 of length 2, etc.
Find the integer containing n by dividing remainder by its length and adding the starting integer of that length.
Time - O(log n)
Space - O(1)
*/
public
class
Solution
{
public
int
findNthDigit
(
int
n
) {
n
--;
int
length
=
1
;
long
count
=
9
;
while
(
n
>=
length
*
count
) {
n
-=
length
*
count
;
length
+=
1
;
count
*=
10
;
}
int
intOfN
= (
n
/
length
) + (
int
)
Math
.
pow
(
10
,
length
-
1
);
String
s
=
Integer
.
toString
(
intOfN
);
return
(
int
) (
s
.
charAt
(
n
%
length
)) -
'0'
;
}
}
Back
|
FazBrowse Home
|
New Git URL