题解 | #把字符串转换成整数#
把字符串转换成整数
https://www.nowcoder.com/practice/1277c681251b4372bdef344468e4f26e
public class Solution {
public int StrToInt(String str) {
int ret = 0;
int flag = 1;
String num = "0123456789";
if(str.isEmpty()) {
return 0;
}
char ch = str.charAt(0);
if (ch == '+') {
flag = 1;
}else if(ch == '-') {
flag *= -1;
}else {
if(num.contains(ch+"")) {
ret = str.charAt(0)-'0';
}
}
for(int i = 1; i < str.length(); i++) {
ch = str.charAt(i);
if(!num.contains(ch+"")) {
return 0;
}
ret = ret*10 + ch-'0';
}
return flag*ret;
}
}



SHEIN公司福利 837人发布