资讯专栏INFORMATION COLUMN

[LeetCode] 8. String to Integer (atoi)

cuieney / 726人阅读

Problem

Implement function atoi to convert a string to an integer.

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.

Example
"10" => 10
"-1" => -1
"123123123123123" => 2147483647
"1.0" => 1

Solution
public class Solution {
    public int myAtoi(String str) {
        str = str.trim();
        boolean isNeg = false;
        int res = 0;
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (i == 0 && (ch == "+" || ch == "-")) isNeg = ch == "+" ? false: true;
            else if (ch >= "0" && ch <= "9") {
                int cur = ch - "0";
                if (res > (Integer.MAX_VALUE-cur)/10) return isNeg ? Integer.MIN_VALUE: Integer.MAX_VALUE;
                else res = res*10+cur;
            }
            else return isNeg ? -res: res; 
        }
        return isNeg? -res: res;
    }
}
Update 2018-10
class Solution {
    public int myAtoi(String str) {
        str = str.trim();
        if (str == null || str.length() == 0) return 0;
        boolean isPositive = true;
        int index = 0;
        if (str.charAt(0) == "-") {
            isPositive = false;
            index++;
        }
        if (str.charAt(0) == "+") {
            index++;
        }
        int sum = 0;
        while (index < str.length()) {
            char ch = str.charAt(index);
            if (ch < "0" || ch > "9") break;
            int digit = str.charAt(index)-"0";
            if ((Integer.MAX_VALUE-digit)/10 < sum) {
                return isPositive ? Integer.MAX_VALUE : Integer.MIN_VALUE;
            }
            sum = sum*10+digit;
            index++;
        }
        return isPositive ? sum : -sum;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65973.html

相关文章

  • Leetcode 8 String to Integer (atoi)

    摘要:难度是标准库中的一个函数可以将字符串表示的整数转换为现在要求我们自己来实现它解题过程中主要有以下两点需要注意字符串开头可能出现或者需要处理使用来记录中间结果防止溢出下面是的解法 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If ...

    cod7ce 评论0 收藏0
  • [Leetcode] String to Integer (atoi) 字符串转整数

    摘要:通用方法复杂度时间空间思路字符串题一般考查的都是边界条件特殊情况的处理。所以遇到此题一定要问清楚各种条件下的输入输出应该是什么样的。 String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input...

    Astrian 评论0 收藏0
  • July 算法习题 - 字符串2 + Leetcode 8,9

    摘要:判断一条单向链表是不是回文解法可以借助栈,将遍历到的前半段链表节点放入栈,后半段每当遍历到一个,都要与出栈的节点相比较。如果中间出现不相等的情况,则不是回文。 [July 程序员编程艺术:面试和算法心得题目及习题][1] 字符串转换成整数 also Leetcode 8 String to Integer (atoi) 题目描述 输入一个由数字组成的字符串,把它转换成整...

    timger 评论0 收藏0
  • LeetCode 之 JavaScript 解答第8题 —— 字符串转换整数 (String to

    摘要:当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。数字前正负号要保留。 Time:2019/4/19Title: String To IntegerDifficulty: MediumAuthor: 小鹿 题目:String To Integer(字...

    zhisheng 评论0 收藏0
  • 实现atoi函数(stringinteger)

    摘要:实现函数转思路利用内置的函数可以将字符串快速转换成型利用是否抛出异常来快速判断能否被转换成,进而迅速确定输入字符串中第一个非数字字符的位置需要注意处理符号的问题代码如果是或者但是会抛出异常,此时返回由于的没有取值上限,如果规定为 实现atoi函数(string转integer) String to Integer (atoi) Implement atoi to convert a ...

    leanote 评论0 收藏0

发表评论

0条评论

cuieney

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<