47 lines
1.1 KiB
Java
47 lines
1.1 KiB
Java
# 67. 把字符串转换成整数
|
||
|
||
## 题目链接
|
||
|
||
[NowCoder](https://www.nowcoder.com/practice/1277c681251b4372bdef344468e4f26e?tpId=13&tqId=11202&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
|
||
|
||
## 题目描述
|
||
|
||
将一个字符串转换成一个整数,字符串不是一个合法的数值则返回 0,要求不能使用字符串转换整数的库函数。
|
||
|
||
```html
|
||
Iuput:
|
||
+2147483647
|
||
1a33
|
||
|
||
Output:
|
||
2147483647
|
||
0
|
||
```
|
||
|
||
## 解题思路
|
||
|
||
```java
|
||
public int StrToInt(String str) {
|
||
if (str == null || str.length() == 0)
|
||
return 0;
|
||
boolean isNegative = str.charAt(0) == '-';
|
||
int ret = 0;
|
||
for (int i = 0; i < str.length(); i++) {
|
||
char c = str.charAt(i);
|
||
if (i == 0 && (c == '+' || c == '-')) /* 符号判定 */
|
||
continue;
|
||
if (c < '0' || c > '9') /* 非法输入 */
|
||
return 0;
|
||
ret = ret * 10 + (c - '0');
|
||
}
|
||
return isNegative ? -ret : ret;
|
||
}
|
||
```
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<div align="center"><img width="320px" src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/githubio/公众号二维码-2.png"></img></div>
|