or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
71. Simplify Path
My Solution
The Key Idea for Solving This Coding Question
要注意
path
的最後一個字元為'\'
的這個特殊狀況。當
path
的最後一個字元為'\'
時,第 17 行的while
迴圈條件不成立,而停止對name
的操作。在執行完第 22 ~ 33 行的程式後,第 7 行的最外層while
迴圈的條件仍然成立 (因為此時i
的值為path.size() - 1
)。所以仍會進入第 8 ~ 10 行的while
迴圈,使得i
的值更新為path.size()
。那麼緊接著第 8 行的while
迴圈條件失敗後,第 11 行的if
條件成立,並執行第 13 行的break
,然後離開第 7 ~ 34 行的整個while
迴圈。這樣的設計是可以處理這個特殊狀況的一個辦法。
C++ Code 1
C++ Code 2
Time Complexity
\(O(n)\)
\(n\) is the length of
path
.Space Complexity
\(O(H + n)\)
\(n\) is the length of
path
.\(H\) is the depth of the filesystem.
Miscellaneous