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

简介: 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

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.

正如题目所说,真的是考虑各种情况。

public int myAtoi(String str) {
        if (str == null || str.length() == 0)
            return 0;
        str = str.trim();
        if (str.matches("([0-9]|-|\\+)[0-9]*")) {
            if (str.charAt(0) == '+') {
                if (str.length() > 11
                        || str.length() == 1
                        || (str.length() == 11 && !str
                                .matches("[+][1-2][0-1][0-4][0-7][0-4][0-8][0-3][0-6][0-4][0-7]")))
                    return 0;
                else
                    return Integer.parseInt(str);
            }
            if (str.charAt(0) == '-') {
                if (str.length() > 11
                        || str.length() == 1
                        || (str.length() == 11 && !str
                                .matches("[-][1-2][0-1][0-4][0-7][0-4][0-8][0-3][0-6][0-4][0-8]"))) {
                    return 0;
                } else {
                    return Integer.parseInt(str);
                }
            }
            if ((str.charAt(0) + "").matches("[0-9]")) {
                if (str.length() > 10
                        || (str.length() == 10 && !str
                                .matches("[1-2][0-1][0-4][0-7][0-4][0-8][0-3][0-6][0-4][0-7]")))
                    return 0;
                else
                    return Integer.parseInt(str);
            }
        }
        return 0;
    }

上述程序是我第一次想出来的,基本情况都考虑进去了,还差一种情况没有考虑到:
这里写图片描述

下面这个就考虑到了所有情况,而且效率高很多。

public int myAtoi(String str) {
        if (str == null)
            return 0;
        str = str.trim();
        if (str.length() == 0)
            return 0;
        int index = 0;
        int sign = 1;
        if (str.charAt(index) == '+') {
            sign = -1;
            index++;
        } else if (str.charAt(index) == '-')
            index++;
        long num = 0;
        for (; index < str.length(); index++) {
            System.out.println(str.charAt(index));
            if (str.charAt(index) < '0' || str.charAt(index) > '9')
                break;
            num = num * 10 + (str.charAt(index) - '0');
            if (num > Integer.MAX_VALUE)
                break;
        }
        if (num * sign >= Integer.MAX_VALUE)
            return Integer.MAX_VALUE;
        if (num * sign <= Integer.MIN_VALUE)
            return Integer.MIN_VALUE;
        return (int) num * sign;
    }
目录
相关文章
|
算法 C++
Leetcode第八题(字符串转换整数(atoi))
这篇文章介绍了LeetCode上第8题“字符串转换整数(atoi)”的解题思路和C++的实现方法,包括处理前导空格、正负号、连续数字字符以及整数溢出的情况。
434 0
LeetCode第8题字符串转换整数 (atoi)
该文章介绍了 LeetCode 第 8 题字符串转换整数 (atoi)的解法,需要对字符串进行格式解析与校验,去除前导空格和处理正负号,通过从高位到低位的计算方式将字符串转换为整数,并处理越界情况。同时总结了这几道题都需要对数字的表示有理解。
LeetCode第8题字符串转换整数 (atoi)
|
SQL 算法 数据可视化
LeetCode第八题:字符串转换整数 (atoi)【8/1000 python】
LeetCode第八题:字符串转换整数 (atoi)【8/1000 python】
|
存储 算法 C#
Leetcode算法系列| 8. 字符串转换整数 (atoi)
Leetcode算法系列| 8. 字符串转换整数 (atoi)
|
算法 C++ Python
【力扣算法11】之 8. 字符串转换整数 (atoi) python
【力扣算法11】之 8. 字符串转换整数 (atoi) python
379 0
|
存储 算法 测试技术
力扣7-整数反转&力扣8-字符串转换整数 (atoi)
力扣7-整数反转&力扣8-字符串转换整数 (atoi)
260 0
|
算法 安全 Swift
LeetCode - #8 字符串转换整数(atoi)
不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。如果大家有建议和意见欢迎在文末留言,我们会尽力满足大家的需求。
247 0
|
存储 测试技术
leetcode:8.字符串转换正数(atoi)
当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。
179 0
力扣 8. 字符串转换整数 (atoi) 解题
力扣 8. 字符串转换整数 (atoi) 解题
247 0
|
算法 C++
模拟实现atoi函数(将数字字符串转换为整型)附加leetcode练习题
各位朋友们,大家好啊!今天我为大家分享的知识是如何模拟实现atoi函数。相信大家如果能够理解这个知识,对大家以后的刷题是有帮助的。