467. [leetcode] Unique Substrings in Wraparound String

问题描述

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.

Update (2015-02-10):

The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.

原题链接:https://leetcode.com/problems/string-to-integer-atoi/

Analysis

public int myAtoi(String str) {
if(str == null || str.length() == 0){
return 0;
}
char[] chs = str.trim().toCharArray();
int isPositive = 1, index = 0;
if(chs[0] == '-'){
isPositive = -1;
index = 1;
}
if(chs[0] == '+'){
isPositive = 1;
index = 1;
}
long res = 0;
for(; index < chs.length; index++){
if(chs[index] < '0' || chs[index] > '9' ){
return (int)res;
}
res = res * 10 + isPositive * (chs[index] - '0');
if(res > Integer.MAX_VALUE){
return Integer.MAX_VALUE;
}
if(res < Integer.MIN_VALUE){
return Integer.MIN_VALUE;
}
}
return (int)res;
}
上一篇:常见的数据库连接池和对应连接池参数值


下一篇:C#关于HttpClient的应用(一):获取IP所在的地理位置信息