Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5718c16

Browse files
authored
feat: add solutions to lc problem: No.0733 (doocs#4087)
No.0733.Flood Fill
1 parent a6a219e commit 5718c16

File tree

15 files changed

+562
-240
lines changed

15 files changed

+562
-240
lines changed

solution/0700-0799/0722.Remove Comments/README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,20 @@ tags:
5959
<strong>解释:</strong> 示例代码可以编排成这样:
6060
/*Test program */
6161
int main()
62-
{
63-
// variable declaration
62+
{
63+
// variable declaration
6464
int a, b, c;
6565
/* This is a test
66-
multiline
67-
comment for
66+
multiline
67+
comment for
6868
testing */
6969
a = b + c;
7070
}
7171
第 1 行和第 6-9 行的字符串 /* 表示块注释。第 4 行的字符串 // 表示行注释。
72-
编排后:
72+
编排后:
7373
int main()
74-
{
75-
74+
{
75+
7676
int a, b, c;
7777
a = b + c;
7878
}</pre>
@@ -106,15 +106,15 @@ a = b + c;
106106

107107
### 方法一:分情况讨论
108108

109-
我们用一个变量 $blockComment$ 来表示当前是否处于块注释中,初始时 $blockComment$ 为 `false`;用一个变量 $t$ 来存储当前行的有效字符。
109+
我们用一个变量 来表示当前是否处于块注释中,初始时 $\textit{blockComment}$ 为 `false`;用一个变量 $t$ 来存储当前行的有效字符。
110110

111111
接下来,遍历每一行,分情况讨论:
112112

113-
如果当前处于块注释中,那么如果当前字符和下一个字符是 `'*/'`,说明块注释结束,我们将 $blockComment$ 置为 `false`,并且跳过这两个字符;否则,我们继续保持块注释状态,不做任何操作;
113+
如果当前处于块注释中,那么如果当前字符和下一个字符是 `'*/'`,说明块注释结束,我们将 $\textit{blockComment}$ 置为 `false`,并且跳过这两个字符;否则,我们继续保持块注释状态,不做任何操作;
114114

115-
如果当前不处于块注释中,那么如果当前字符和下一个字符是 `'/*'`,说明块注释开始,我们将 $blockComment$ 置为 `true`,并且跳过这两个字符;如果当前字符和下一个字符是 `'//'`,那么说明行注释开始,我们直接退出当前行的遍历;否则,说明当前字符是有效字符,我们将其加入 $t$ 中;
115+
如果当前不处于块注释中,那么如果当前字符和下一个字符是 `'/*'`,说明块注释开始,我们将 $\textit{blockComment}$ 置为 `true`,并且跳过这两个字符;如果当前字符和下一个字符是 `'//'`,那么说明行注释开始,我们直接退出当前行的遍历;否则,说明当前字符是有效字符,我们将其加入 $t$ 中;
116116

117-
遍历完当前行后,如果 $blockComment$ 为 `false`,并且 $t$ 不为空,说明当前行是有效行,我们将其加入答案数组中,并且清空 $t$。继续遍历下一行。
117+
遍历完当前行后,如果 $\textit{blockComment}$ 为 `false`,并且 $t$ 不为空,说明当前行是有效行,我们将其加入答案数组中,并且清空 $t$。继续遍历下一行。
118118

119119
时间复杂度 $O(L)$,空间复杂度 $O(L)$,其中 $L$ 是源代码的总长度。
120120

solution/0700-0799/0722.Remove Comments/README_EN.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,20 @@ tags:
5858
<strong>Explanation:</strong> The line by line code is visualized as below:
5959
/*Test program */
6060
int main()
61-
{
62-
// variable declaration
61+
{
62+
// variable declaration
6363
int a, b, c;
6464
/* This is a test
65-
multiline
66-
comment for
65+
multiline
66+
comment for
6767
testing */
6868
a = b + c;
6969
}
7070
The string /* denotes a block comment, including line 1 and lines 6-9. The string // denotes line 4 as comments.
7171
The line by line output code is visualized as below:
7272
int main()
73-
{
74-
73+
{
74+
7575
int a, b, c;
7676
a = b + c;
7777
}
@@ -102,7 +102,19 @@ a = b + c;
102102

103103
<!-- solution:start -->
104104

105-
### Solution 1
105+
### Solution 1: Case Analysis
106+
107+
We use a variable $\textit{blockComment}$ to indicate whether we are currently in a block comment. Initially, $\textit{blockComment}$ is `false`. We use a variable $t$ to store the valid characters of the current line.
108+
109+
Next, we traverse each line and discuss the following cases:
110+
111+
If we are currently in a block comment, and the current character and the next character are `'*/'`, it means the block comment ends. We set $\textit{blockComment}$ to `false` and skip these two characters. Otherwise, we continue in the block comment state without doing anything.
112+
113+
If we are not currently in a block comment, and the current character and the next character are `'/*'`, it means a block comment starts. We set $\textit{blockComment}$ to `true` and skip these two characters. If the current character and the next character are `'//'`, it means a line comment starts, and we exit the current line traversal. Otherwise, the current character is a valid character, and we add it to $t$.
114+
115+
After traversing the current line, if $\textit{blockComment}$ is `false` and $t$ is not empty, it means the current line is valid. We add it to the answer array and clear $t$. Continue to traverse the next line.
116+
117+
The time complexity is $O(L)$, and the space complexity is $O(L)$, where $L$ is the total length of the source code.
106118

107119
<!-- tabs:start -->
108120

solution/0700-0799/0727.Minimum Window Subsequence/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ s1 = "abcdebdde", s2 = "bde"
6060

6161
### 方法一:动态规划
6262

63-
我们定义 $f[i][j]$ 表示字符串 $s1$ 的前 $i$ 个字符包含字符串 $s2$ 的前 $j$ 个字符时的最短子串的起始位置,如果不存在则为 $0$。
63+
我们定义 $f[i][j]$ 表示字符串 $\textit{s1}$ 的前 $i$ 个字符包含字符串 $\textit{s2}$ 的前 $j$ 个字符时的最短子串的起始位置,如果不存在则为 $0$。
6464

6565
我们可以得到状态转移方程:
6666

@@ -72,9 +72,9 @@ f[i - 1][j], & s1[i-1] \ne s2[j-1]
7272
\end{cases}
7373
$$
7474

75-
接下来我们只需要遍历 $s1$,如果 $f[i][n] \gt 0$,则更新最短子串的起始位置和长度。最后返回最短子串即可。
75+
接下来我们只需要遍历 $\textit{s1}$,如果 $f[i][n] \gt 0$,则更新最短子串的起始位置和长度。最后返回最短子串即可。
7676

77-
时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别为字符串 $s1$ 和 $s2$ 的长度。
77+
时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别为字符串 $\textit{s1}$ 和 $\textit{s2}$ 的长度。
7878

7979
<!-- tabs:start -->
8080

solution/0700-0799/0727.Minimum Window Subsequence/README_EN.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ tags:
2828
<pre>
2929
<strong>Input:</strong> s1 = &quot;abcdebdde&quot;, s2 = &quot;bde&quot;
3030
<strong>Output:</strong> &quot;bcde&quot;
31-
<strong>Explanation:</strong>
31+
<strong>Explanation:</strong>
3232
&quot;bcde&quot; is the answer because it occurs before &quot;bdde&quot; which has the same length.
3333
&quot;deb&quot; is not a smaller window because the elements of s2 in the window must occur in order.
3434
</pre>
@@ -55,7 +55,23 @@ tags:
5555

5656
<!-- solution:start -->
5757

58-
### Solution 1
58+
### Solution 1: Dynamic Programming
59+
60+
We define $f[i][j]$ to represent the starting position of the shortest substring of the first $i$ characters of string $\textit{s1}$ that contains the first $j$ characters of string $\textit{s2}$. If it does not exist, it is $0$.
61+
62+
We can derive the state transition equation:
63+
64+
$$
65+
f[i][j] = \begin{cases}
66+
i, & j = 1 \textit{ and } s1[i-1] = s2[j] \\
67+
f[i - 1][j - 1], & j > 1 \textit{ and } s1[i-1] = s2[j-1] \\
68+
f[i - 1][j], & s1[i-1] \ne s2[j-1]
69+
\end{cases}
70+
$$
71+
72+
Next, we only need to traverse $\textit{s1}$. If $f[i][n] \gt 0$, update the starting position and length of the shortest substring. Finally, return the shortest substring.
73+
74+
The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. Here, $m$ and $n$ are the lengths of strings $\textit{s1}$ and $\textit{s2}$, respectively.
5975

6076
<!-- tabs:start -->
6177

0 commit comments

Comments
 (0)