Easy : 1ms solution in Java


  • 0
    G
       public int lengthOfLastWord(String s) {
            
    		 s =s.trim();
    		 int len = s.length()-1;
    		 if(len ==-1 || len ==1) return 0;
    		 int originalLength = len;
    	       while(s.charAt(len)!=' '){
    	    	   if(len==0)
    	    		   return originalLength - len +1;
    	    	   len--;
    	       }
    	        return originalLength-len;
    	    
        }

Log in to reply
 

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