Easy 8题 String to Integer (atoi)
来源:互联网 发布:手机淘宝秒杀在哪里 编辑:程序博客网 时间:2024/06/05 20:20
Question:
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.
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:
public class Solution { public int myAtoi(String str) { if(str==null||str.length()==0) return 0; String strr=str.trim(); int start=0; int flag=0; if(strr.charAt(0)=='-') { if(strr.length()>=2) { start++; flag=1; } else return 0; } else if(strr.charAt(0)=='+') { if(strr.length()>=2) { start++; } else return 0; } else if(strr.charAt(0)>'9'&&strr.charAt(0)<'0') return 0; long res=0; int cor=0; for(int i=start;i<=strr.length()-1;i++) { if(strr.charAt(i)<='9'&&strr.charAt(i)>='0'){ res=res*10+(long)strr.charAt(i)-48; cor=1; } if(cor==1&&(strr.charAt(i)>'9'||strr.charAt(i)<'0')) break; if(cor!=1&&(strr.charAt(i)>'9'||strr.charAt(i)<'0')) return 0; if(res>=Integer.MAX_VALUE) break; } if(flag==1) res=-res; if(res>=Integer.MAX_VALUE) return Integer.MAX_VALUE; if(res<=Integer.MIN_VALUE) return Integer.MIN_VALUE; return (int)res; }}
- Easy 8题 String to Integer (atoi)
- LeetCode-8-String to Integer (atoi) (String/Num)-Easy
- Leetcode[easy] - String to Integer (atoi)
- 8. String to Integer (atoi) [easy] (Python)
- 8、String to Integer (atoi)
- 8,String to Integer (atoi)
- 8 String to Integer (atoi)
- 8 String to Integer (atoi)
- #8 String to Integer (atoi)
- 8String to Integer (atoi)
- 8 String to Integer (atoi)
- 8 - String to Integer (atoi)
- [8]String to Integer (atoi)
- 8、String to Integer (atoi)
- 【8】String to Integer (atoi)
- 8String to Integer (atoi)
- 8 String to Integer (atoi)
- Leet Code OJ 8. String to Integer (atoi) [Difficulty: Easy]
- A*算法程序
- Ora-12518 TNS:监听程序无法分发客户机
- AngularJS 指令
- 洛谷1040加分二叉树
- Swift SpriteKit 中使用 FontAwesome
- Easy 8题 String to Integer (atoi)
- AngularJS ng-model 指令
- 关于socket编程 第一节 认识socket编程
- sizeof小程序
- Bootstrap维护者宣布停止v3版本的更新(附逐浪官方镜像)
- 1.matlab中的通用命令
- 大数据平台--存储方向
- 营养与铁
- AngularJS Scope(作用域)