File tree Expand file tree Collapse file tree 5 files changed +73
-18
lines changed
src/main/java/com/diguage/algo/leetcode Expand file tree Collapse file tree 5 files changed +73
-18
lines changed Original file line number Diff line number Diff line change @@ -3064,13 +3064,13 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
3064
3064
|Medium
3065
3065
|
3066
3066
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
+
3074
3074
//|{counter:codes}
3075
3075
//|{leetcode_base_url}/non-overlapping-intervals/[435. Non-overlapping Intervals^]
3076
3076
//|{source_base_url}/_0435_NonOverlappingIntervals.java[Java]
Original file line number Diff line number Diff line change 1
1
[#0434-number-of-segments-in-a-string]
2
- = 434. Number of Segments in a String
2
+ = 434. 字符串中的单词数
3
3
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. 字符串中的单词数 ^]
5
5
6
- Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.
6
+ 统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。
7
7
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
+ ....
9
17
10
- *Example:*
11
- [subs="verbatim,quotes,macros"]
12
- ----
13
- *Input:* "Hello, my name is John"
14
- *Output:* 5
15
- ----
16
18
19
+ == 思路分析
17
20
21
+ 识别出单词开始的字符,进行统计数量,其余向后走。
18
22
19
23
[[src-0434]]
24
+ [tabs]
25
+ ====
26
+ 一刷::
27
+ +
28
+ --
20
29
[{java_src_attr}]
21
30
----
22
31
include::{sourcedir}/_0434_NumberOfSegmentsInAString.java[tag=answer]
23
32
----
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
+ == 参考资料
24
47
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. 字符串中的单词数 - 官方题解^]
Original file line number Diff line number Diff line change @@ -953,7 +953,7 @@ include::0429-n-ary-tree-level-order-traversal.adoc[leveloffset=+1]
953
953
954
954
include::0433-minimum-genetic-mutation.adoc[leveloffset=+1]
955
955
956
- // include::0434-number-of-segments-in-a-string.adoc[leveloffset=+1]
956
+ include::0434-number-of-segments-in-a-string.adoc[leveloffset=+1]
957
957
958
958
// include::0435-non-overlapping-intervals.adoc[leveloffset=+1]
959
959
Original file line number Diff line number Diff line change @@ -1314,6 +1314,11 @@ endif::[]
1314
1314
|{doc_base_url} /0423-reconstruct-original-digits-from-english.adoc[题解]
1315
1315
|⭕️ 按照最初想法,先统计每个字符出现的次数,然后再按照数字顺序,逐个去尝试是否在字符串中。这样时间复杂度就不可控。看题解,可以按照单词中的字符出现特性,先将只出现在一个数字中的字符挑选出来,确定对应的数字出现次数。然后,在处理出现中两个数字中的字符,逐步把所有数字出现次数确定好。
1316
1316
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
+
1317
1322
1318
1323
|===
1319
1324
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments