Leetcode-8(Java) 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 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.

传送门:https://leetcode.com/problems/string-to-integer-atoi/

把字符串转换成整型,注意空格和符号即可。

public class Solution {
    public int myAtoi(String str)
    {
        if(str == null)
            return 0;
        str = str.trim();//去掉字符串前面的空格
        int len = str.length();
        if(len == 0)
            return 0;
        char signal = ‘+‘;
        double res = 0;
        for(int i = 0; i < str.length(); i++)
        {
            char ch = str.charAt(i);
            if(ch == ‘-‘ || ch == ‘+‘)
            {
                if(i != 0)
                    return 0;
                else
                {
                    if(ch == ‘-‘)
                        signal = ‘-‘;
                }
            }
            else if(ch >= ‘0‘ && ch <= ‘9‘)
            {
                res = res * 10 + ch - ‘0‘;
            }
            else
                break;
        }
        if(signal == ‘-‘)
            res = -res;
        if(res > Integer.MAX_VALUE)
            return Integer.MAX_VALUE;
        else if(res < Integer.MIN_VALUE)
            return Integer.MIN_VALUE;
        else
            return (int)res;
    }
}
时间: 2024-10-10 09:24:52

Leetcode-8(Java) String to Integer (atoi)的相关文章

【leetcode系列】String to Integer (atoi)

这个我就直接上代码了,最开始把"abc123"也算作合法的了,后来查了一下atoi的定义,把这种去掉了. public class Solution { public static int atoi(String inStr) { long result = 0L; /* * 网上查了一下,atoi函数的定义是如果第一个非空格字符存在,是数字或者正负号则开始做类型转换, * 之后检测到非数字(包括结束符\0)字符时停止转换,返回整型数.否则,返回零.可能的输入情况有: 1.空字符串 *

【LeetCode】008 String to Integer (atoi)

题目:LeetCode 008 String to Integer 题意:完成内置函数atoi的功能,将字符串转换成整数. 教训:一开始理所应当的随便一写,然后发现有很多的异常情况需要处理.然后按照C++ Reference中关于atoi的规定一条一条写,才AC.另外还有一个溢出的问题,一开始以为int会自动处理直接返回边界值,其实不是,如果溢出的话大于2147483647的数会给变成负数,因此要单独判断是否会大,但是设置成longlong 之后出现的问题是,还有可能会溢出longlong,所以

[LeetCode][Java] 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 input cases. Notes: It is intended for this problem to be

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

[题目] Implement atoi to convert a string to an integer. [题目解析] 该题目比较常见,从LeetCode上看代码通过率却只有13.7%,于是编码提交,反复修改了三四次才完全通过.该题目主要需要考虑各种测试用例的情况,比如"+5"."   67"."   +0078"."  12a56--".int的最大值最小值溢出等情况,通过这个题目,联系到实际项目中,我们一定要多考虑边界

【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 input cases. Notes: It is intended for this problem to be

leetcode笔记: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 input cases. Notes: It is intended for this problem t

【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 input cases. Notes: It is intended for this problem to be spe

LeetCode Medium: 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 input cases. Notes: It is intended for this problem to b

LeetCode题解 #8 String to Integer (atoi)

又是一道恶心的简单题. 一开始没想到这么多情况的,幸好LeetCode是个很人性化的oj,能让你知道你在哪个case上错了,否则一辈子都过不了. 考虑不周到只能一个个补了. 列举一下恶心的case //" 010" //" +004500" //" -0012a42" //"2147483648" //" b11228552307" //"18446744073709551617" //