-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path0722-RemoveComments.cs
55 lines (47 loc) · 1.67 KB
/
0722-RemoveComments.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
//-----------------------------------------------------------------------------
// Runtime: 232ms
// Memory Usage: 30.9 MB
// Link: https://leetcode.com/submissions/detail/367603448/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Text;
namespace LeetCode
{
public class _0722_RemoveComments
{
public IList<string> RemoveComments(string[] source)
{
var inCommentBlock = false;
var sb = new StringBuilder();
var result = new List<string>();
foreach (var line in source)
{
if (!inCommentBlock && sb.Length > 0) sb.Clear();
for (int i = 0; i < line.Length; i++)
{
if (inCommentBlock)
{
if (i + 1 < line.Length && line[i] == '*' && line[i + 1] == '/')
{
inCommentBlock = false;
i++;
}
continue;
}
if (i + 1 < line.Length)
if (line[i] == '/' && line[i + 1] == '/') break;
else if (line[i] == '/' && line[i + 1] == '*')
{
inCommentBlock = true;
i++;
continue;
}
sb.Append(line[i]);
}
if (!inCommentBlock && sb.Length > 0)
result.Add(sb.ToString());
}
return result;
}
}
}