java 8. String to Integer(atoi).java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 8. String to Integer(atoi).java相关的知识,希望对你有一定的参考价值。

"""
Requirements for atoi:
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. 
Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, 
and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, 
which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, 
or if no such sequence exists because either str is empty or it contains only whitespace characters, 
no conversion is performed.
If no valid conversion could be performed, a zero value is returned. 
If the correct value is out of the range of representable values, 
INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.




1.discards all leading whitespaces eg:  "    010"     | 10
                                        "01    0"     |  1
2.sign of the number               eg:  "+1"          | 1
                                        "-1"          | -1
3.overflow                         eg:  "2147483648"  | 2147483647
                                        "-21474836489"| -2147483648
4.invalid input                    eg:  "-1....3"     | -1
                                        ""            | 0 
                                        "+++1"        | 0

Your input

""
"+1"
"+++1"
"1.22222223"
"2147483648"
"21474836489999"
"-21474836489"
"2-3"
"1.56"
"-1....3"
"0.000001"

"""
class Solution(object):
    def myAtoi(self, str):
        """
        :type str: str
        :rtype: int
        """
        
        if not str:
            return 0;
        
       
        res = 0;
        
        digits = {'0' : 0,
                  '1' : 1,
                  '2' : 2,
                  '3' : 3,
                  '4' : 4,
                  '5' : 5,
                  '6' : 6,
                  '7' : 7,
                  '8' : 8,
                  '9' : 9}
        flag = 0;
        
        a = 0;
        while(str[a] == ' '):
            a += 1;
        str = str[a:];
        
        if str[0] != '-' and str[0] != '+' and str[0] not in digits:
            return 0;
        elif str[0] == '-':
            flag = 1;
        elif str[0] == '+':
            flag = 0;
        else:
            res = digits[str[0]];
            
        for i in str[1:]:
            if i not in digits:
                if flag == 1:
                    return res * -1;
                return res;
            else:
                res = res * 10 + digits[i];
                if res >= 2147483647 and flag == 0:
                    return 2147483647;
                elif res >= 2147483648 and flag == 1:
                    return -2147483648
        
        if flag == 1:
            return res * -1;
            
        return res;
                
public class Solution {
    public int myAtoi(String str) {
        if(str == null || str == "") return 0;
        boolean valid = false;
        long res = 0;
        int flag = 1;
        for(int i = 0; i < str.length(); i++){
            char temp = str.charAt(i);
            if(temp == ' ' && !valid){
                continue;
            } else if(temp >= '0' && temp <= '9'){
                valid = true;
                res = res * 10 + temp - '0';
            } else if(temp == '+' && !valid){
                valid = true;
            } else if(temp == '-' && !valid){
                valid = true;
                flag = -1;
            } else {
                return (int)res * flag;
            }
            
            if( flag == 1 && res >= Integer.MAX_VALUE){
                return Integer.MAX_VALUE;
            } else if(flag == -1 && res * (-1) <= Integer.MIN_VALUE){
                return Integer.MIN_VALUE;
            }
        }
        return (int)res * flag ;
    }
}

/*
""
"    010"
"01    0"
"+1"
"-1"
"2147483648"
"-21474836489"
"-1....3"
"+++1"
*/

以上是关于java 8. String to Integer(atoi).java的主要内容,如果未能解决你的问题,请参考以下文章

java 8. String to Integer(atoi).java

java 8. String to Integer(atoi).java

java 8. String to Integer(atoi).java

java 8. String to Integer(atoi).java

[LeetCode][8]String to Integer (atoi)解析与模仿Java源码实现 -Java实现

[LeetCode][8]String to Integer (atoi)解析与模仿Java源码实现 -Java实现