Skip to content

Commit 4dfce58

Browse files
author
MouCoder
committed
字符串转换整数(提交成功代码)
1 parent 35aac9f commit 4dfce58

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

stringToInt/Solution.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
}

stringToInt/Test.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
}

stringToInt/stringToInt/Test.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
}

0 commit comments

Comments
 (0)