File tree Expand file tree Collapse file tree 4 files changed +98
-0
lines changed
Expand file tree Collapse file tree 4 files changed +98
-0
lines changed Original file line number Diff line number Diff line change 1+ package LeetCode .String .stringToInt ;
2+
3+ public class Solution {
4+ public int myAtoi (String str ) {
5+ double num = 0 ;
6+ for (int i = 0 ;i < str .length ();i ++) {
7+ //判断第一个非空格字符是否为数字或者'-'
8+ if (str .charAt (i ) != ' ' && ((str .charAt (i ) == '-' || str .charAt (i ) == '+' )|| (str .codePointAt (i ) >= 48 && str .codePointAt (i ) <= 57 ))) {
9+ //如果第一个非空格字符是'-'但是'-'后不是数字,直接返回0
10+ if (i <str .length ()-1 &&str .charAt (i ) == '-' && (str .codePointAt (i +1 ) < 48 || str .codePointAt (i +1 ) > 57 )) {
11+ num = 0 ;
12+ break ;
13+ }
14+ else if (str .charAt (i ) == '-' ) {
15+ for (int j = i + 1 ; j < str .length () && (str .codePointAt (j ) >= 48 && str .codePointAt (j ) <= 57 ); j ++) {
16+ num = (num * 10 + Integer .parseInt (str .substring (j ,j +1 )));
17+ }
18+ num = (-1 ) * num ;
19+ break ;
20+ }else if (i <str .length ()-1 &&str .charAt (i ) == '+' && (str .codePointAt (i +1 ) < 48 || str .codePointAt (i +1 ) > 57 )) {
21+ num = 0 ;
22+ break ;
23+ }else if (str .charAt (i ) == '+' ){
24+ for (int j = i +1 ;j < str .length () && (str .codePointAt (j ) >= 48 && str .codePointAt (j ) <= 57 ) ; j ++) {
25+ num = (num * 10 + Integer .parseInt (str .substring (j ,j +1 )));
26+ }
27+ break ;
28+ }else {
29+ for (int j = i ;j < str .length () && (str .codePointAt (j ) >= 48 && str .codePointAt (j ) <= 57 ) ; j ++) {
30+ num = (num * 10 + Integer .parseInt (str .substring (j ,j +1 )));
31+ }
32+ break ;
33+ }
34+ } else if (str .charAt (i ) != ' ' ){
35+ num = 0 ;
36+ break ;
37+ }
38+ }
39+ return (int )num ;
40+ }
41+ }
Original file line number Diff line number Diff line change 1+ package LeetCode .String .stringToInt ;
2+
3+ public class Test {
4+ public static void main (String [] args ) {
5+ Solution sol = new Solution ();
6+ System .out .println (sol .myAtoi ("42" ));
7+ }
8+ }
Original file line number Diff line number Diff line change 1+ package LeetCode .String .stringToInt ;
2+
3+ public class Solution {
4+ public int myAtoi (String str ) {
5+ double num = 0 ;
6+ for (int i = 0 ;i < str .length ();i ++) {
7+ //判断第一个非空格字符是否为数字或者'-'
8+ if (str .charAt (i ) != ' ' && ((str .charAt (i ) == '-' || str .charAt (i ) == '+' )|| (str .codePointAt (i ) >= 48 && str .codePointAt (i ) <= 57 ))) {
9+ //如果第一个非空格字符是'-'但是'-'后不是数字,直接返回0
10+ if (i <str .length ()-1 &&str .charAt (i ) == '-' && (str .codePointAt (i +1 ) < 48 || str .codePointAt (i +1 ) > 57 )) {
11+ num = 0 ;
12+ break ;
13+ }
14+ else if (str .charAt (i ) == '-' ) {
15+ for (int j = i + 1 ; j < str .length () && (str .codePointAt (j ) >= 48 && str .codePointAt (j ) <= 57 ); j ++) {
16+ num = (num * 10 + Integer .parseInt (str .substring (j ,j +1 )));
17+ }
18+ num = (-1 ) * num ;
19+ break ;
20+ }else if (i <str .length ()-1 &&str .charAt (i ) == '+' && (str .codePointAt (i +1 ) < 48 || str .codePointAt (i +1 ) > 57 )) {
21+ num = 0 ;
22+ break ;
23+ }else if (str .charAt (i ) == '+' ){
24+ for (int j = i +1 ;j < str .length () && (str .codePointAt (j ) >= 48 && str .codePointAt (j ) <= 57 ) ; j ++) {
25+ num = (num * 10 + Integer .parseInt (str .substring (j ,j +1 )));
26+ }
27+ break ;
28+ }else {
29+ for (int j = i ;j < str .length () && (str .codePointAt (j ) >= 48 && str .codePointAt (j ) <= 57 ) ; j ++) {
30+ num = (num * 10 + Integer .parseInt (str .substring (j ,j +1 )));
31+ }
32+ break ;
33+ }
34+ } else if (str .charAt (i ) != ' ' ){
35+ num = 0 ;
36+ break ;
37+ }
38+ }
39+ return (int )num ;
40+ }
41+ }
Original file line number Diff line number Diff line change 1+ package LeetCode .String .stringToInt ;
2+
3+ public class Test {
4+ public static void main (String [] args ) {
5+ Solution sol = new Solution ();
6+ System .out .println (sol .myAtoi ("42" ));
7+ }
8+ }
You can’t perform that action at this time.
0 commit comments