convert string to integer
Topic description
Converting a string to an integer requires library functions that cannot use strings to convert integers. Returns 0 if the value is 0 or the string is not a valid value
- Enter description:
- Enter a string, including alphanumeric characters, can be empty
- Return value description:
- Returns the number if it is a valid numeric representation, otherwise returns 0
title link : converts a string to an integer
code
/**
* 标题:把字符串转换成整数
* 题目描述
* 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0
* 输入描述:
* 输入一个字符串,包括数字字母符号,可以为空
* 返回值描述:
* 如果是合法的数值表达则返回该数字,否则返回0
* 题目链接:
* https://www.nowcoder.com/practice/1277c681251b4372bdef344468e4f26e?tpId=13&&tqId=11202&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
*/
public class Jz49 {
public int strToInt(String str) {
if (str == null || str.length() == 0) {
return 0;
}
boolean isNegative = str.charAt(0) == '-';
int result = 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;
}
result = result * 10 + (c - '0');
}
return isNegative ? -result : result;
}
public static void main(String[] args) {
Jz49 jz49 = new Jz49();
System.out.println(jz49.strToInt("+32293023a"));
System.out.println(jz49.strToInt("+2392032"));
System.out.println(jz49.strToInt("2293043a"));
System.out.println(jz49.strToInt("-fd3323"));
System.out.println(jz49.strToInt("-23232942"));
System.out.println(jz49.strToInt("292930203"));
}
}
[Daily Message] Good luck starts in the morning. May you have a smile in the morning and happiness in your smile.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。