Skip to content

Commit 05eab3a

Browse files
refactor 434
1 parent 3e05583 commit 05eab3a

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
package com.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
public class _434 {
1315

14-
public int countSegments(String s) {
15-
if (s == null || s.isEmpty()) {
16-
return 0;
17-
}
18-
String[] segments = s.split(" ");
19-
int count = 0;
20-
for (String seg : segments) {
21-
if (seg.equals("")) {
22-
continue;
16+
public static class Solution1 {
17+
public int countSegments(String s) {
18+
if (s == null || s.isEmpty()) {
19+
return 0;
20+
}
21+
String[] segments = s.split(" ");
22+
int count = 0;
23+
for (String seg : segments) {
24+
if (seg.equals("")) {
25+
continue;
26+
}
27+
count++;
2328
}
24-
count++;
29+
return count;
2530
}
26-
return count;
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
import static junit.framework.Assert.assertEquals;
99

1010
public class _434Test {
11-
private static _434 test;
12-
private static int expected;
13-
private static int actual;
14-
private static String s;
11+
private static _434.Solution1 solution1;
12+
private static int expected;
13+
private static int actual;
14+
private static String s;
1515

16-
@BeforeClass
17-
public static void setup() {
18-
test = new _434();
19-
}
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _434.Solution1();
19+
}
2020

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

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

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

0 commit comments

Comments
 (0)