Skip to content

Commit 3a9a779

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

File tree

6 files changed

+170
-10
lines changed

6 files changed

+170
-10
lines changed

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

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,95 @@
1717
<strong>解释: </strong>这里的单词是指连续的不是空格的字符,所以 &quot;Hello,&quot; 算作 1 个单词。
1818
</pre>
1919

20-
2120
## 解法
2221

2322
<!-- 这里可写通用的实现逻辑 -->
2423

24+
split 切分字符串,或者直接遍历计数。
25+
2526
<!-- tabs:start -->
2627

2728
### **Python3**
2829

2930
<!-- 这里可写当前语言的特殊实现逻辑 -->
3031

3132
```python
33+
class Solution:
34+
def countSegments(self, s: str) -> int:
35+
return sum(1 for t in s.split(' ') if t)
36+
```
3237

38+
```python
39+
class Solution:
40+
def countSegments(self, s: str) -> int:
41+
res, n = 0, len(s)
42+
for i in range(n):
43+
if s[i] != ' ' and (i == 0 or s[i - 1] == ' '):
44+
res += 1
45+
return res
3346
```
3447

3548
### **Java**
3649

3750
<!-- 这里可写当前语言的特殊实现逻辑 -->
3851

3952
```java
53+
class Solution {
54+
public int countSegments(String s) {
55+
int res = 0;
56+
for (String t : s.split(" ")) {
57+
if (!"".equals(t)) {
58+
++res;
59+
}
60+
}
61+
return res;
62+
}
63+
}
64+
```
65+
66+
```java
67+
class Solution {
68+
public int countSegments(String s) {
69+
int res = 0;
70+
for (int i = 0; i < s.length(); ++i) {
71+
if (s.charAt(i) != ' ' && (i == 0 || s.charAt(i - 1) == ' ')) {
72+
++res;
73+
}
74+
}
75+
return res;
76+
}
77+
}
78+
```
79+
80+
### **C++**
81+
82+
```cpp
83+
class Solution {
84+
public:
85+
int countSegments(string s) {
86+
int res = 0;
87+
for (int i = 0; i < s.size(); ++i)
88+
{
89+
if (s[i] != ' ' && (i == 0 || s[i - 1] == ' '))
90+
++res;
91+
}
92+
return res;
93+
}
94+
};
95+
```
4096
97+
### **Go**
98+
99+
```go
100+
func countSegments(s string) int {
101+
res := 0
102+
for i, c := range s {
103+
if c != ' ' && (i == 0 || s[i-1] == ' ') {
104+
res++
105+
}
106+
}
107+
return res
108+
}
41109
```
42110

43111
### **...**

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

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,87 @@
4747
<li>The only space character in <code>s</code> is <code>&#39; &#39;</code>.</li>
4848
</ul>
4949

50-
5150
## Solutions
5251

5352
<!-- tabs:start -->
5453

5554
### **Python3**
5655

5756
```python
57+
class Solution:
58+
def countSegments(self, s: str) -> int:
59+
return sum(1 for t in s.split(' ') if t)
60+
```
5861

62+
```python
63+
class Solution:
64+
def countSegments(self, s: str) -> int:
65+
res, n = 0, len(s)
66+
for i in range(n):
67+
if s[i] != ' ' and (i == 0 or s[i - 1] == ' '):
68+
res += 1
69+
return res
5970
```
6071

6172
### **Java**
6273

6374
```java
75+
class Solution {
76+
public int countSegments(String s) {
77+
int res = 0;
78+
for (String t : s.split(" ")) {
79+
if (!"".equals(t)) {
80+
++res;
81+
}
82+
}
83+
return res;
84+
}
85+
}
86+
```
87+
88+
```java
89+
class Solution {
90+
public int countSegments(String s) {
91+
int res = 0;
92+
for (int i = 0; i < s.length(); ++i) {
93+
if (s.charAt(i) != ' ' && (i == 0 || s.charAt(i - 1) == ' ')) {
94+
++res;
95+
}
96+
}
97+
return res;
98+
}
99+
}
100+
```
101+
102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
int countSegments(string s) {
108+
int res = 0;
109+
for (int i = 0; i < s.size(); ++i)
110+
{
111+
if (s[i] != ' ' && (i == 0 || s[i - 1] == ' '))
112+
++res;
113+
}
114+
return res;
115+
}
116+
};
117+
```
64118
119+
### **Go**
120+
121+
```go
122+
func countSegments(s string) int {
123+
res := 0
124+
for i, c := range s {
125+
if c != ' ' && (i == 0 || s[i-1] == ' ') {
126+
res++
127+
}
128+
}
129+
return res
130+
}
65131
```
66132

67133
### **...**
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
class Solution {
22
public:
33
int countSegments(string s) {
4-
if (s.length() < 1)
5-
return 0 ;
6-
7-
int cnt = isspace(s[0])? 0: 1 ;
8-
for (int i = 1; i < s.length(); ++i)
9-
if (!isspace(s[i]) && isspace(s[i-1]))
10-
++cnt ;
11-
return cnt ;
4+
int res = 0;
5+
for (int i = 0; i < s.size(); ++i)
6+
{
7+
if (s[i] != ' ' && (i == 0 || s[i - 1] == ' '))
8+
++res;
9+
}
10+
return res;
1211
}
1312
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func countSegments(s string) int {
2+
res := 0
3+
for i, c := range s {
4+
if c != ' ' && (i == 0 || s[i-1] == ' ') {
5+
res++
6+
}
7+
}
8+
return res
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int countSegments(String s) {
3+
int res = 0;
4+
for (int i = 0; i < s.length(); ++i) {
5+
if (s.charAt(i) != ' ' && (i == 0 || s.charAt(i - 1) == ' ')) {
6+
++res;
7+
}
8+
}
9+
return res;
10+
}
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def countSegments(self, s: str) -> int:
3+
res, n = 0, len(s)
4+
for i in range(n):
5+
if s[i] != ' ' and (i == 0 or s[i - 1] == ' '):
6+
res += 1
7+
return res

0 commit comments

Comments
 (0)