Skip to content

Commit a94e080

Browse files
committed
一刷434
1 parent ab3ad42 commit a94e080

File tree

5 files changed

+73
-18
lines changed

5 files changed

+73
-18
lines changed

README.adoc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3064,13 +3064,13 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
30643064
|Medium
30653065
|
30663066

3067-
//|{counter:codes}
3068-
//|{leetcode_base_url}/number-of-segments-in-a-string/[434. Number of Segments in a String^]
3069-
//|{source_base_url}/_0434_NumberOfSegmentsInAString.java[Java]
3070-
//|{doc_base_url}/0434-number-of-segments-in-a-string.adoc[题解]
3071-
//|Easy
3072-
//|
3073-
//
3067+
|{counter:codes}
3068+
|{leetcode_base_url}/number-of-segments-in-a-string/[434. Number of Segments in a String^]
3069+
|{source_base_url}/_0434_NumberOfSegmentsInAString.java[Java]
3070+
|{doc_base_url}/0434-number-of-segments-in-a-string.adoc[题解]
3071+
|Easy
3072+
|
3073+
30743074
//|{counter:codes}
30753075
//|{leetcode_base_url}/non-overlapping-intervals/[435. Non-overlapping Intervals^]
30763076
//|{source_base_url}/_0435_NonOverlappingIntervals.java[Java]
Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,49 @@
11
[#0434-number-of-segments-in-a-string]
2-
= 434. Number of Segments in a String
2+
= 434. 字符串中的单词数
33

4-
{leetcode}/problems/number-of-segments-in-a-string/[LeetCode - Number of Segments in a String^]
4+
https://leetcode.cn/problems/number-of-segments-in-a-string/[LeetCode - 434. 字符串中的单词数 ^]
55

6-
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.
6+
统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。
77

8-
Please note that the string does not contain any *non-printable* characters.
8+
请注意,你可以假定字符串里不包括任何不可打印的字符。
9+
10+
*示例:*
11+
12+
....
13+
输入: "Hello, my name is John"
14+
输出: 5
15+
解释: 这里的单词是指连续的不是空格的字符,所以 "Hello," 算作 1 个单词。
16+
....
917

10-
*Example:*
11-
[subs="verbatim,quotes,macros"]
12-
----
13-
*Input:* "Hello, my name is John"
14-
*Output:* 5
15-
----
1618

19+
== 思路分析
1720

21+
识别出单词开始的字符,进行统计数量,其余向后走。
1822

1923
[[src-0434]]
24+
[tabs]
25+
====
26+
一刷::
27+
+
28+
--
2029
[{java_src_attr}]
2130
----
2231
include::{sourcedir}/_0434_NumberOfSegmentsInAString.java[tag=answer]
2332
----
33+
--
34+
35+
// 二刷::
36+
// +
37+
// --
38+
// [{java_src_attr}]
39+
// ----
40+
// include::{sourcedir}/_0434_NumberOfSegmentsInAString_2.java[tag=answer]
41+
// ----
42+
// --
43+
====
44+
45+
46+
== 参考资料
2447

48+
. https://leetcode.cn/problems/number-of-segments-in-a-string/solutions/1034164/gong-shui-san-xie-jian-dan-zi-fu-mo-ni-t-0gx6/[434. 字符串中的单词数 - 简单字符模拟题^]
49+
. https://leetcode.cn/problems/number-of-segments-in-a-string/solutions/1033996/zi-fu-chuan-zhong-de-dan-ci-shu-by-leetc-igfb/[434. 字符串中的单词数 - 官方题解^]

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ include::0429-n-ary-tree-level-order-traversal.adoc[leveloffset=+1]
953953

954954
include::0433-minimum-genetic-mutation.adoc[leveloffset=+1]
955955

956-
// include::0434-number-of-segments-in-a-string.adoc[leveloffset=+1]
956+
include::0434-number-of-segments-in-a-string.adoc[leveloffset=+1]
957957

958958
// include::0435-non-overlapping-intervals.adoc[leveloffset=+1]
959959

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,11 @@ endif::[]
13141314
|{doc_base_url}/0423-reconstruct-original-digits-from-english.adoc[题解]
13151315
|⭕️ 按照最初想法,先统计每个字符出现的次数,然后再按照数字顺序,逐个去尝试是否在字符串中。这样时间复杂度就不可控。看题解,可以按照单词中的字符出现特性,先将只出现在一个数字中的字符挑选出来,确定对应的数字出现次数。然后,在处理出现中两个数字中的字符,逐步把所有数字出现次数确定好。
13161316

1317+
|{counter:codes2503}
1318+
|{leetcode_base_url}/number-of-segments-in-a-string/[434. 字符串中的单词数^]
1319+
|{doc_base_url}/0434-number-of-segments-in-a-string.adoc[题解]
1320+
|✅ 识别单词开头,进行数量统计。
1321+
13171322

13181323
|===
13191324

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0434_NumberOfSegmentsInAString {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2024-08-29 20:49:07
8+
*/
9+
public int countSegments(String s) {
10+
int result = 0;
11+
boolean flag = false;
12+
for (int i = 0; i < s.length(); i++) {
13+
if (s.charAt(i) == ' ') {
14+
flag = false;
15+
continue;
16+
}
17+
if (!flag) {
18+
result++;
19+
flag = true;
20+
}
21+
}
22+
return result;
23+
}
24+
// end::answer[]
25+
}

0 commit comments

Comments
 (0)