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

feat: add solutions to lc problem: No.0733 #4087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions solution/0700-0799/0722.Remove Comments/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,20 @@ tags:
<strong>解释:</strong> 示例代码可以编排成这样:
/*Test program */
int main()
{
// variable declaration
{
// variable declaration
int a, b, c;
/* This is a test
multiline
comment for
multiline
comment for
testing */
a = b + c;
}
第 1 行和第 6-9 行的字符串 /* 表示块注释。第 4 行的字符串 // 表示行注释。
编排后:
编排后:
int main()
{
{

int a, b, c;
a = b + c;
}</pre>
Expand Down Expand Up @@ -106,15 +106,15 @@ a = b + c;

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

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

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

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

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

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

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

Expand Down
26 changes: 19 additions & 7 deletions solution/0700-0799/0722.Remove Comments/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,20 @@ tags:
<strong>Explanation:</strong> The line by line code is visualized as below:
/*Test program */
int main()
{
// variable declaration
{
// variable declaration
int a, b, c;
/* This is a test
multiline
comment for
multiline
comment for
testing */
a = b + c;
}
The string /* denotes a block comment, including line 1 and lines 6-9. The string // denotes line 4 as comments.
The line by line output code is visualized as below:
int main()
{
{

int a, b, c;
a = b + c;
}
Expand Down Expand Up @@ -102,7 +102,19 @@ a = b + c;

<!-- solution:start -->

### Solution 1
### Solution 1: Case Analysis

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.

Next, we traverse each line and discuss the following cases:

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.

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$.

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.

The time complexity is $O(L)$, and the space complexity is $O(L)$, where $L$ is the total length of the source code.

<!-- tabs:start -->

Expand Down
6 changes: 3 additions & 3 deletions solution/0700-0799/0727.Minimum Window Subsequence/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ s1 = "abcdebdde", s2 = "bde"

### 方法一:动态规划

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

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

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

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

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

<!-- tabs:start -->

Expand Down
20 changes: 18 additions & 2 deletions solution/0700-0799/0727.Minimum Window Subsequence/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ tags:
<pre>
<strong>Input:</strong> s1 = &quot;abcdebdde&quot;, s2 = &quot;bde&quot;
<strong>Output:</strong> &quot;bcde&quot;
<strong>Explanation:</strong>
<strong>Explanation:</strong>
&quot;bcde&quot; is the answer because it occurs before &quot;bdde&quot; which has the same length.
&quot;deb&quot; is not a smaller window because the elements of s2 in the window must occur in order.
</pre>
Expand All @@ -55,7 +55,23 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Dynamic Programming

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$.

We can derive the state transition equation:

$$
f[i][j] = \begin{cases}
i, & j = 1 \textit{ and } s1[i-1] = s2[j] \\
f[i - 1][j - 1], & j > 1 \textit{ and } s1[i-1] = s2[j-1] \\
f[i - 1][j], & s1[i-1] \ne s2[j-1]
\end{cases}
$$

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.

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.

<!-- tabs:start -->

Expand Down
Loading