-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
136 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
20241126: 722 | ||
20241125: 33,81 | ||
20241124: 153 | ||
20241123: 253 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
leetcode_java/src/main/java/LeetCodeJava/String/RemoveComments.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package LeetCodeJava.String; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
// https://leetcode.com/problems/remove-comments/description/ | ||
// https://leetcode.ca/all/722.html | ||
public class RemoveComments { | ||
|
||
// V0 | ||
// TODO : implement | ||
// public List<String> removeComments(String[] source) { | ||
// | ||
// } | ||
|
||
// V1 | ||
// IDEA : PARSING | ||
// https://leetcode.com/problems/remove-comments/editorial/ | ||
public List<String> removeComments_1(String[] source) { | ||
boolean inBlock = false; | ||
StringBuilder newline = new StringBuilder(); | ||
List<String> ans = new ArrayList(); | ||
for (String line: source) { | ||
int i = 0; | ||
char[] chars = line.toCharArray(); | ||
if (!inBlock) { | ||
newline = new StringBuilder(); | ||
} | ||
while (i < line.length()) { | ||
if (!inBlock && i+1 < line.length() && chars[i] == '/' && chars[i+1] == '*') { | ||
inBlock = true; | ||
i++; | ||
} else if (inBlock && i+1 < line.length() && chars[i] == '*' && chars[i+1] == '/') { | ||
inBlock = false; | ||
i++; | ||
} else if (!inBlock && i+1 < line.length() && chars[i] == '/' && chars[i+1] == '/') { | ||
break; | ||
} else if (!inBlock) { | ||
newline.append(chars[i]); | ||
} | ||
i++; | ||
} | ||
if (!inBlock && newline.length() > 0) { | ||
ans.add(new String(newline)); | ||
} | ||
} | ||
return ans; | ||
} | ||
|
||
|
||
// V2 | ||
// https://leetcode.ca/2017-11-21-722-Remove-Comments/ | ||
public List<String> removeComments_2(String[] source) { | ||
List<String> ans = new ArrayList<>(); | ||
StringBuilder sb = new StringBuilder(); | ||
boolean blockComment = false; | ||
for (String s : source) { | ||
int m = s.length(); | ||
for (int i = 0; i < m; ++i) { | ||
if (blockComment) { | ||
if (i + 1 < m && s.charAt(i) == '*' && s.charAt(i + 1) == '/') { | ||
blockComment = false; | ||
++i; | ||
} | ||
} else { | ||
if (i + 1 < m && s.charAt(i) == '/' && s.charAt(i + 1) == '*') { | ||
blockComment = true; | ||
++i; | ||
} else if (i + 1 < m && s.charAt(i) == '/' && s.charAt(i + 1) == '/') { | ||
break; | ||
} else { | ||
sb.append(s.charAt(i)); | ||
} | ||
} | ||
} | ||
if (!blockComment && sb.length() > 0) { | ||
ans.add(sb.toString()); | ||
sb.setLength(0); | ||
} | ||
} | ||
return ans; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters