[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 "& ...
随机推荐
- Dynamics CRM 2011 权限管理(转)
http://www.cnblogs.com/LeoTang/p/3344265.html Dynamics CRM 2011 权限管理 CRM系统基于角色的权限主要通过部门.角色.用户.团队来 进行 ...
- virtualenvwrapper
VirtualEnv 是什么 VirtualEnv用于在一台机器上创建多个独立的python运行环境,VirtualEnvWrapper为前者提供了一些便利的命令行上的封装. 为什么要用 - 隔离项目 ...
- flash 读取系统默认编码
java有类可以直接读取,但貌似flash没有. Charset.defaultCharset(); 但是浏览器里可以有. document.defaultCharset;//从当前的区域语言中获取默 ...
- Collections.emptyList() and Collections.EMPTY_LIST
返回的emptyList不支持add()
- H264-AVS POC理解
H264码流的输出顺序是编码顺序,所以在编码B帧的时候,由于B是双向预测,需要先编码后面编码帧P/I,这时候先输出I/P,后面才有B帧. 在解码段拿到相应的I/P帧后,不能马上丢到buffer lis ...
- WebsitePanel(wsp)配置详解(安装指南)
WebsitePanel(wsp)配置详解(安装指南) 铁卫士原创 估计很多同学都还不知道WebsitePanel是什么东东吧,WebsitePanel简称wsp是微软旗下,开源免费的虚拟主机系统,我 ...
- 测试方法---"SFDIPOT"
SFDIPOT方法是快速测试的一种方法,可以帮助你快速理清测试点. 我粗略地想了一下,凡是面试时我遇到过的奇葩的让我测一个电梯.雨伞.电话.水杯.测一个奇怪的东西的面试题上都能用. 然后嘛,日常工作中 ...
- 0901~0907面试总结(腾讯CDC、金蝶)
纯脑记,但应该不会差太多 20150901腾讯CDC面试(初级外包岗) 0826的上午先用QQ进行了初步沟通,要求做一个不考虑AI的井字棋游戏,0830上午E-mail上交了做好的DEMO,然后等了几 ...
- 传智Java基础知识测试
共40道选择题,每题2.5分.多选题有错则全错,全对才满分. 单选题: 1. 下列哪个声明是错误的?(B) A. int i=10; B. float f=1.1; C. double ...
- 翻译「C++ Rvalue References Explained」C++右值引用详解 Part8:Perfect Forwarding(完美转发):解决方案
本文为第八部分,目录请参阅概述部分:http://www.cnblogs.com/harrywong/p/cpp-rvalue-references-explained-introduction.ht ...