8. String to Integer (atoi)

public class Solution {
    public int myAtoi(String str) {
        if(str == null||str.length()<=0)
           return 0;
        str = str.trim();
        if(str.length()<=0)
           return 0;
        char flag = '+';
        int i = 0;
        if(str.charAt(0)=='-')
         {
            flag = '-';
            i++;
         }
        else if(str.charAt(0)=='+')
            i++;
    //    System.out.print(i);
        double s = 0;
        while(i='0'&&str.charAt(i)<='9')
             {
                 s = 10*s+(str.charAt(i++)-'0');
                // System.out.println(s);
             }
        if(flag == '-')
            s=-s;
       
        if(s>Integer.MAX_VALUE)
            return Integer.MAX_VALUE;
       
        if(s

你可能感兴趣的:(8. String to Integer (atoi))