An Accept 0ms java solution, I think it is easy to understand


  • 0
    J

    public class Solution {
    public int lengthOfLastWord(String s) {
    int length=s.length();
    char space=' ';
    int last_word_lengh=0;
    for (int i = length-1; i >=0; i--) {
    if(space==s.charAt(i))
    {
    if(0!=last_word_lengh)
    {
    break;
    }

    		}
    		else {
    			 last_word_lengh++;
    		}
    		
    	}
    	return last_word_lengh;
    }
    

    }


Log in to reply
 

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