题目
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
总结
这个题目很简单,本来没有记录的意义,但是我犯了一个特别愚笨的错误
我的解法:首先去除头尾两边的空格,再从0开始遍历字符串记录最后的长度
反思:碰到这类求“最后“问题时,首先考虑能否把问题从末尾开始计算。把“最后“的问题转化为“最先“的问题。可以大幅度降低问题求解的时间,理论上从O(N)降低为O(S),N:字符串长度,S:解长度