8. String to Integer (atoi)
2016-07-15 11:31
274 查看
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
function signature accepts a
button to reset your code definition.
spoilers alert... click to show requirements for atoi.
Requirements for atoi:
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed
by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
自己的好丑好混乱
public static int myAtoi(String str)
{
int len=str.length();
if(len==0)
return 0;
char[] carr=str.toCharArray();
int templen=0;
char[] temp=new char[len];
int start=0;
while(carr[start]==' ')
start++;
for(int i=start;i<len;i++)
temp[templen++]=carr[i];
carr=temp;
len=templen;
boolean isnegative=carr[0]=='-'?true:false;
if(carr[0]=='+'||carr[0]=='-')
{
char[] t=new char[len-1];
System.arraycopy(carr, 1, t, 0, len-1);
carr=t;
len--;
}
int illegalposition=-1;
for(int k=0;k<len;k++)
if(carr[k]<'0'||carr[k]>'9')
{
illegalposition=k;
break;
}
if(illegalposition==0)
return 0;
if(illegalposition>0)
len=illegalposition;
if(isnegative&&len>10)
return -2147483648;
if(len>10)
return 2147483647;
if(len==10)
{
char[] pomaxrange={'2','1','4','7','4','8','3','6','4','7'};
char[] nemaxrange={'2','1','4','7','4','8','3','6','4','8'};
boolean inrange=true;
if(isnegative)
{
int base=1000000000;
int sum=0;
for(int i=0;i<len;i++)
{
sum+=(carr[i]-nemaxrange[i])*base;
base/=10;
}
if(carr[0]-nemaxrange[0]>1||sum>0)
inrange=false;
}
else {
int base=1000000000;
int sum=0;
for(int i=0;i<len;i++)
{
sum+=(carr[i]-pomaxrange[i])*base;
base/=10;
}
if(carr[0]-pomaxrange[0]>1||sum>0)
inrange=false;
}
if(!inrange)
if(isnegative)
return -2147483648;
else {
return 2147483647;
}
}
int base=1;
for(int i=0;i<len-1;i++)
base*=10;
int result=0;
for(int i=0;i<len;i++)
{
result+=(carr[i]-'0')*base;
base/=10;
}
result=isnegative?-result:result;
return result;
}
--------------------------------------------------------------------------------------------
还是借鉴别人简洁明了的吧。摘自https://discuss.leetcode.com/topic/2666/my-simple-solution/7
we only need to handle four cases:
discards all leading whitespaces
sign of the number
overflow
invalid input
public static int myAtoi(String str) {
if (str.isEmpty()) return 0;
int sign = 1, base = 0, i = 0;
while (str.charAt(i) == ' ')
i++;
if (str.charAt(i) == '-' || str.charAt(i) == '+')
sign = str.charAt(i++) == '-' ? -1 : 1;
while (i < str.length() && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
if (base > Integer.MAX_VALUE / 10 || (base == Integer.MAX_VALUE / 10 && str.charAt(i) - '0' > 7)) {
return (sign == 1) ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
base = 10 * base + (str.charAt(i++) - '0');
}
return base * sign;
}
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.
spoilers alert... click to show requirements for atoi.
Requirements for atoi:
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed
by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
自己的好丑好混乱
public static int myAtoi(String str)
{
int len=str.length();
if(len==0)
return 0;
char[] carr=str.toCharArray();
int templen=0;
char[] temp=new char[len];
int start=0;
while(carr[start]==' ')
start++;
for(int i=start;i<len;i++)
temp[templen++]=carr[i];
carr=temp;
len=templen;
boolean isnegative=carr[0]=='-'?true:false;
if(carr[0]=='+'||carr[0]=='-')
{
char[] t=new char[len-1];
System.arraycopy(carr, 1, t, 0, len-1);
carr=t;
len--;
}
int illegalposition=-1;
for(int k=0;k<len;k++)
if(carr[k]<'0'||carr[k]>'9')
{
illegalposition=k;
break;
}
if(illegalposition==0)
return 0;
if(illegalposition>0)
len=illegalposition;
if(isnegative&&len>10)
return -2147483648;
if(len>10)
return 2147483647;
if(len==10)
{
char[] pomaxrange={'2','1','4','7','4','8','3','6','4','7'};
char[] nemaxrange={'2','1','4','7','4','8','3','6','4','8'};
boolean inrange=true;
if(isnegative)
{
int base=1000000000;
int sum=0;
for(int i=0;i<len;i++)
{
sum+=(carr[i]-nemaxrange[i])*base;
base/=10;
}
if(carr[0]-nemaxrange[0]>1||sum>0)
inrange=false;
}
else {
int base=1000000000;
int sum=0;
for(int i=0;i<len;i++)
{
sum+=(carr[i]-pomaxrange[i])*base;
base/=10;
}
if(carr[0]-pomaxrange[0]>1||sum>0)
inrange=false;
}
if(!inrange)
if(isnegative)
return -2147483648;
else {
return 2147483647;
}
}
int base=1;
for(int i=0;i<len-1;i++)
base*=10;
int result=0;
for(int i=0;i<len;i++)
{
result+=(carr[i]-'0')*base;
base/=10;
}
result=isnegative?-result:result;
return result;
}
--------------------------------------------------------------------------------------------
还是借鉴别人简洁明了的吧。摘自https://discuss.leetcode.com/topic/2666/my-simple-solution/7
we only need to handle four cases:
discards all leading whitespaces
sign of the number
overflow
invalid input
public static int myAtoi(String str) {
if (str.isEmpty()) return 0;
int sign = 1, base = 0, i = 0;
while (str.charAt(i) == ' ')
i++;
if (str.charAt(i) == '-' || str.charAt(i) == '+')
sign = str.charAt(i++) == '-' ? -1 : 1;
while (i < str.length() && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
if (base > Integer.MAX_VALUE / 10 || (base == Integer.MAX_VALUE / 10 && str.charAt(i) - '0' > 7)) {
return (sign == 1) ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
base = 10 * base + (str.charAt(i++) - '0');
}
return base * sign;
}
相关文章推荐
- 5. ESXi集群配置一例(HP C7000+DELL EQlogic+HUAWEI 6700)
- 更好地学习dubbo源代码
- Bootrap学习笔记——不定期更新
- XZ_iOS之使用webView实现开机动画效果
- httpd.conf文件配置详解
- Canvas 在高清屏下绘制图片变模糊的解决方法
- git merge和git rebase的区别
- OpenGL学习笔记(一)
- linux系统编程手册 I/O复用
- nginx的配置、虚拟主机、负载均衡和反向代理
- 【BZOJ1968】[Ahoi2005]COMMON 约数研究【数论】
- 1j2ee即web项目,严格意义上来说,没有jsp代码,只有class文件、html文件、javascript文件。2 基于extjs框架的项目怎么怎么查看在浏览器中展示的最终html页面的代码 3
- Elasticsearch与Solr
- H5 学习资源网站
- Comparable<T> 和 Comparator<T>
- iOS内存管理retain,assign,copy,strong,weak
- nvidia gpu fan speed control
- (HttpURLConnection)强制转化
- iOS- define宏定义和const常变量区别
- url中含有中文字符