Skip to content

Commit 357fd5c

Browse files
add 722
1 parent c1a79c5 commit 357fd5c

File tree

3 files changed

+105
-0
lines changed
  • paginated_contents/algorithms/1st_thousand
  • src
    • main/java/com/fishercoder/solutions/firstthousand
    • test/java/com/fishercoder/firstthousand

3 files changed

+105
-0
lines changed

Diff for: paginated_contents/algorithms/1st_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
| 725 | [Split Linked List in Parts](https://leetcode.com/problems/split-linked-list-in-parts/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_725.java) | | Medium | LinkedList
155155
| 724 | [Find Pivot Index](https://leetcode.com/problems/find-pivot-index/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_724.java) | | Easy | Array
156156
| 723 | [Candy Crush](https://leetcode.com/problems/candy-crush/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_723.java) | | Medium | Array, Two Pointers
157+
| 722 | [Remove Comments](https://leetcode.com/problems/remove-comments/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_722.java) | | Medium | Array, String
157158
| 721 | [Accounts Merge](https://leetcode.com/problems/accounts-merge/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_721.java) | | Medium | DFS, Union Find
158159
| 720 | [Longest Word in Dictionary](https://leetcode.com/problems/longest-word-in-dictionary/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_720.java) | | Easy | Trie
159160
| 719 | [Find K-th Smallest Pair Distance](https://leetcode.com/problems/find-k-th-smallest-pair-distance/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_719.java) | | Hard | Binary Search
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.fishercoder.solutions.firstthousand;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _722 {
7+
public static class Solution1 {
8+
/**
9+
* My completely original solution.
10+
* Not a hard one, just some corner cases to consider.
11+
*/
12+
public List<String> removeComments(String[] source) {
13+
List<String> result = new ArrayList<>();
14+
StringBuilder sb = new StringBuilder();
15+
boolean possiblyMultilineComment = false;
16+
for (String line : source) {
17+
for (int i = 0; i < line.length(); i++) {
18+
if (line.charAt(i) == '/' && i + 1 < line.length()) {
19+
//adding (&& !possiblyMultilineComment) is for cases like test 6
20+
if (line.charAt(i + 1) == '*' && !possiblyMultilineComment) {
21+
//but cannot break here, because this might be a single line comment, so we need to find the closing "*/"
22+
possiblyMultilineComment = true;
23+
//increment i by one to bypass this '*'
24+
i++;
25+
} else if (line.charAt(i + 1) == '/') {
26+
//this is a single line comment, remove
27+
if (!possiblyMultilineComment) {
28+
//if this is not part of a possibly multiline comment, then we can safely break out, see test case 4
29+
break;
30+
}
31+
} else if (!possiblyMultilineComment) {
32+
sb.append(line.charAt(i));
33+
}
34+
} else if (line.charAt(i) == '*' && i + 1 < line.length() && line.charAt(i + 1) == '/' && possiblyMultilineComment) {
35+
//this is the end of the multiline comment
36+
possiblyMultilineComment = false;
37+
//increment i by one to bypass the closing '/'
38+
i++;
39+
} else if (!possiblyMultilineComment) {
40+
sb.append(line.charAt(i));
41+
}
42+
}
43+
if (sb.length() > 0 && !possiblyMultilineComment) {
44+
result.add(sb.toString());
45+
sb.setLength(0);
46+
}
47+
}
48+
return result;
49+
}
50+
}
51+
}
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.fishercoder.firstthousand;
2+
3+
import com.fishercoder.solutions.firstthousand._722;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.Arrays;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
11+
public class _722Test {
12+
private static _722.Solution1 solution1;
13+
14+
@BeforeEach
15+
public void setup() {
16+
solution1 = new _722.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals(Arrays.asList("int main()", "{ ", " ", "int a, b, c;", "a = b + c;", "}"), solution1.removeComments(new String[]{"/*Test program */", "int main()", "{ ", " // variable declaration ", "int a, b, c;", "/* This is a test", " multiline ", " comment for ", " testing */", "a = b + c;", "}"}));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
assertEquals(Arrays.asList("ab"), solution1.removeComments(new String[]{"a/*comment", "line", "more_comment*/b"}));
27+
}
28+
29+
@Test
30+
public void test3() {
31+
assertEquals(Arrays.asList("struct Node{", " ", " int size;", " int val;", "};"),
32+
solution1.removeComments(new String[]{"struct Node{", " /*/ declare members;/**/", " int size;", " /**/int val;", "};"}));
33+
}
34+
35+
@Test
36+
public void test4() {
37+
assertEquals(Arrays.asList("main() {", " double s = 33;", " cout << s;", "}"),
38+
solution1.removeComments(new String[]{"main() {", "/* here is commments", " // still comments */", " double s = 33;", " cout << s;", "}"}));
39+
}
40+
41+
@Test
42+
public void test5() {
43+
assertEquals(Arrays.asList("void func(int k) {", " k = k*2/4;", " k = k/2;*/", "}"),
44+
solution1.removeComments(new String[]{"void func(int k) {", "// this function does nothing /*", " k = k*2/4;", " k = k/2;*/", "}"}));
45+
}
46+
47+
@Test
48+
public void test6() {
49+
assertEquals(Arrays.asList("a", "blank", "df"),
50+
solution1.removeComments(new String[]{"a//*b/*/c", "blank", "d/*/e/*/f"}));
51+
}
52+
53+
}

0 commit comments

Comments
 (0)