资讯专栏INFORMATION COLUMN

Leetcode 8 String to Integer (atoi)

cod7ce / 1095人阅读

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

Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a
challenge, please do not see below and ask yourself what are the
possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no
given input specs). You are responsible to gather all the input
requirements up front.

Requirements for atoi: The function first discards as many whitespace
characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible,
and interprets them as a numerical value.

The string can contain additional characters after those that form the
integral number, which are ignored and have no effect on the behavior
of this function.

If the first sequence of non-whitespace characters in str is not a
valid integral number, or if no such sequence exists because either
str is empty or it contains only whitespace characters, no conversion
is performed.

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.

难度: Easy

atoi是c标准库中的一个函数, 可以将字符串表示的整数转换为int. 现在要求我们自己来实现它.

解题过程中, 主要有以下两点需要注意:

字符串开头可能出现 + 或者 - , 需要处理

使用long来记录中间结果防止溢出

下面是AC的解法:

public class Solution {

    public int myAtoi(String str) {
        str = str.trim();
        if (str.length() == 0) {
            return 0;
        }
        int flag = 1;
        int start = 0;
        long sum = 0;
        if (str.charAt(0) == "-") {
            flag = -1;
            start = 1;
        } else if (str.charAt(0) == "+") {
            flag = 1;
            start = 1;
        }
        for (int i = start; i < str.length(); i++) {
            if (str.charAt(i) > "9" || str.charAt(i) < "0") {
                return this.getSuitInt(sum * flag);
            }
            sum = sum * 10 + (str.charAt(i) - "0");
            if (sum >= Integer.MAX_VALUE) {
                break;
            }
        }
        return this.getSuitInt(sum * flag);
    }

    private int getSuitInt(long sum) {
        if (sum > Integer.MAX_VALUE) {
            return Integer.MAX_VALUE;
        } else if (sum < Integer.MIN_VALUE) {
            return Integer.MIN_VALUE;
        } else {
            return (int) sum;
        }
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.myAtoi("0"));
        System.out.println(s.myAtoi("4"));
        System.out.println(s.myAtoi("18384"));
        System.out.println(s.myAtoi("-10203"));
        System.out.println(s.myAtoi("+304040"));
        System.out.println(s.myAtoi("9223372036854775809"));
    }
}

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

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

相关文章

  • [LeetCode] 8. String to Integer (atoi)

    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...

    cuieney 评论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条评论

cod7ce

|高级讲师

TA的文章

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