solution.md 3.3 KB
Newer Older
1
# 最后一个单词的长度
F
fix bug  
feilong 已提交
2

3
<p>给你一个字符串 <code>s</code>,由若干单词组成,单词之间用空格隔开。返回字符串中最后一个单词的长度。如果不存在最后一个单词,请返回 0 。</p><p><strong>单词</strong> 是指仅由字母组成、不包含任何空格字符的最大子字符串。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "Hello World"<strong><br />输出:</strong>5</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = " "<strong><br />输出:</strong>0</pre><p> </p><p><strong>提示:</strong></p><ul>	<li><code>1 <= s.length <= 10<sup>4</sup></code></li>	<li><code>s</code> 仅有英文字母和空格 <code>' '</code> 组成</li></ul>
每日一练社区's avatar
每日一练社区 已提交
4
<p>以下<span style="color:red">错误</span>的选项是?</p>
F
fix bug  
feilong 已提交
5

6
## aop
F
fix bug  
feilong 已提交
7

8
### before
F
fix bug  
feilong 已提交
9

每日一练社区's avatar
每日一练社区 已提交
10
```c
11 12 13
#include <bits/stdc++.h>
using namespace std;
```
每日一练社区's avatar
每日一练社区 已提交
14

15
### after
F
fix bug  
feilong 已提交
16

每日一练社区's avatar
每日一练社区 已提交
17
```c
18 19 20 21 22 23 24 25 26 27 28 29 30
int main()
{
    Solution sol;
    string s = "Hello World";
    int res;

    res = sol.lengthOfLastWord(s);
    cout << res;
    return 0;
}
```

## 答案
F
fix bug  
feilong 已提交
31

每日一练社区's avatar
每日一练社区 已提交
32
```c
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
class Solution
{
public:
    int lengthOfLastWord(string s)
    {
        if (s.empty())
            return 0;
        int pos = s.find_last_of(' ');
        while (pos == s.length() - 1)
        {
            if (s.empty())
                return 0;
            s.erase(pos);
            pos = s.find_last_of(' ');
        }
        return s.length() - pos;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
54

55
### A
F
fix bug  
feilong 已提交
56

每日一练社区's avatar
每日一练社区 已提交
57
```c
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
class Solution
{
public:
    int lengthOfLastWord(string s)
    {
        string word;
        stringstream ss(s);
        while (ss >> word)
        {
        }
        return word.size();
    }
};
```

### B
F
fix bug  
feilong 已提交
74

每日一练社区's avatar
每日一练社区 已提交
75
```c
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
class Solution
{
public:
    int lengthOfLastWord(string s)
    {
        int countWord = 0;
        for (int index = 0; index < s.size(); index++)
        {
            if (s[index] == ' ')
            {
                if (index != s.size() - 1 && s[index + 1] != ' ')
                {
                    countWord = 0;
                    continue;
                }
                else
                    continue;
            }
            countWord++;
        }
        return countWord;
    }
};
```

### C
F
fix bug  
feilong 已提交
102

每日一练社区's avatar
每日一练社区 已提交
103
```c
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
class Solution
{
public:
    int lengthOfLastWord(string s)
    {
        if (s.empty())
            return 0;
        vector<string> word;
        int begin, lenWord = 0;
        for (int i = 0; i < s.size(); i++)
        {
            if ((i == 0 && s[i] != ' ') || (i != 0 && s[i - 1] == ' ' && s[i] != ' '))
            {
                begin = i;
                lenWord++;
                if (i == s.size() - 1)
                {
                    word.push_back(s.substr(begin, lenWord));
                    break;
                }
            }
            else if (s[i] != ' ' && i != s.size() - 1)
            {
                lenWord++;
            }
            else if (i != 0 && s[i - 1] != ' ' && s[i] == ' ')
            {
                word.push_back(s.substr(begin, lenWord));
                lenWord = 0;
            }
            else if (s[i] != ' ' && i == s.size() - 1)
            {
                word.push_back(s.substr(begin, lenWord + 1));
            }
        }
        if (word.empty())
            return 0;
        else
            return word[word.size() - 1].size();
    }
};
```