LintCode解题思路LintCode解题思路

OJ lintcode 最后一个单词的长度

2017-02-19  本文已影响3人  DayDayUpppppp

给定一个字符串, 包含大小写字母、空格' ',请返回其最后一个单词的长度。
如果不存在最后一个单词,请返回 0 。
注意事项
一个单词的界定是,由字母组成,但不包含任何的空格。
您在真实的面试中是否遇到过这个题?
Yes
样例
给定 s = "Hello World",返回 5。

class Solution {
public:
    /**
     * @param s A string
     * @return the length of last word
     */
    int lengthOfLastWord(string& s) {
        // Write your code here
        int index=s.rfind(' ');
        int length=s.length();
        return length-index-1;
    }
};
上一篇 下一篇

猜你喜欢

热点阅读