String to Integer conversion:


  • 0
    B

    Re: Java Solution with 4 steps explanations

    i wrote this code:
    public int myAtoi(String str) {
    int result=0;
    if(str.equals("")){
    result=0;
    }
    else{
    String [] s1= str.split("");
    outer:for(int i=0;i<s1.length;i++){
    char c= (str.charAt(i));
    int x=c;
    if(i==0 &&(x==43 || x==45) && str.length()>1)
    result=1;
    else{
    int y=Character.getNumericValue(str.charAt(i));
    if(y>10 || y<0){
    result=0;
    break outer;
    }else{
    result=1;
    }
    }
    }
    }
    return result;
    }

    the solution is getting rejected. May be i have not understood the requirements clearly . Please can any one check and let me know ?


Log in to reply
 

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