Skip to content

Commit c2a2117

Browse files
committed
feat: add solutions to lc problem: No.0434
No.0434.Number of Segments in a String
1 parent ca918eb commit c2a2117

File tree

7 files changed

+114
-51
lines changed

7 files changed

+114
-51
lines changed

solution/0400-0499/0434.Number of Segments in a String/README.md

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@
2121

2222
<!-- 这里可写通用的实现逻辑 -->
2323

24-
split 切分字符串,或者直接遍历计数。
24+
**方法一:字符串分割**
25+
26+
将字符串 `s` 按照空格进行分割,然后统计不为空的单词个数。
27+
28+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。
29+
30+
**方法二:模拟**
31+
32+
直接模拟,遍历字符串,检测每个字符,统计个数。
33+
34+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
2535

2636
<!-- tabs:start -->
2737

@@ -38,11 +48,11 @@ class Solution:
3848
```python
3949
class Solution:
4050
def countSegments(self, s: str) -> int:
41-
res = 0
42-
for i in range(len(s)):
43-
if s[i] != ' ' and (i == 0 or s[i - 1] == ' '):
44-
res += 1
45-
return res
51+
ans = 0
52+
for i, c in enumerate(s):
53+
if c != ' ' and (i == 0 or s[i - 1] == ' '):
54+
ans += 1
55+
return ans
4656
```
4757

4858
### **Java**
@@ -52,27 +62,27 @@ class Solution:
5262
```java
5363
class Solution {
5464
public int countSegments(String s) {
55-
int res = 0;
65+
int ans = 0;
5666
for (String t : s.split(" ")) {
5767
if (!"".equals(t)) {
58-
++res;
68+
++ans;
5969
}
6070
}
61-
return res;
71+
return ans;
6272
}
6373
}
6474
```
6575

