8. String to Integer (atoi) 字符串转为int类型的所有可能情况

8. String to Integer (atoi)

问题:

输入一个字符串,将字符串转为int类型,处理所有可能的输入情况。

可能的输入情况:

1.字符串为空。即“”。

2.首先是假设输入的字符串都是数字型的,可含正负号,例如12345,+12548,-15568。

3.字符串中含有非数字的其他字符类型,例如a,-sd,-1286dgg558,000822fg55。

4.字符串首尾含有空格的,例如 -558dg12, 12dc12 。

5.针对转换后的数字越界,有两种情况,一个是超过int型的表示范围,一个是超过long型的表示范围。

自己写的:(java)

public class Solution {
    public int myAtoi(String str) {
        //4、处理带空格的情况,将所有首尾空格先删掉
        str=str.trim();
        char[] charSplitArray = str.toCharArray();
        int result = 0;
        //1、处理字符串为空的情况,直接返回0
        if (str.equals("")) {
            return result;
        }
        //2、带符号的情况判断。判断第一个位置是不是符号。是符号且长度大于1,则继续,不是符号且非数字,或者是符号但是长度为1,直接return 0.
        int intLetter = (int)charSplitArray[0];
        if (charSplitArray.length==1 && (intLetter < 48 || intLetter > 57) ) {
           return 0;
        }
        //针对带符号类型和不带符号类型分别处理。
        if (intLetter==45 || intLetter==43) {
            result = positiveNum(charSplitArray,1,str,intLetter);
            System.out.println(result);
        }
        else {
            result = positiveNum(charSplitArray,0,str,0);
        }
        return result;
    }

    //对不带符号的统一处理逻辑。
    public int positiveNum(char[] charArray,int startIndex,String strPos,int symbolTag) {
        int resultPos = 0;
        for (int i=startIndex; i<charArray.length; i++) {
            int intLetter = (int)charArray[i];
            //如果符号后面跟的不是数字,则不能转换
            if ((intLetter < 48 || intLetter > 57) && i==startIndex) {
                return 0;
            }
            //3、处理字符串中含有非数字的其他字符类型。例如“  -0023asd23”
            if ((intLetter < 48 || intLetter > 57)) {
                strPos = String.copyValueOf(charArray,startIndex,i-startIndex);
                //5、判断超出long型的数据。
                if (i-startIndex > 18) {
                    if (symbolTag == 45) {
                        return -2147483648;
                    }else {
                        return 2147483647;
                    }
                }
                break;
            }
            //符号后面全为数字时
            if ((intLetter >= 48 && intLetter <= 57 && i==charArray.length-1)) {
                strPos = String.copyValueOf(charArray,startIndex,i-startIndex+1);
            }
            //5、判断超出long型的数据。
            if (charArray.length > 18) {
                if (symbolTag == 45) {
                    return -2147483648;
                }else {
                    return 2147483647;
                }
            }
        }
        long longResult=Long.parseLong(strPos);
        if (symbolTag==45) {
            longResult = 0-longResult;
        }
        //5、判断超出int型的数据。
        if (longResult > 2147483647) {
            resultPos = 2147483647;
        }
        else if (longResult < -2147483648) {
            resultPos = -2147483648;
        }
        else {
            resultPos = new Long(longResult).intValue();
        }
        return resultPos;
    }
}

思路:

1、首先清除字符串首尾的空格。

2、判断字符串是否为空,为空则直接返回0。

3、判断是否存在符号,首先将“-”这种仅含有符号的字符串直接排除返回0。则还存在三种情况,例如”+2232”这种带有正号的字符串;”-1”这种带有负号的字符串,以及”23”这种不带有符号的字符串。

4、将所有带符号的字符串先去掉符号,和不带符号的字符串统一通过positiveNum函数进行处理。传入参数有,字符数组,处理起始位置(有符号时传入数值为1,无符号时为0),字符串,符号位判断(传入第一位的ascii码值)。这里分析的是positiveNum函数的处理逻辑,判断的情况有:(1)符号后面一位,这里就直接是处理起始位置(传入的参数)不是数字,例如”-a”,直接返回0。(2)字符串中含有非数字类型,例如“23as12”,则遇到非数字类型时就要只取前面的数字类型,将后面全部舍弃,处理完转换为long型之前,需要判断是否会越界。(3)字符串中全部为数字类型,在转换为long型之前需要判断是否越界。

5、将处理后的字符串转换为long型,通过传入参数判断字符数组第一位是否为负号,为负号则在long型数据上加上负号。

6、判断数据是否超出int型,超出则直接处理,未超出则转为int型。

代码精简版:

。。。

时间: 2024-08-06 16:06:07

8. String to Integer (atoi) 字符串转为int类型的所有可能情况的相关文章

[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 to be spe

leetcode——String to Integer (atoi) 字符串转换为整型数(AC)

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

leetCode 8. String to Integer (atoi) 字符串

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 intende

leetcode - String to Integer (atoi) 字符串转整数

Implement atoi to convert a string to an integer. 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. 题目:实现字符串转整数 注意事项:考虑好各种可能的输入(坑); public class Solution

【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]41. 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] 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] 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 OJ String to Integer (atoi) 字符串转数字

1 #include <iostream> 2 #include <assert.h> 3 using namespace std; 4 int ato(const char *str) { 5 int i=0,e=0,s=0; 6 int max=2147483647,min=-2147483648; 7 int f=1; 8 int tem[10]={0}; 9 unsigned int pan=0; 10 while(*str==' '){ //过滤掉连续空格 11 str+