字符转换 字符串转换整数 (atoi) 字符转数字(char转int方法)

请你来实现一个 atoi 函数,使其能将字符串转换成整数。

首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。

当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。

该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。

注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。

在任何情况下,若函数不能进行有效的转换时,请返回 0。

说明:

假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [−231,  231 − 1]。如果数值超过这个范围,请返回  INT_MAX (231 − 1) 或 INT_MIN (−231)

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/string-to-integer-atoi

我的实现:

class Solution {

    public int myAtoi(String str) {

      int lenth=str.length();

        if(lenth==0) return 0;

        int tag=1;

        Stack<Integer> st = new Stack<Integer>();

        int i=0;


        if(Character.isDigit(str.charAt(0))){

            if(lenth==1) return str.charAt(0)-'0';

            if(str.charAt(i)!='0'){

            st.push(str.charAt(0)-'0');}

            i=1;

            while(i<lenth&&Character.isDigit(str.charAt(i))){

            if(!(st.isEmpty()&&str.charAt(i)=='0')){


                st.push(str.charAt(i)-'0');


            }

            i++;

            }


        }

            else if(str.charAt(0)=='-'||str.charAt(0)=='+'){

                if(lenth==1) return 0;

                if(str.charAt(0)=='-') tag=-1;


                i=1;

                while(i<lenth&&Character.isDigit(str.charAt(i))){

                if(!(st.isEmpty()&&str.charAt(i)=='0')){


                        st.push(str.charAt(i)-'0');


                    }

                i++;

                }


            }

            else if(str.charAt(0)==' '){


                if(lenth==1) return 0;


                i=1;

                while(i<lenth&&str.charAt(i)==' ') {i++;}

                if(i==lenth) return 0;

                if(str.charAt(i)=='-'){tag=-1;i++;}

                if(str.charAt(i)=='+'){i++;}


                while(i<lenth&&Character.isDigit(str.charAt(i))){

                if(!(st.isEmpty()&&str.charAt(i)=='0')){


                        st.push(str.charAt(i)-'0');


                    }

                i++;


                }






            }

            else{

                return 0;

            }


            long num=0;

            int a=0;

            int temp=0;

            int tt=0;

            while(! st.isEmpty()){


                a = (Integer) st.pop().intValue();

                num=num+a*(long)Math.pow(10,temp);

                tt++;

                if(tag==1){

                if (num>Integer.MAX_VALUE||tt>=11) return Integer.MAX_VALUE;}

                if(tag==-1){

                if (num*-1 < Integer.MIN_VALUE||tt>=11) return Integer.MIN_VALUE;}


                temp++;

            }

            return (int)num*tag;

    }

}

这道题走了n多坑,坑总结: while(Character.isDigit(str.charAt(i)))-> while(i<lenth&&Character.isDigit(str.charAt(i)))

原因:没有考虑到str.charAt(i)会越界的问题;

   if (num>Integer.MAX_VALUE&&tt>=10)-> if (num>Integer.MAX_VALUE||tt>=11)

原因:在遇到20000000000000000的时候报错,原因是当num有数的时候直接超出了long的可表示范围;

 num=num+a*Math.pow(10,temp);-> num=num+a*(long)Math.pow(10,temp);

原因:Math.pow(10,temp)可能超过int存储范围,数组会截取

没有考虑-00000123456这种情况

知识点总结:

判断str某个字符是不是数字? Character.isDigit(str.charAt(i))

字符转数字(char转int方法): str.charAt(i)-'0'


letcode精选方法:

python一行:

class Solution:

    def myAtoi(self, s: str) -> int:

        return max(min(int(*re.findall('^[\+\-]?\d+', s.lstrip())), 2**31 - 1), -2**31)


使用正则表达式:

^:匹配字符串开头

[\+\-]:代表一个+字符或-字符

?:前面一个字符可有可无

\d:一个数字

+:前面一个字符的一个或多个

\D:一个非数字字符

*:前面一个字符的0个或多个

max(min(数字, 2**31 - 1), -2**31) 用来防止结果越界

作者:QQqun902025048

链接:https://leetcode-cn.com/problems/string-to-integer-atoi/solution/python-1xing-zheng-ze-biao-da-shi-by-knifezhu/

来源:力扣(LeetCode)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。