File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed
Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -23,9 +23,11 @@ LeetCode 最强题解(持续更新中):
2323| [ 0219.存在重复元素II] ( https://github.com/youngyangyang04/leetcode/blob/master/problems/0219.存在重复元素II.md ) | 哈希表 | 简单| ** 哈希** |
2424| [ 0237.删除链表中的节点] ( https://github.com/youngyangyang04/leetcode/blob/master/problems/0237.删除链表中的节点.md ) | 链表 | 简单| ** 原链表移除** ** 添加虚拟节点** 递归|
2525| [ 0242.有效的字母异位词] ( https://github.com/youngyangyang04/leetcode/blob/master/problems/0242.有效的字母异位词.md ) | 哈希表 | 简单| ** 哈希** |
26+ | [ 0344.反转字符串] ( https://github.com/youngyangyang04/leetcode/blob/master/problems/0344.反转字符串.md ) | 字符串 | 简单| ** 双指针** |
2627| [ 0349.两个数组的交集] ( https://github.com/youngyangyang04/leetcode/blob/master/problems/0349.两个数组的交集.md ) | 哈希表 | 简单| ** 哈希** |
2728| [ 0350.两个数组的交集II] ( https://github.com/youngyangyang04/leetcode/blob/master/problems/0350.两个数组的交集II.md ) | 哈希表 | 简单| ** 哈希** |
2829| [ 0383.赎金信] ( https://github.com/youngyangyang04/leetcode/blob/master/problems/0383.赎金信.md ) | 数组 | 简单| ** 暴力** ** 字典计数** ** 哈希** |
30+ | [ 0434.字符串中的单词数] ( https://github.com/youngyangyang04/leetcode/blob/master/problems/0434.字符串中的单词数.md ) | 字符串 | 简单| ** 模拟** |
2931| [ 0454.四数相加II] ( https://github.com/youngyangyang04/leetcode/blob/master/problems/0454.四数相加II.md ) | 哈希表 | 中等| ** 哈希** |
3032| [ 0575.分糖果.md] ( https://github.com/youngyangyang04/leetcode/blob/master/problems/0575.分糖果.md ) | 哈希表 | 简单| ** 哈希** |
3133| [ 0705.设计哈希集合.md] ( https://github.com/youngyangyang04/leetcode/blob/master/problems/0705.设计哈希集合.md ) | 哈希表 | 简单| ** 模拟** |
Original file line number Diff line number Diff line change 1+ ## 题目地址
2+ https://leetcode-cn.com/problems/number-of-segments-in-a-string/
3+
4+ ## 思路
5+
6+ 可以把问题抽象为 遇到s[ i] 是空格,s[ i+1] 不是空格,就统计一个单词, 然后特别处理一下第一个字符不是空格的情况
7+
8+ ## C++代码
9+
10+ ```
11+ class Solution{
12+ public:
13+ int countSegments(string s){
14+ int count = 0;
15+ for (int i = 0; i < s.size(); i++){
16+ // 第一个字符不是空格的情况
17+ if (i == 0 && s[i] != ' '){
18+ count++;
19+ }
20+ // 只要s[i]是空格,s[i+1]不是空格,count就加1
21+ if (i + 1 < s.size() && s[i] == ' ' && s[i + 1] != ' '){
22+ count++;
23+ }
24+ }
25+ return count;
26+ }
27+ };
28+ ```
You can’t perform that action at this time.
0 commit comments