This commit is contained in:
youngyangyang04
2020-07-08 09:20:01 +08:00
parent 9903e8f26d
commit 67e76055d9
2 changed files with 30 additions and 0 deletions

View File

@ -23,9 +23,11 @@ LeetCode 最强题解(持续更新中):
|[0219.存在重复元素II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0219.存在重复元素II.md) | 哈希表 |简单| **哈希** |
|[0237.删除链表中的节点](https://github.com/youngyangyang04/leetcode/blob/master/problems/0237.删除链表中的节点.md) |链表 |简单| **原链表移除** **添加虚拟节点** 递归|
|[0242.有效的字母异位词](https://github.com/youngyangyang04/leetcode/blob/master/problems/0242.有效的字母异位词.md) |哈希表 |简单| **哈希**|
|[0344.反转字符串](https://github.com/youngyangyang04/leetcode/blob/master/problems/0344.反转字符串.md) |字符串 |简单| **双指针**|
|[0349.两个数组的交集](https://github.com/youngyangyang04/leetcode/blob/master/problems/0349.两个数组的交集.md) |哈希表 |简单|**哈希**|
|[0350.两个数组的交集II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0350.两个数组的交集II.md) |哈希表 |简单|**哈希**|
|[0383.赎金信](https://github.com/youngyangyang04/leetcode/blob/master/problems/0383.赎金信.md) |数组 |简单|**暴力** **字典计数** **哈希**|
|[0434.字符串中的单词数](https://github.com/youngyangyang04/leetcode/blob/master/problems/0434.字符串中的单词数.md) |字符串 |简单|**模拟**|
|[0454.四数相加II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0454.四数相加II.md) |哈希表 |中等| **哈希**|
|[0575.分糖果.md](https://github.com/youngyangyang04/leetcode/blob/master/problems/0575.分糖果.md) |哈希表 |简单|**哈希**|
|[0705.设计哈希集合.md](https://github.com/youngyangyang04/leetcode/blob/master/problems/0705.设计哈希集合.md) |哈希表 |简单|**模拟**|

View File

@ -0,0 +1,28 @@
## 题目地址
https://leetcode-cn.com/problems/number-of-segments-in-a-string/
## 思路
可以把问题抽象为 遇到s[i]是空格s[i+1]不是空格,就统计一个单词, 然后特别处理一下第一个字符不是空格的情况
## C++代码
```
class Solution {
public:
int countSegments(string s) {
int count = 0;
for (int i = 0; i < s.size(); i++) {
// 第一个字符不是空格的情况
if (i == 0 && s[i] != ' ') {
count++;
}
// 只要s[i]是空格s[i+1]不是空格count就加1
if (i + 1 < s.size() && s[i] == ' ' && s[i + 1] != ' ') {
count++;
}
}
return count;
}
};
```