[leetcode]_String to Integer (atoi)
非常考虑思维全面性的一道题,考验是否能够考虑本问题的方方面面。
题目:将一个string转换为int。实现函数atoi()的功能。
先应该明确atoi()有哪些特殊功能:(正常的正负数情况我就不列了)
input | output |
”+1“ | 1 |
” + 1“ | 0(error了) |
” 1“ | 1(前头只有空格是合法的) |
”12b45“ | 12(取前面的数字) |
溢出 : ”2147483648“(负数情况同) | 2147483647(MAX_VALUE) |
类似我对atoi()的功能不熟的人来说,这道题就是不停WA的血泪史。每次要修正一次错误答案。
最终AC:
public int atoi(String str) {
int len = str.length();
long num = 0;
//用long型存储,以处理溢出的情况
int ifNegative = 1;
boolean numStatus = false;
for(int i = 0 ; i < len ; i++){
char ch = str.charAt(i);
if(numStatus && (ch < '0' || ch > '9')) break;
else if(numStatus && ch >= '0' && ch <= '9'){
num = num * 10 + (ch - '0');
}else if(!numStatus && ch != '-' && ch != '+' && (ch < '0' || ch > '9')){
num = 0;
break;
}else if(!numStatus && ch == '-'){
numStatus = true;
ifNegative = -1;
}else if(!numStatus && ch == '+'){
numStatus = true;
}else if(!numStatus && ch >= '0' && ch <= '9'){
numStatus = true;
num = num * 10 + (ch - '0');
} }
num *= ifNegative; int result = 0;
if(num > Integer.MAX_VALUE) result = Integer.MAX_VALUE;
else if(num < Integer.MIN_VALUE) result = Integer.MIN_VALUE;
else result = (int) num; return result; }
[leetcode]_String to Integer (atoi)的更多相关文章
- LeetCode: String to Integer (atoi) 解题报告
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- [LeetCode] String to Integer (atoi) 字符串转为整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- LeetCode OJ-- String to Integer (atoi) **
https://oj.leetcode.com/problems/string-to-integer-atoi/ 细节题,把一个字符串转换成整数 class Solution { public: in ...
- [Leetcode] String to integer atoi 字符串转换成整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [LeetCode] String to Integer (atoi) 字符串
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [LeetCode]-algorithms-String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [Leetcode]String to Integer (atoi) 简易实现方法
刚看到题就想用数组做,发现大多数解也是用数组做的,突然看到一个清新脱俗的解法: int atoi(const char *str) { ; int n; string s(str); istrings ...
- [LeetCode]String to Integer (atoi)
题意:字符串转正数 原题来自:https://leetcode.com/problems/string-to-integer-atoi/ 分析: <程序员面试宝典>上出现的面试题,主要是考 ...
- leetcode String to Integer (atoi) python
class Solution(object): def myAtoi(self, str): """ :type str: str :rtype: int "& ...
随机推荐
- DEDE织梦,文章页里的幻灯调用,能调用全部栏目的吗
arclist 是必须要有 typeid 设置的,如果你没设置,默认是取的当前栏目的 typeid,而首页取到的是 top,所以你强制指定typeid=top就可以了.---------------- ...
- nginx 均衡负载配置
nginx详细配置介绍: 参考资料:http://blog.csdn.net/xmtblog/article/details/42295181 配置实例: // nginx服务器虚拟为代理服务器和we ...
- RDLC添加页码
页脚显示页码并总页数首先添加页脚,在页脚拖一textbox,textbox的表达式输入:="第" & Globals!PageNumber & "页 共& ...
- visual studio 中快捷键的使用
我在使用编辑器的过程中是比较喜欢使用快捷键的,因为这样可以在操作中更加便捷 ①ctrl+k,ctrl+d,代码重排 ②ctrl+k,k就是ctrl键加连续两次k键,添加书签,然后通过ctrl+k,ct ...
- Android 媒体键监听以及模拟媒体键盘的实现 demo
有时我们需要程序模拟按钮或点击,而手机本身又没有,哪么可以采取其它方式 模拟实现,最后再去实际设备去测试(前期一般都拿不到设备): 如上一首,下一首,暂停等,手机上是没有的,但有些设备上是有的,所以我 ...
- 软件测试入门——测试模型(V型 W型 H型)
软件测试工程师称为“QA”,质量保证者——这是入门的第一点要学习的. 首先看基本的测试模型 1.“V”型 特点:[活动串行]这是一种古老的瀑布模型,反映了实际和测试之间的关系. 局限:仅仅把测试过程作 ...
- 转载 radio值获取
选择控件:select ,radio,checkbox之用jquery获取选中值的小结 博客分类: jQuery select下拉框radio单选按钮checkbox多选框jquery获取选中的值 ...
- WinCE下使用C#获得带毫秒的DateTime.Now
在WinCE下,使用DateTime.Now获取的系统时间是不带毫秒的,如果想要它带毫秒,需要耍点手段.话不多说,直接上代码: public static DateTimePrecisely { // ...
- ctags 文章
http://blog.csdn.net/wuziqi4/article/details/1709722
- 来自Google产品管理和营销高级副总裁Jonathan Rosenberg的42条军规(转)
#35 营造一个说 yes 的文化 你想要建立一个充满正能量和积极思考的地方.“组织里有阻碍变革发生的抗体.所以许多大公司无法创新.如果你是一个创新者,你就变成了病毒,所有的抗体都想杀死你.”在这种情 ...