Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 41e37fb

Browse files
✨feat: Add 434
1 parent b404f78 commit 41e37fb

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
| [405. 数字转换为十六进制数](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/solution/gong-shui-san-xie-yi-ti-shuang-jie-jin-z-d93o/) | 简单 | 🤩🤩🤩🤩 |
3030
| [413. 等差数列划分](https://leetcode-cn.com/problems/arithmetic-slices/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/arithmetic-slices/solution/gong-shui-san-xie-shuang-zhi-zhen-qiu-ji-ef1q/) | 中等 | 🤩🤩🤩🤩 |
3131
| [414. 第三大的数](https://leetcode-cn.com/problems/third-maximum-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/third-maximum-number/solution/gong-shui-san-xie-yi-ti-shuang-jie-pai-x-pmln/) | 中等 | 🤩🤩🤩🤩 |
32+
| [434. 字符串中的单词数](https://leetcode-cn.com/problems/number-of-segments-in-a-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-segments-in-a-string/solution/gong-shui-san-xie-jian-dan-zi-fu-mo-ni-t-0gx6/) | 简单 | 🤩🤩🤩🤩 |
3233
| [443. 压缩字符串](https://leetcode-cn.com/problems/string-compression/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/string-compression/solution/gong-shui-san-xie-shuang-zhi-zhen-yuan-d-bppu/) | 中等 | 🤩🤩🤩🤩 |
3334
| [451. 根据字符出现频率排序](https://leetcode-cn.com/problems/sort-characters-by-frequency/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sort-characters-by-frequency/solution/gong-shui-san-xie-shu-ju-jie-gou-yun-yon-gst9/) | 中等 | 🤩🤩🤩🤩 |
3435
| [457. 环形数组是否存在循环](https://leetcode-cn.com/problems/circular-array-loop/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/circular-array-loop/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-ag05/) | 中等 | 🤩🤩🤩🤩 |
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[434. 字符串中的单词数](https://leetcode-cn.com/problems/number-of-segments-in-a-string/solution/gong-shui-san-xie-jian-dan-zi-fu-mo-ni-t-0gx6/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。
8+
9+
请注意,你可以假定字符串里不包括任何不可打印的字符。
10+
11+
示例:
12+
```
13+
输入: "Hello, my name is John"
14+
15+
输出: 5
16+
17+
解释: 这里的单词是指连续的不是空格的字符,所以 "Hello," 算作 1 个单词。
18+
```
19+
20+
---
21+
22+
### 模拟
23+
24+
题目对于「单词」的定义为「连续的不是空格的字符」。
25+
26+
因此,我们可以从前往后处理字符串 `s` 并进行计数,对于是空格的字符进行跳过(不计数),而对于非空格字符,则在遍历完一个完整单词(连续一段)后进行一次计数。
27+
28+
代码:
29+
```Java
30+
class Solution {
31+
public int countSegments(String s) {
32+
int n = s.length();
33+
int ans = 0;
34+
for (int i = 0; i < n; ) {
35+
if (s.charAt(i) == ' ' && i++ >= 0) continue;
36+
while (i < n && s.charAt(i) != ' ') i++;
37+
ans++;
38+
}
39+
return ans;
40+
}
41+
}
42+
```
43+
* 时间复杂度:$O(n)$
44+
* 空间复杂度:$O(1)$
45+
46+
---
47+
48+
### 最后
49+
50+
这是我们「刷穿 LeetCode」系列文章的第 `No.434` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
51+
52+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
53+
54+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode。
55+
56+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
57+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /