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

10 11 12 13 14
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
F
fix bug  
feilong 已提交
15

16 17 18 19 20 21 22 23 24 25 26 27 28 29
```cpp
int main()
{
    Solution sol;
    string s = "Hello World";
    int res;

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

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

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
```cpp
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 已提交
53

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

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
```cpp
class Solution
{
public:
    int lengthOfLastWord(string s)
    {
        string word;
        stringstream ss(s);
        while (ss >> word)
        {
        }
        return word.size();
    }
};
```

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

74 75 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
```cpp
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 已提交
101

102 103 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
```cpp
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();
    }
};
```