非常考虑思维全面性的一道题,考验是否能够考虑本问题的方方面面。

题目:将一个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)的更多相关文章

  1. LeetCode: String to Integer (atoi) 解题报告

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  2. [LeetCode] String to Integer (atoi) 字符串转为整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  3. LeetCode OJ-- String to Integer (atoi) **

    https://oj.leetcode.com/problems/string-to-integer-atoi/ 细节题,把一个字符串转换成整数 class Solution { public: in ...

  4. [Leetcode] String to integer atoi 字符串转换成整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  5. [LeetCode] String to Integer (atoi) 字符串

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  6. [LeetCode]-algorithms-String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  7. [Leetcode]String to Integer (atoi) 简易实现方法

    刚看到题就想用数组做,发现大多数解也是用数组做的,突然看到一个清新脱俗的解法: int atoi(const char *str) { ; int n; string s(str); istrings ...

  8. [LeetCode]String to Integer (atoi)

    题意:字符串转正数 原题来自:https://leetcode.com/problems/string-to-integer-atoi/ 分析: <程序员面试宝典>上出现的面试题,主要是考 ...

  9. leetcode String to Integer (atoi) python

    class Solution(object): def myAtoi(self, str): """ :type str: str :rtype: int "& ...

随机推荐

  1. log4j 缓存

    log4j.appender.A2.bufferedIO=truelog4j.appender.A2.bufferSize=512000

  2. RDLC报表分页显示标题

    将报表以 XML的方式打开,搜索找到“详细信息” 在这个位置 <TablixRowHierarchy> <TablixMembers> <TablixMember> ...

  3. photoshop CS 调整选择区域的大小

    网上看到说:矩形选框不能直接调整大小,如果你不想重新画一个可以利用转换路径,然后再调整.这是不对的,矩形选框是可以调整大小的,使用"变换选区"即可.   对应步骤截图如下: 1.画 ...

  4. Mac 下 Maven 的命令行安装

    JDK 的安装 系统的“系统偏好设置”中我们可以看到 Java的设置, Java 7(含) 之后的版本在这里可以看到. 点击进去后,可以看到独立的 Java 控制面板 注意,这里是 JRE 的版本, ...

  5. Ext vtype

    //form验证中vtype的默认支持类型1.alpha //只能输入字母,无法输入其他(如数字,特殊符号等)2.alphanum//只能输入字母和数字,无法输入其他3.email//email验证, ...

  6. poj 1094 Sorting It All Out(nyoj 349)

    点击打开链接 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24544   Accep ...

  7. nyoj 76 超级台阶

    点击打开链接 超级台阶 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 有一楼梯共m级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第m级,共有多少走法? 注:规 ...

  8. Python的方法分类

    1.Python的类方法,实例方法,和静态方法 class S(object): def Test(self): print("TEST") @classmethod#类方法 de ...

  9. 用ie调试的时候显示:脚本调试程序无法连接到目标进程,已附加调试程序。

    解决方案如图所示: 解决方案: 在internet的选项工具中选中高级然后去掉禁止脚本调试的情况:

  10. 一张图看Goodle Clean设计架构

    之前用一张图分析了Google给出的MVP架构,但是在Google给出的所有案例里面除了基本的MVP架构还有其它几种架构,今天就来分析其中的Clean架构.同样的,网上介绍Clean架构的文章很多,我 ...