This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int lengthOfLastWord(String s) { | |
int end = s.length() - 1; | |
while (end >= 0 && s.charAt(end) == ' ') { | |
end--; | |
} | |
int start = end; | |
while (start >= 0 && s.charAt(start) != ' ') { | |
start--; | |
} | |
if (end >= 0) { | |
return end - start; | |
} | |
return 0; | |
} | |
} |
没有评论:
发表评论