LeetCode 8 String to Integer (atoi) (C,C++,Java,Python)
来源:互联网 发布:房地产软件 编辑:程序博客网 时间:2024/05/14 21:49
转自http://blog.csdn.net/runningtortoises/article/details/45557659
Problem:
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.
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.
Solution:
To deal with overflow, inspect the current number before multiplication. If the current number is greater than 214748364, we know it is going to overflow. On the other hand, if the current number is equal to 214748364, we know that it will overflow only when the current digit is greater than or equal to 8.
题目大意:
实现C语言中的atoi方法(字符串转为整数)解题思路:题目简单,重要的是注意一些边界条件,如判定越界,是否含有非法字符,前空格去掉等
Java源代码(用时314ms):
public class Solution { private final static int INT_MAX=2147483647; private final static int INT_MIN=-2147483648; public int myAtoi(String str) { char[] chs = str.toCharArray(); int index=0; while(index<str.length() && chs[index]==' ')index++; int flag=1; if(index<str.length() && chs[index]=='-'){ flag=-1; index++; }else if(index<str.length() && chs[index]=='+'){ index++; } int res=0; while(index<str.length()){ if(chs[index]<'0' || chs[index]>'9'){ return flag*res; } int digit=chs[index]-'0'; if(flag==1 && res*10.0+digit > INT_MAX){ return INT_MAX; }else if(flag==-1 &&-(res*10.0+digit)<INT_MIN){ return INT_MIN; } res = res*10+digit; index++; } return flag*res; } }
C语言源代码(用时5ms):
int myAtoi(char* str) { int flag=1,res=0,dig; while(*str==' ')str++; if(*str=='-'){ flag=-1; str++; }else if(*str=='+'){ str++; } while(*str){ if(*str<'0' || *str>'9'){ return flag*res; } dig=*str-'0'; if(flag==1 && res*10.0+dig>INT_MAX){ return INT_MAX; }else if(flag==-1 && -res*10.0-dig<INT_MIN){ return INT_MIN; } res= res*10+dig; str++; } return flag*res; }
C++源代码(用时17ms):
class Solution { public: int myAtoi(string str) { int index=0; while(str[index]==' ')index++; int flag=1; if(str[index]=='-'){ index++; flag=-1; }else if(str[index]=='+'){ index++; } int res=0; while(index<str.size()){ if(str[index]<'0' || str[index]>'9'){ return flag*res; } int digit=str[index]-'0'; if(flag==1 && res*10.0+digit>INT_MAX){ return INT_MAX; }else if(flag==-1 && -(res*10.0+digit)<INT_MIN){ return INT_MIN; } res = res*10+digit; index++; } return flag*res; } };
Python源代码(用时80ms):
class Solution: # @param {string} str # @return {integer} def myAtoi(self, str): INT_MAX=2147483647 INT_MIN=-2147483648 index=0 while index<len(str) and str[index]==' ':index+=1 flag=1 if index<len(str) and str[index]=='-': index+=1 flag=-1 elif index<len(str) and str[index]=='+': index+=1 res=0 while index<len(str): if str[index]<'0' or str[index]>'9':return flag*res digit=ord(str[index])-ord('0') if flag==1 and res*10+digit>INT_MAX:return INT_MAX if flag==-1 and res*10+digit>-INT_MIN:return INT_MIN res=res*10+digit index+=1 return flag*res
- LeetCode 8 String to Integer (atoi) (C,C++,Java,Python)
- LeetCode 8 String to Integer (atoi) (C,C++,Java,Python)
- [C++]LeetCode: 24 String to Integer (atoi)
- 【leetcode c++】08 String to Integer (atoi)
- Leetcode c语言-String to Integer (atoi)
- LeetCode (8) String to Integer (atoi) C语言程序
- [LeetCode Java] 8 String to Integer (atoi)
- [leetcode-8]String to Integer (atoi)(java)
- [C++] String to Integer (atoi)
- LeetCode 之 String to Integer (atoi) — C 实现
- 【LeetCode算法练习(C++)】String to Integer (atoi)
- [LeetCode] 008. String to Integer (Easy) (C++/Java/Python)
- 【LeetCode with Python】 String to Integer (atoi)
- 8. String to Integer (atoi) Leetcode Python
- 【LeetCode】【Python】8. String to Integer (atoi)
- [LeetCode] String to Integer (atoi) Python
- 【Leetcode】【python】String to Integer (atoi)
- LeetCode 8: String to Integer (atoi) (python)
- java图形验证码生成工具类及web页面校验验证码
- 【Android】Android的接口Interface的应用
- 为什么静态成员函数不能是const
- Linux的rc.local自启动服务
- 分解质因数
- LeetCode 8 String to Integer (atoi) (C,C++,Java,Python)
- ORACLE中如何查看表空间
- 趴一趴如何用最简单的方式从html form表单中获取到数据
- 【Deep Learning】Face Detection and Alignment
- AndFix解析——(中)
- Linux 文件打开数(fd)总结
- 判断一个字符串中的字符是否唯一
- java的三种注释
- DEVOPS的支撑服务:K8S容器管理与应用部署