Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LeetCode] 58. Length of Last Word #58

Open
grandyang opened this issue May 30, 2019 · 2 comments
Open

[LeetCode] 58. Length of Last Word #58

grandyang opened this issue May 30, 2019 · 2 comments

Comments

@grandyang
Copy link
Owner

grandyang commented May 30, 2019


请点击下方图片观看讲解视频
Click below image to watch YouTube Video
Video

Given a string s consisting of words and spaces, return the length of the last word in the string.

A word is a maximal substring consisting of non-space characters only.

Example 1:

Input: s = "Hello World"
Output: 5
Explanation: The last word is "World" with length 5.

Example 2:

Input: s = "   fly me   to   the moon  "
Output: 4
Explanation: The last word is "moon" with length 4.

Example 3:

Input: s = "luffy is still joyboy"
Output: 6
Explanation: The last word is "joyboy" with length 6.

Constraints:

  • 1 <= s.length <= 10^4
  • s consists of only English letters and spaces ' '.
  • There will be at least one word in s.

这道题难度不是很大。先对输入字符串做预处理,去掉开头和结尾的空格,然后用一个计数器来累计非空格的字符串的长度,遇到空格则将计数器清零,参见代码如下:

解法一:

class Solution {
public:
    int lengthOfLastWord(string s) {
        int left = 0, right = (int)s.size() - 1, res = 0;
        while (s[left] == ' ') ++left;
        while (s[right] == ' ') --right;
        for (int i = left; i <= right; ++i) {
            if (s[i] == ' ') res = 0;
            else ++res;
        }
        return res;
    }
};

昨晚睡觉前又想到了一种解法,其实不用上面那么复杂的,这里关心的主要是非空格的字符,那么实际上在遍历字符串的时候,如果遇到非空格的字符,只需要判断其前面一个位置的字符是否为空格,如果是的话,那么当前肯定是一个新词的开始,将计数器重置为1,如果不是的话,说明正在统计一个词的长度,计数器自增1即可。但是需要注意的是,当 i=0 的时候,无法访问前一个字符,所以这种情况要特别判断一下,归为计数器自增1那类,参见代码如下:

解法二:

class Solution {
public:
    int lengthOfLastWord(string s) {
        int res = 0;
        for (int i = 0; i < s.size(); ++i) {
            if (s[i] != ' ') {
                if (i != 0 && s[i - 1] == ' ') res = 1;
                else ++res;
            }
        }
        return res;
    }
};

下面这种方法是第一种解法的优化版本,由于只关于最后一个单词的长度,所以开头有多少个空格起始并不需要在意,从字符串末尾开始,先将末尾的空格都去掉,然后开始找非空格的字符的长度即可,参见代码如下:

解法三:

class Solution {
public:
    int lengthOfLastWord(string s) {
        int right = s.size() - 1, res = 0;
        while (right >= 0 && s[right] == ' ') --right;
        while (right >= 0 && s[right] != ' ' ) {
            --right; 
            ++res;
        }
        return res;
    }
};

这道题用Java来做可以一行搞定,请参见这个帖子.

Github 同步地址:

#58

参考资料:

https://leetcode.com/problems/length-of-last-word/

https://leetcode.com/problems/length-of-last-word/discuss/21927/My-3-line-0-ms-java-solution

https://leetcode.com/problems/length-of-last-word/discuss/21892/7-lines-4ms-C%2B%2B-Solution

LeetCode All in One 题目讲解汇总(持续更新中...)

(欢迎加入博主的知识星球,博主将及时答疑解惑,并分享刷题经验与总结,快快加入吧~)

知识星球 喜欢请点赞,疼爱请打赏❤️~.~

微信打赏

|

Venmo 打赏


---|---

@0liu
Copy link

0liu commented Aug 15, 2020

class Solution {
public:
    int lengthOfLastWord(string s) {
        auto it = s.rbegin();
        for (; it != s.rend() && *it == ' '; ++it);
        auto end = it;
        for (; it != s.rend() && *it != ' '; ++it);
        return it - end;
    }
};

@0liu
Copy link

0liu commented Aug 15, 2020

class Solution {
public:
    int lengthOfLastWord(string s) {
        string word;
        for (stringstream ss {s}; std::getline(ss, word, ' '); ss >> std::ws);
        return word.length();
    }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants