How to convert a string to an integer in LeetCode?
- 内容介绍
- 文章标签
- 相关推荐
本文共计185个文字,预计阅读时间需要1分钟。
java/** * 8. String to Integer (atoi) * @param str * @return * Implement atoi to convert a string to an integer. * * Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask. */
/**
* 8. String to Integer (atoi)
* @param str
* @return
* Implement atoi to convert a string to an integer.
* Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
* Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
* There are some problems
*/
public static int atoi(String str) {
str = str.trim();
if(str.length()==0){
return 0;
}
int base = 1;
if(str.startsWith("-")){
base = -1;
str = str.substring(1);
}
int dev = 1;
for(int i=1;i
本文共计185个文字,预计阅读时间需要1分钟。
java/** * 8. String to Integer (atoi) * @param str * @return * Implement atoi to convert a string to an integer. * * Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask. */
/**
* 8. String to Integer (atoi)
* @param str
* @return
* Implement atoi to convert a string to an integer.
* Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
* Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
* There are some problems
*/
public static int atoi(String str) {
str = str.trim();
if(str.length()==0){
return 0;
}
int base = 1;
if(str.startsWith("-")){
base = -1;
str = str.substring(1);
}
int dev = 1;
for(int i=1;i

