-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution722.cs
56 lines (50 loc) · 1.53 KB
/
Solution722.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System.Text;
namespace LeetCode.Solutions;
public class Solution722
{
/// <summary>
/// 722. Remove Comments - Medium
/// <a href="https://leetcode.com/problems/remove-comments">See the problem</a>
/// </summary>
public IList<string> RemoveComments(string[] source)
{
var sb = new StringBuilder();
var inBlockComment = false;
var result = new List<string>();
foreach (var line in source)
{
var i = 0;
if (!inBlockComment)
{
sb = new StringBuilder();
}
while (i < line.Length)
{
if (!inBlockComment && i + 1 < line.Length && line[i] == '/' && line[i + 1] == '/')
{
break; // Skip the rest of the line
}
else if (!inBlockComment && i + 1 < line.Length && line[i] == '/' && line[i + 1] == '*')
{
inBlockComment = true;
i++;
}
else if (inBlockComment && i + 1 < line.Length && line[i] == '*' && line[i + 1] == '/')
{
inBlockComment = false;
i++;
}
else if (!inBlockComment)
{
sb.Append(line[i]);
}
i++;
}
if (!inBlockComment && sb.Length > 0)
{
result.Add(sb.ToString());
}
}
return result;
}
}