[LeetCode]8. String to Integer (atoi)
来源:互联网 发布:淘宝合同范本 编辑:程序博客网 时间:2024/06/05 23:03
Easy
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.
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 myAtoi(String str) { long sum = 0; if(!str.equals("")){ char[] mm = str.toCharArray(); int[] ii = new int[mm.length+1]; boolean begin = false; int b = 0; for(int a=0;a<mm.length;a++){ if(mm[a]==' '&&!begin) continue; if(!begin&&(mm[a]=='+'||mm[a]=='-'||(mm[a]>='0'&&mm[a]<='9'))){ ii[0] = mm[a]=='-'?-1:1; if(a<mm.length-1&&(mm[a]=='-'||mm[a]=='+')) a++; begin = true; } if(mm[a]>='0'&&mm[a]<='9'){ ii[++b] = mm[a]-'0'; }else{ break; } } for(int x=0;b>0;b--){ sum += ii[b]*(Math.pow(10, x)); x++; } sum *= ii[0]; if(sum>2147483647) sum = Integer.MAX_VALUE; if(sum<-2147483648) sum = Integer.MIN_VALUE; } return (int)sum; }}
4ms:
if (str == null || str.length() < 1) return 0; // trim white spaces str = str.trim(); char flag = '+'; // check negative or positive int i = 0; if (str.charAt(0) == '-') { flag = '-'; i++; } else if (str.charAt(0) == '+') { i++; } // use double to store result double result = 0; // calculate value while (str.length() > i && str.charAt(i) >= '0' && str.charAt(i) <= '9') { result = result * 10 + (str.charAt(i) - '0'); i++; } if (flag == '-') result = -result; // handle max and min if (result > Integer.MAX_VALUE) return Integer.MAX_VALUE; if (result < Integer.MIN_VALUE) return Integer.MIN_VALUE; return (int) result;
- [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)
- npm
- 《JAVA与模式》之简单工厂模式
- Notepad++使用技巧——同时编辑(对比)两个文档
- 运行ant脚本进行打包无法创建路径(and the archive is probably corrupt but I could not delete it)解决办法
- 二层交换机、三层交换机和路由器的原理及区别
- [LeetCode]8. String to Integer (atoi)
- MAC头、IP头、TCP头、UDP头详解以及定义
- 修改图片颜色
- 沸点百度文库下载器v2.3.3.2绿色版
- Java华丽转身 —— J2EE规范(一) JNDI Java Naming and Directory Interface
- Webpack
- 推荐系统杂谈
- NSUserDefaults 简介,使用 NSUserDefaults 存储自定义对象
- Grunt