FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/myAtoi.java at master · wpande/leetcode · GitHub
wpande
leetcode
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
myAtoi.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
52 lines (52 loc) · 1.27 KB
Breadcrumbs
leetcode
/
myAtoi.java
Copy path
File metadata and controls
52 lines (52 loc) · 1.27 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public
class
Solution
{
public
int
myAtoi
(
String
str
) {
if
(
str
==
null
||
str
.
length
()==
0
)
return
0
;
boolean
sign
=
true
;
int
idx
=
0
;
for
(
int
i
=
0
;
i
<
str
.
length
();
i
++){
char
c
=
str
.
charAt
(
i
);
if
(
c
>=
'0'
&&
c
<=
'9'
){
idx
=
i
;
break
;
}
if
(
c
==
'-'
){
sign
=
false
;
idx
=
i
+
1
;
break
;
}
if
(
c
==
'+'
){
idx
=
i
+
1
;
break
;
}
if
(
c
==
' '
){
idx
=
i
+
1
;
}
else
return
0
;
}
if
(!(
str
.
charAt
(
idx
)>=
'0'
&&
str
.
charAt
(
idx
)<=
'9'
))
return
0
;
long
res
=
0
;
for
(
int
i
=
idx
;
i
<
str
.
length
();
i
++){
char
c
=
str
.
charAt
(
i
);
if
(
c
>=
'0'
&&
c
<=
'9'
){
res
=
res
*
10
+(
c
-
'0'
);
if
(
res
>
Integer
.
MAX_VALUE
)
break
;
}
else
break
;
}
int
tres
=
0
;
if
(
res
>
Integer
.
MAX_VALUE
||
res
<
0
){
if
(
sign
)
return
Integer
.
MAX_VALUE
;
else
return
Integer
.
MIN_VALUE
;
}
else
tres
=(
int
)
res
;
return
sign
?
tres
:-
tres
;
}
}
Back
|
FazBrowse Home
|
New Git URL