JAVA solution without trim()


  • 0
    W

    As @stellari says,"our solution is very practical, but note that what your interviewer wants to hear more about is probably how you would deal with this problem on a lower level, without the help of those standard library functions. If you give an answer like this, then your interviewer will probably ask you to implement two more functions: trim and lastIndexOf."

    public static int lengthOfLastWord(String s) {
          int a=0; 
        for(int i=s.length()-1;i>=0;i--){
        	if(s.charAt(i)!=' '){
        		a++;
        	}else if(a>0){
        		break;
        	}
        	
        }
        return a;
        }
    

Log in to reply
 

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