diff --git a/README.md b/README.md index 0fc650a9..e5b5c53e 100644 --- a/README.md +++ b/README.md @@ -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) |哈希表 |简单|**模拟**| diff --git a/problems/0434.字符串中的单词数.md b/problems/0434.字符串中的单词数.md new file mode 100644 index 00000000..bd4fd4f2 --- /dev/null +++ b/problems/0434.字符串中的单词数.md @@ -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; + } +}; +```