Length-of-Last-Word
第76天。
快考试,可能要水一个月的easy题了。
今天的题目是Length of Last Word:
Given a string s consists of upper/lower-case alphabets and empty space characters ‘ ‘, return the length of last word in the string.
If the last word does not exist, return 0.
Note: A word is defined as a character sequence consists of non-space characters only.
Example:
Input: “Hello World”
Output: 5
一看完题目,我就想到了python的split:
1 | def lengthOfLastWord(self, s): |
然后是用c++用find去解的:
1 | int lengthOfLastWord(string s) { |
然后是从后面向前扫描的方法:
1 | int lengthOfLastWord(string s) { |
然后是dicuss中的解法,和上面的从后向前扫描的方法类似,只不过它第二个循环里面顺带计算了length:
1 | int lengthOfLastWord(string s) { |