beat 99.75 java submission!!


  • 0
    Y

    public class Solution {
    public int myAtoi(String str) {
    int length=str.length();
    int ret=0;
    boolean negative=false;
    int i=0;
    while(i<length&&str.charAt(i)==' ') i++;
    if(i<length&&str.charAt(i)=='-') {
    negative=true;
    i++;
    }
    else if(i<length&&str.charAt(i)=='+') i++;
    for(;i<length;i++){
    char c=str.charAt(i);
    if(c>'9'||c<'0') break;
    if(ret>Integer.MAX_VALUE/10||ret==Integer.MAX_VALUE/10&&(c-'0')>Integer.MAX_VALUE%10){
    return negative==true?Integer.MIN_VALUE:Integer.MAX_VALUE;
    }
    ret=ret*10+c-'0';
    }
    return negative==true?-ret:ret;
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.