1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.demo.s58;
public class Solution { public int lengthOfLastWord(String s) { int end = s.length() - 1; while(end >= 0 && s.charAt(end) == ' ') end--; if(end < 0) return 0; int start = end; while(start >= 0 && s.charAt(start) != ' ') start--; return end - start; } }
|