leetcode 8. String to Integer (atoi)
来源:互联网 发布:淘宝网折800女士皮草 编辑:程序博客网 时间:2024/05/20 08:25
//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. public class Solution {public static void main(String[] args) {String a = "0101231+123";int result = myAtoi(a);System.out.println(result);}public static int myAtoi(String str) {double result = 0;String s = "";int i = 0;boolean flag = false;if(str.length() == 0){//输入空串返回0return 0;}while(str.charAt(i) == ' '){//去除字符串前面的空格i++;}if(str.charAt(i) == '+'){//判断首字符是+或-i++;}else if(str.charAt(i) == '-'){i++;flag = true;}for(int j = i;j<str.length();j++){if(str.charAt(j)<='9'&&str.charAt(j)>='0'){//是数字则加入字符串s = s+str.charAt(j);}else{//如果是连续非数字则输入错误break;}}if(s.length() == 0){//连续非数字或只有一个非数字字符则返回0return 0;}result = Double.parseDouble(s);//将字符串转换为double型,防止溢出if(flag == true){result = -result;}return (int)result;//返回int } }
0 0
- [LeetCode]8. String to Integer (atoi)
- LeetCode 8. String to Integer (atoi)
- 8. String to Integer (atoi) Leetcode Python
- LeetCode --- 8. String to Integer (atoi)
- LeetCode 8.String to Integer (atoi)
- [Leetcode] 8. String to Integer (atoi)
- [leetcode] 8.String to Integer (atoi)
- [LeetCode] 8.String to Integer (atoi)
- <LeetCode OJ> 8. String to Integer (atoi)
- leetCode 8. String to Integer (atoi)
- 8. String to Integer (atoi) LeetCode
- leetcode 8. String to Integer (atoi)
- leetcode 8. String to Integer (atoi)
- LeetCode OJ 8.String to Integer (atoi)
- Leetcode ☞ 8. String to Integer (atoi)
- 8. String to Integer (atoi) ---Leetcode
- leetcode 8. String to Integer (atoi)
- Leetcode: 8. String to Integer (atoi)(JAVA)
- mybatis配置insert/update/delete同一个模板
- js 自定义日期(Date)格式化
- Android动画机制与使用技巧
- 关于某些进程使adb不能正常工作
- 2016春季练习——二分+贪心
- leetcode 8. String to Integer (atoi)
- poj 3186 Treats for the Cows(区间dp)
- hdu--5670--Machine (模拟三进制)
- POJ 3903 Stock Exchange【LIS 二分查找】
- 边工作边学习的现实选择
- 进程(应用程序)之一
- web项目下让tomcat效率提升!
- win7 下搭建前端开发环境 sublime-text3
- GridView添加HeaderView