6676
```java
6777
class Solution {
6878
public int countSegments(String s) {
69-
int res = 0;
79+
int ans = 0;
7080
for (int i = 0; i < s.length(); ++i) {
7181
if (s.charAt(i) != ' ' && (i == 0 || s.charAt(i - 1) == ' ')) {
72-
++res;
82+
++ans;
7383
}
7484
}
75-
return res;
85+
return ans;
7686
}
7787
}
7888
```
@@ -83,12 +93,25 @@ class Solution {
8393
class Solution {
8494
public:
8595
int countSegments(string s) {
86-
int res = 0;
96+
int ans = 0;
97+
istringstream ss(s);
98+
while (ss >> s) ++ans;
99+
return ans;
100+
}
101+
};
102+
```
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
int countSegments(string s) {
108+
int ans = 0;
87109
for (int i = 0; i < s.size(); ++i) {
88-
if (s[i] != ' ' && (i == 0 || s[i - 1] == ' '))
89-
++res;
110+
if (s[i] != ' ' && (i == 0 || s[i - 1] == ' ')) {
111+
++ans;
112+
}
90113
}
91-
return res;
114+
return ans;
92115
}
93116
};
94117
```
@@ -97,13 +120,25 @@ public:
97120

98121
```go
99122
func countSegments(s string) int {
100-
res := 0
123+
ans := 0
124+
for _, t := range strings.Split(s, " ") {
125+
if len(t) > 0 {
126+
ans++
127+
}
128+
}
129+
return ans
130+
}
131+
```
132+
133+
```go
134+
func countSegments(s string) int {
135+
ans := 0
101136
for i, c := range s {
102137
if c != ' ' && (i == 0 || s[i-1] == ' ') {
103-
res++
138+
ans++
104139
}
105140
}
106-
return res
141+
return ans
107142
}
108143
```
109144

solution/0400-0499/0434.Number of Segments in a String/README_EN.md

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ class Solution:
4848
```python
4949
class Solution:
5050
def countSegments(self, s: str) -> int:
51-
res = 0
52-
for i in range(len(s)):
53-
if s[i] != ' ' and (i == 0 or s[i - 1] == ' '):
54-
res += 1
55-
return res
51+
ans = 0
52+
for i, c in enumerate(s):
53+
if c != ' ' and (i == 0 or s[i - 1] == ' '):
54+
ans += 1
55+
return ans
5656
```
5757

5858
### **Java**
@@ -74,13 +74,13 @@ class Solution {
7474
```java
7575
class Solution {
7676
public int countSegments(String s) {
77-
int res = 0;
78-
for (int i = 0; i < s.length(); ++i) {
79-
if (s.charAt(i) != ' ' && (i == 0 || s.charAt(i - 1) == ' ')) {
80-
++res;
77+
int ans = 0;
78+
for (String t : s.split(" ")) {
79+
if (!"".equals(t)) {
80+
++ans;
8181
}
8282
}
83-
return res;
83+
return ans;
8484
}
8585
}
8686
```
@@ -91,12 +91,25 @@ class Solution {
9191
class Solution {
9292
public:
9393
int countSegments(string s) {
94-
int res = 0;
94+
int ans = 0;
95+
istringstream ss(s);
96+
while (ss >> s) ++ans;
97+
return ans;
98+
}
99+
};
100+
```
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
int countSegments(string s) {
106+
int ans = 0;
95107
for (int i = 0; i < s.size(); ++i) {
96-
if (s[i] != ' ' && (i == 0 || s[i - 1] == ' '))
97-
++res;
108+
if (s[i] != ' ' && (i == 0 || s[i - 1] == ' ')) {
109+
++ans;
110+
}
98111
}
99-
return res;
112+
return ans;
100113
}
101114
};
102115
```
@@ -105,13 +118,25 @@ public:
105118

106119
```go
107120
func countSegments(s string) int {
108-
res := 0
121+
ans := 0
122+
for _, t := range strings.Split(s, " ") {
123+
if len(t) > 0 {
124+
ans++
125+
}
126+
}
127+
return ans
128+
}
129+
```
130+
131+
```go
132+
func countSegments(s string) int {
133+
ans := 0
109134
for i, c := range s {
110135
if c != ' ' && (i == 0 || s[i-1] == ' ') {
111-
res++
136+
ans++
112137
}
113138
}
114-
return res
139+
return ans
115140
}
116141
```
117142

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
class Solution {
22
public:
33
int countSegments(string s) {
4-
int res = 0;
4+
int ans = 0;
55
for (int i = 0; i < s.size(); ++i) {
6-
if (s[i] != ' ' && (i == 0 || s[i - 1] == ' '))
7-
++res;
6+
if (s[i] != ' ' && (i == 0 || s[i - 1] == ' ')) {
7+
++ans;
8+
}
89
}
9-
return res;
10+
return ans;
1011
}
1112
};
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
func countSegments(s string) int {
2-
res := 0
2+
ans := 0
33
for i, c := range s {
44
if c != ' ' && (i == 0 || s[i-1] == ' ') {
5-
res++
5+
ans++
66
}
77
}
8-
return res
8+
return ans
99
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution {
22
public int countSegments(String s) {
3-
int res = 0;
3+
int ans = 0;
44
for (int i = 0; i < s.length(); ++i) {
55
if (s.charAt(i) != ' ' && (i == 0 || s.charAt(i - 1) == ' ')) {
6-
++res;
6+
++ans;
77
}
88
}
9-
return res;
9+
return ans;
1010
}
1111
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
def countSegments(self, s: str) -> int:
3-
res = 0
4-
for i in range(len(s)):
5-
if s[i] != ' ' and (i == 0 or s[i - 1] == ' '):
6-
res += 1
7-
return res
3+
ans = 0
4+
for i, c in enumerate(s):
5+
if c != ' ' and (i == 0 or s[i - 1] == ' '):
6+
ans += 1
7+
return ans

solution/2000-2099/2046.Sort Linked List Already Sorted Using Absolute Values/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
**方法一:头插法**
60+
5961
先默认第一个点已经排序完毕。然后从第二个点开始,遇到值为负数的节点,采用头插法;非负数,则继续往下遍历即可。
6062

6163
<!-- tabs:start -->

0 commit comments

Comments
 (0)