FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/problems/src/math/NthDigit.java at master · KindleBooks66/leetcode · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
KindleBooks66
/
leetcode
Public
forked from
gouthampradhan/leetcode
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
problems
/
src
/
math
/
NthDigit.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
27 lines (25 loc) · 914 Bytes
Breadcrumbs
leetcode
/
problems
/
src
/
math
/
NthDigit.java
Copy path
File metadata and controls
27 lines (25 loc) · 914 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
package
math
;
/** Created by gouthamvidyapradhan on 05/11/2019 */
public
class
NthDigit
{
public
static
void
main
(
String
[]
args
) {
System
.
out
.
println
(
new
NthDigit
().
findNthDigit
(
1000000000
));
}
public
int
findNthDigit
(
int
n
) {
if
(
n
>=
1
&&
n
<=
9
)
return
n
;
long
sum
=
0L
;
for
(
int
i
=
0
; ;
i
++) {
long
pow
= (
9
* (
new
Double
(
Math
.
pow
(
10
,
i
)).
longValue
())) * (
i
+
1
);
sum
+=
pow
;
if
(
sum
>=
n
) {
long
diff
= (
long
)
n
- (
sum
-
pow
);
long
num
=
diff
/ (
i
+
1
);
long
mod
=
diff
% (
i
+
1
);
long
result
=
new
Double
(
Math
.
pow
(
10
,
i
)).
intValue
() + (
num
-
1
) + (
mod
>
0
?
1
:
0
);
String
resultStr
=
String
.
valueOf
(
result
);
return
(
mod
==
0
)
?
Integer
.
parseInt
(
String
.
valueOf
(
resultStr
.
charAt
(
resultStr
.
length
() -
1
)))
:
Integer
.
parseInt
(
String
.
valueOf
(
resultStr
.
charAt
((
int
)
mod
-
1
)));
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL