|
23333 | 23333 | <ul class="md-nav__list">
|
23334 | 23334 |
|
23335 | 23335 | <li class="md-nav__item">
|
23336 |
| - <a href="#solution-1" class="md-nav__link"> |
| 23336 | + <a href="#solution-1-case-analysis" class="md-nav__link"> |
23337 | 23337 | <span class="md-ellipsis">
|
23338 |
| - Solution 1 |
| 23338 | + Solution 1: Case Analysis |
23339 | 23339 | </span>
|
23340 | 23340 | </a>
|
23341 | 23341 |
|
@@ -86403,19 +86403,19 @@ <h2 id="description">Description</h2>
|
86403 | 86403 | <strong>Explanation:</strong> The line by line code is visualized as below:
|
86404 | 86404 | /*Test program */
|
86405 | 86405 | int main()
|
86406 |
| -{ |
86407 |
| - // variable declaration |
| 86406 | +{ |
| 86407 | + // variable declaration |
86408 | 86408 | int a, b, c;
|
86409 | 86409 | /* This is a test
|
86410 |
| - multiline |
86411 |
| - comment for |
| 86410 | + multiline |
| 86411 | + comment for |
86412 | 86412 | testing */
|
86413 | 86413 | a = b + c;
|
86414 | 86414 | }
|
86415 | 86415 | The string /* denotes a block comment, including line 1 and lines 6-9. The string // denotes line 4 as comments.
|
86416 | 86416 | The line by line output code is visualized as below:
|
86417 | 86417 | int main()
|
86418 |
| -{ |
| 86418 | +{ |
86419 | 86419 |
|
86420 | 86420 | int a, b, c;
|
86421 | 86421 | a = b + c;
|
@@ -86446,7 +86446,13 @@ <h2 id="description">Description</h2>
|
86446 | 86446 | <h2 id="solutions">Solutions</h2>
|
86447 | 86447 | <!-- solution:start -->
|
86448 | 86448 |
|
86449 |
| -<h3 id="solution-1">Solution 1</h3> |
| 86449 | +<h3 id="solution-1-case-analysis">Solution 1: Case Analysis</h3> |
| 86450 | +<p>We use a variable <span class="arithmatex">\(\textit{blockComment}\)</span> to indicate whether we are currently in a block comment. Initially, <span class="arithmatex">\(\textit{blockComment}\)</span> is <code>false</code>. We use a variable <span class="arithmatex">\(t\)</span> to store the valid characters of the current line.</p> |
| 86451 | +<p>Next, we traverse each line and discuss the following cases:</p> |
| 86452 | +<p>If we are currently in a block comment, and the current character and the next character are <code>'*/'</code>, it means the block comment ends. We set <span class="arithmatex">\(\textit{blockComment}\)</span> to <code>false</code> and skip these two characters. Otherwise, we continue in the block comment state without doing anything.</p> |
| 86453 | +<p>If we are not currently in a block comment, and the current character and the next character are <code>'/*'</code>, it means a block comment starts. We set <span class="arithmatex">\(\textit{blockComment}\)</span> to <code>true</code> and skip these two characters. If the current character and the next character are <code>'//'</code>, 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 <span class="arithmatex">\(t\)</span>.</p> |
| 86454 | +<p>After traversing the current line, if <span class="arithmatex">\(\textit{blockComment}\)</span> is <code>false</code> and <span class="arithmatex">\(t\)</span> is not empty, it means the current line is valid. We add it to the answer array and clear <span class="arithmatex">\(t\)</span>. Continue to traverse the next line.</p> |
| 86455 | +<p>The time complexity is <span class="arithmatex">\(O(L)\)</span>, and the space complexity is <span class="arithmatex">\(O(L)\)</span>, where <span class="arithmatex">\(L\)</span> is the total length of the source code.</p> |
86450 | 86456 | <div class="tabbed-set tabbed-alternate" data-tabs="1:6"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><input id="__tabbed_1_6" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Python3</label><label for="__tabbed_1_2">Java</label><label for="__tabbed_1_3">C++</label><label for="__tabbed_1_4">Go</label><label for="__tabbed_1_5">TypeScript</label><label for="__tabbed_1_6">Rust</label></div>
|
86451 | 86457 | <div class="tabbed-content">
|
86452 | 86458 | <div class="tabbed-block">
|
|
0 commit comments