[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 "& ...
随机推荐
- sikuli实例
代码: package selenium.sikuli; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; im ...
- TFS如何设置在客户端独占签出
步骤:1.打开源代码管理资源管理器,点击“工作区”的下拉框,选择,“工作区”2.选择编辑3.选择“高级”4.进入编辑工作区,tfs中“位置”选项中,默认的时本地,如果想独占签出,这里我们就必须设置成“ ...
- 安装Weblogic11g
1.下载weblogic11g http://www.oracle.com/technetwork/middleware/weblogic/downloads/wls-for-dev-1703574. ...
- SpringMVC 注解事务
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactio ...
- 在点击HOME键时, 在点击icon回到原来的应用。
参考资料: http://www.linuxidc.com/Linux/2012-01/51332.htm
- 【转】 ip段/数字,如192.168.0.1/24是什么意思?
http://blog.csdn.net/aerchi/article/details/39396423 ip段/数字,如192.168.0.1/24是什么意思? ip段/数字,如192.168.0. ...
- SQL语句在OLAP的妙用(多维分析与指标计算)
================================================================================ BI传统实现原理: 1.涉及维度管理( ...
- 用js控制选项卡的隐藏与显示
通过使用ul和div来,借助于jquery来实现选项卡的显示与隐藏 <form action="" method="post"> <div&g ...
- x86_64平台编译链接汇编程序
assemble: nasm -f elf32 sample.asm -l sample.lst link: ld -m elf_i386 -o test sample.o io.o
- [原创] IIS7下顶级域名301跳转到WWW域名
百度搜索了众多方法,居然没有一个全面的IIS7下301域名跳转能用的教程,最终自己研究出了个可以用的供大家参考.1.绑定域名01ruodian.cn www.01ruodian.cn到空间: 2.在I ...