Skip to content

Commit 219c53b

Browse files
halfrostdezhiy
authored andcommitted
Update 434 solution
1 parent 16b1ab7 commit 219c53b

File tree

6 files changed

+1901
-1772
lines changed

6 files changed

+1901
-1772
lines changed

README.md

Lines changed: 1534 additions & 1507 deletions
Large diffs are not rendered by default.

leetcode/0434.Number-of-Segments-in-a-String/README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [434. Number of Segments in a String](https://leetcode-cn.com/problems/number-of-segments-in-a-string/)
1+
# [434. Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/)
22

33

44
## 题目
@@ -43,3 +43,28 @@ A segment is defined to be a contiguous sequence of non-space characters.
4343
## 解题思路
4444

4545
- 以空格为分割计算元素个数
46+
47+
## 代码
48+
49+
```go
50+
51+
package leetcode
52+
53+
func countSegments(s string) int {
54+
segments := false
55+
cnt := 0
56+
for _, v := range s {
57+
if v == ' ' && segments {
58+
segments = false
59+
cnt += 1
60+
} else if v != ' ' {
61+
segments = true
62+
}
63+
}
64+
if segments {
65+
cnt++
66+
}
67+
return cnt
68+
}
69+
70+
```

website/content/ChapterFour/0400~0499/0433.Minimum-Genetic-Mutation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,5 +186,5 @@ func convert(gene string) uint32 {
186186
----------------------------------------------
187187
<div style="display: flex;justify-content: space-between;align-items: center;">
188188
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0429.N-ary-Tree-Level-Order-Traversal/">⬅️上一页</a></p>
189-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0435.Non-overlapping-Intervals/">下一页➡️</a></p>
189+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0434.Number-of-Segments-in-a-String/">下一页➡️</a></p>
190190
</div>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# [434. Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/)
2+
3+
4+
## 题目
5+
6+
You are given a string s, return the number of segments in the string.
7+
8+
A segment is defined to be a contiguous sequence of non-space characters.
9+
10+
**Example 1:**
11+
12+
Input: s = "Hello, my name is John"
13+
Output: 5
14+
Explanation: The five segments are ["Hello,", "my", "name", "is", "John"]
15+
16+
**Example 2:**
17+
18+
Input: s = "Hello"
19+
Output: 1
20+
21+
**Example 3:**
22+
23+
Input: s = "love live! mu'sic forever"
24+
Output: 4
25+
26+
**Example 4:**
27+
28+
Input: s = ""
29+
Output: 0
30+
31+
**Constraints**
32+
33+
- 0 <= s.length <= 300
34+
- s consists of lower-case and upper-case English letters, digits or one of the following characters "!@#$%^&*()_+-=',.:".
35+
- The only space character in s is ' '.
36+
37+
## 题目大意
38+
39+
统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。
40+
41+
请注意,你可以假定字符串里不包括任何不可打印的字符。
42+
43+
## 解题思路
44+
45+
- 以空格为分割计算元素个数
46+
47+
## 代码
48+
49+
```go
50+
51+
package leetcode
52+
53+
func countSegments(s string) int {
54+
segments := false
55+
cnt := 0
56+
for _, v := range s {
57+
if v == ' ' && segments {
58+
segments = false
59+
cnt += 1
60+
} else if v != ' ' {
61+
segments = true
62+
}
63+
}
64+
if segments {
65+
cnt++
66+
}
67+
return cnt
68+
}
69+
70+
```
71+
72+
73+
----------------------------------------------
74+
<div style="display: flex;justify-content: space-between;align-items: center;">
75+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0433.Minimum-Genetic-Mutation/">⬅️上一页</a></p>
76+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0435.Non-overlapping-Intervals/">下一页➡️</a></p>
77+
</div>

website/content/ChapterFour/0400~0499/0435.Non-overlapping-Intervals.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,6 @@ func eraseOverlapIntervals1(intervals [][]int) int {
137137

138138
----------------------------------------------
139139
<div style="display: flex;justify-content: space-between;align-items: center;">
140-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0433.Minimum-Genetic-Mutation/">⬅️上一页</a></p>
140+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0434.Number-of-Segments-in-a-String/">⬅️上一页</a></p>
141141
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0436.Find-Right-Interval/">下一页➡️</a></p>
142142
</div>

0 commit comments

Comments
 (0)