[LeetCode]String to Integer (atoi)
来源:互联网 发布:网络销售合同 编辑:程序博客网 时间:2024/06/08 17:29
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.
spoilers alert... click to show 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 class Solution { public int atoi(String str) { int sign=0; if(str.length()==0) return 0; char c[]=str.toCharArray(); int count=0; while(c[count]==' '||c[count]!='+'&&c[count]!='-'&&c[count]>'9'&&c[count]<'0'){ if(count>=str.length()-1) break; else {count++;} } if(c[count]=='+'){sign=1;count++;} else if(c[count]=='-'){sign=-1;count++;} else{ ; } double result=0; while(c[count]<='9'&&c[count]>='0'){ if(sign==0) sign=1; result=result*10+Integer.parseInt(String.valueOf(c[count])); if(count>=str.length()-1) break; else count++; } if(sign==-1){ result=result*-1; } else if(sign==0) return 0; if(result>=2147483647) result=2147483647; else if(result<=-2147483648) result=-2147483648; int result2=(int)result; return result2; }}
- LeetCode:String to Integer (atoi)
- LeetCode: String to Integer (atoi)
- LeetCode String to Integer(atoi)
- LeetCode :String to Integer (atoi)
- Leetcode: String to Integer (atoi)
- Leetcode : String to Integer (atoi)
- 【leetcode】String to Integer (atoi)
- [LeetCode]String to Integer (atoi)
- LeetCode-String to Integer (atoi)
- [leetcode] String to Integer (atoi)
- LeetCode - String to Integer (atoi)
- 【leetcode】String to Integer (atoi)
- LeetCode - String to Integer (atoi)
- LeetCode: String to Integer (atoi)
- Leetcode: String to Integer (atoi)
- [LeetCode] String to Integer (atoi)
- leetcode String to Integer (atoi)
- [LeetCode]String to Integer (atoi)
- hdu1043(八数码问题,广搜 + hash(实现状态压缩) )
- Shell学习(1)——第一个shell脚本
- @class和#import的区别
- MyEclipse8.5 起动不了报 An error has occurred.See the log file
- Flex中通过RadioButton进行切换
- [LeetCode]String to Integer (atoi)
- SHELL : echo字体控制
- is not in the sudoers file 解决(转)
- window7 wifi配置
- @property与内存管理以及可传参数
- 通过傅里叶变换方法求图像卷积-OpenCV实现
- 程序员面试100题之五:二叉树两个结点的最低共同父结点
- PHP控制Header信息以强制转换文件类型表
- MyBatis的动态SQL详解