File tree Expand file tree Collapse file tree 6 files changed +170
-10
lines changed
solution/0400-0499/0434.Number of Segments in a String Expand file tree Collapse file tree 6 files changed +170
-10
lines changed Original file line number Diff line number Diff line change 17
17
<strong >解释: </strong >这里的单词是指连续的不是空格的字符,所以 " ; Hello," ; 算作 1 个单词。
18
18
</pre >
19
19
20
-
21
20
## 解法
22
21
23
22
<!-- 这里可写通用的实现逻辑 -->
24
23
24
+ split 切分字符串,或者直接遍历计数。
25
+
25
26
<!-- tabs:start -->
26
27
27
28
### ** Python3**
28
29
29
30
<!-- 这里可写当前语言的特殊实现逻辑 -->
30
31
31
32
``` python
33
+ class Solution :
34
+ def countSegments (self , s : str ) -> int :
35
+ return sum (1 for t in s.split(' ' ) if t)
36
+ ```
32
37
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
33
46
```
34
47
35
48
### ** Java**
36
49
37
50
<!-- 这里可写当前语言的特殊实现逻辑 -->
38
51
39
52
``` 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
+ ```
40
96
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
+ }
41
109
```
42
110
43
111
### ** ...**
Original file line number Diff line number Diff line change 47
47
<li>The only space character in <code>s</code> is <code>' '</code>.</li>
48
48
</ul >
49
49
50
-
51
50
## Solutions
52
51
53
52
<!-- tabs:start -->
54
53
55
54
### ** Python3**
56
55
57
56
``` python
57
+ class Solution :
58
+ def countSegments (self , s : str ) -> int :
59
+ return sum (1 for t in s.split(' ' ) if t)
60
+ ```
58
61
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
59
70
```
60
71
61
72
### ** Java**
62
73
63
74
``` 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
+ ```
64
118
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
+ }
65
131
```
66
132
67
133
### ** ...**
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
3
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;
12
11
}
13
12
};
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments