leetcode-8。文字列は数値に変換されます

件名:https : //leetcode-cn.com/problems/string-to-integer-atoi/

回答:文字列 

public int myAtoi(String str){

  str = str.trim();

        int len = str.length();

        if(len == 0)は0を返します。

        char [] chars = str.toCharArray();

        ブール負= false;

        intインデックス= 0;

        if(chars [index] == '-'){

            負=真;

            index ++;

        } else if(chars [index] == '+'){

            index ++;

        } else if(!Character.isDigit(chars [index])){

            0を返します。

        }

        int年= 0;

        while(index <len && Character.isDigit(chars [index])){

            int digit = chars [index]-'0';

            if(ans>(Integer.MAX_VALUE-digit)/ 10){

                マイナスを返す?Integer.MIN_VALUE:Integer.MAX_VALUE;

            }

            年=年* 10 +桁;

            index ++;

        }

        マイナスを返す?-ans:ans;

    }

おすすめ

転載: blog.csdn.net/wuqiqi1992/article/details/108366342