Skip to content

eMahtab/string-to-integer-atoi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

Sring to integer atoi

Implementation :

class Solution {
    public int myAtoi(String str) {
       if (str == null) return 0;
	   str = str.trim();
       if (str.length() == 0) return 0;
        
       char firstChar = str.charAt(0);
       int sign = 1, start = 0, len = str.length();
       long sum = 0;
       if (firstChar == '+') {
            sign = 1;
            start++;
       } else if (firstChar == '-') {
            sign = -1;
            start++;
       }
        
       for (int i = start; i < len; i++) {
            if (!Character.isDigit(str.charAt(i)))
                return (int) sum * sign;
            sum = sum * 10 + str.charAt(i) - '0';
            if (sign == 1 && sum > Integer.MAX_VALUE)
                return Integer.MAX_VALUE;
            if (sign == -1 && (-1) * sum < Integer.MIN_VALUE)
                return Integer.MIN_VALUE;
       }

     return (int) sum * sign;
    }
}

Releases

No releases published

Packages

No packages published