image.png

解题思路

image.png

  1. public int lengthOfLastWord(String s) {
  2. int end = s.length() - 1;
  3. while(end >= 0 && s.charAt(end) == ' ') end--;
  4. if(end < 0) return 0;
  5. int start = end;
  6. while(start >= 0 && s.charAt(start) != ' ') start--;
  7. return end - start;
  8. }