Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit05eab3a

Browse files
refactor 434
1 parent3e05583 commit05eab3a

File tree

3 files changed

+48
-44
lines changed

3 files changed

+48
-44
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ Your ideas/fixes/algorithms are more than welcome!
354354
|437|[Path Sum III](https://leetcode.com/problems/path-sum-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_437.java) | O(n^2) |O(n) | |Easy| DFS, recursion
355355
|436|[Find Right Interval](https://leetcode.com/problems/find-right-interval/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_436.java) | O(nlogn) |O(n) | |Medium| Binary Search
356356
|435|[Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_435.java) | O(nlogn) |O(1) | |Medium| Greedy
357-
|434|[Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/NumberofSegmentsinaString.java)| O(n)|O(1)||Easy|
357+
|434|[Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_434.java)| O(n)|O(1)||Easy|
358358
|432|[All O`one Data Structure](https://leetcode.com/problems/all-oone-data-structure/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_432.java)| O(1)|O(n) | |Hard| Design
359359
|429|[N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_429.java)| O(n)|O(n) | |Easy| BFS, Tree
360360
|425|[Word Squares](https://leetcode.com/problems/word-squares/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_425.java)| O(n!)|O(n) | |Hard| Trie, Backtracking, Recursion
Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
packagecom.fishercoder.solutions;
2-
/**434. Number of Segments in a String
3-
4-
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.
5-
6-
Please note that the string does not contain any non-printable characters.
2+
/**
3+
* 434. Number of Segments in a String
4+
*
5+
* Count the number of segments in a string,
6+
* where a segment is defined to be a contiguous sequence of non-space characters.
7+
*
8+
* Please note that the string does not contain any non-printable characters.
79
810
Example:
911
1012
Input: "Hello, my name is John"
1113
Output: 5*/
1214
publicclass_434 {
1315

14-
publicintcountSegments(Strings) {
15-
if (s ==null ||s.isEmpty()) {
16-
return0;
17-
}
18-
String[]segments =s.split(" ");
19-
intcount =0;
20-
for (Stringseg :segments) {
21-
if (seg.equals("")) {
22-
continue;
16+
publicstaticclassSolution1 {
17+
publicintcountSegments(Strings) {
18+
if (s ==null ||s.isEmpty()) {
19+
return0;
20+
}
21+
String[]segments =s.split(" ");
22+
intcount =0;
23+
for (Stringseg :segments) {
24+
if (seg.equals("")) {
25+
continue;
26+
}
27+
count++;
2328
}
24-
count++;
29+
returncount;
2530
}
26-
returncount;
2731
}
2832
}

‎src/test/java/com/fishercoder/_434Test.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,35 @@
88
importstaticjunit.framework.Assert.assertEquals;
99

1010
publicclass_434Test {
11-
privatestatic_434test;
12-
privatestaticintexpected;
13-
privatestaticintactual;
14-
privatestaticStrings;
11+
privatestatic_434.Solution1solution1;
12+
privatestaticintexpected;
13+
privatestaticintactual;
14+
privatestaticStrings;
1515

16-
@BeforeClass
17-
publicstaticvoidsetup() {
18-
test=new_434();
19-
}
16+
@BeforeClass
17+
publicstaticvoidsetup() {
18+
solution1=new_434.Solution1();
19+
}
2020

21-
@Before
22-
publicvoidsetupForEachTest() {
23-
expected =0;
24-
actual =0;
25-
}
21+
@Before
22+
publicvoidsetupForEachTest() {
23+
expected =0;
24+
actual =0;
25+
}
2626

27-
@Test
28-
publicvoidtest1() {
29-
s ="Hello, my name is John";
30-
expected =5;
31-
actual =test.countSegments(s);
32-
assertEquals(expected,actual);
33-
}
27+
@Test
28+
publicvoidtest1() {
29+
s ="Hello, my name is John";
30+
expected =5;
31+
actual =solution1.countSegments(s);
32+
assertEquals(expected,actual);
33+
}
3434

35-
@Test
36-
publicvoidtest2() {
37-
s =", , , , a, eaefa";
38-
expected =6;
39-
actual =test.countSegments(s);
40-
assertEquals(expected,actual);
41-
}
35+
@Test
36+
publicvoidtest2() {
37+
s =", , , , a, eaefa";
38+
expected =6;
39+
actual =solution1.countSegments(s);
40+
assertEquals(expected,actual);
41+
}
4242
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp