|
67 | 67 |
|
68 | 68 | <!-- 这里可写通用的实现逻辑 -->
|
69 | 69 |
|
70 |
| -栈实现。 |
| 70 | +**方法一:栈** |
| 71 | + |
| 72 | +我们先将路径按照 `'/'` 分割成若干个子串,然后遍历每个子串,根据子串的内容进行如下操作: |
| 73 | + |
| 74 | +- 若子串为空,或者为 `'.'`,则不做任何操作,因为 `'.'` 表示当前目录; |
| 75 | +- 若子串为 `'..'`,则需要将栈顶元素弹出,因为 `'..'` 表示上一级目录; |
| 76 | +- 若子串为其他字符串,则将该子串入栈,因为该子串表示当前目录的子目录。 |
| 77 | + |
| 78 | +最后,我们将栈中的所有元素按照从栈底到栈顶的顺序拼接成字符串,即为简化后的规范路径。 |
| 79 | + |
| 80 | +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为路径的长度。 |
71 | 81 |
|
72 | 82 | <!-- tabs:start -->
|
73 | 83 |
|
@@ -113,6 +123,39 @@ class Solution {
|
113 | 123 | }
|
114 | 124 | ```
|
115 | 125 |
|
| 126 | +### **C++** |
| 127 | + |
| 128 | +```cpp |
| 129 | +class Solution { |
| 130 | +public: |
| 131 | + string simplifyPath(string path) { |
| 132 | + deque<string> stk; |
| 133 | + stringstream ss(path); |
| 134 | + string t; |
| 135 | + while (getline(ss, t, '/')) { |
| 136 | + if (t == "" || t == ".") { |
| 137 | + continue; |
| 138 | + } |
| 139 | + if (t == "..") { |
| 140 | + if (!stk.empty()) { |
| 141 | + stk.pop_back(); |
| 142 | + } |
| 143 | + } else { |
| 144 | + stk.push_back(t); |
| 145 | + } |
| 146 | + } |
| 147 | + if (stk.empty()) { |
| 148 | + return "/"; |
| 149 | + } |
| 150 | + string ans; |
| 151 | + for (auto& s : stk) { |
| 152 | + ans += "/" + s; |
| 153 | + } |
| 154 | + return ans; |
| 155 | + } |
| 156 | +}; |
| 157 | +``` |
| 158 | +
|
116 | 159 | ### **Go**
|
117 | 160 |
|
118 | 161 | ```go
|
@@ -140,114 +183,58 @@ func simplifyPath(path string) string {
|
140 | 183 | }
|
141 | 184 | ```
|
142 | 185 |
|
143 |
| -### **C#** |
144 |
| - |
145 |
| -```cs |
146 |
| -using System.Collections.Generic; |
147 |
| -using System.Linq; |
148 |
| -using System.Text; |
149 |
| - |
150 |
| -public class Solution { |
151 |
| - public string SimplifyPath(string path) { |
152 |
| - var stack = new Stack<string>(); |
153 |
| - var sb = new StringBuilder(); |
154 |
| - foreach (var ch in ((IEnumerable<char>)path).Concat(Enumerable.Repeat('/', 1))) |
155 |
| - { |
156 |
| - if (ch == '/') |
157 |
| - { |
158 |
| - if (sb.Length > 0) |
159 |
| - { |
160 |
| - var folder = sb.ToString(); |
161 |
| - sb.Clear(); |
162 |
| - switch (folder) |
163 |
| - { |
164 |
| - case ".": |
165 |
| - break; |
166 |
| - case "..": |
167 |
| - if (stack.Any()) |
168 |
| - { |
169 |
| - stack.Pop(); |
170 |
| - } |
171 |
| - break; |
172 |
| - default: |
173 |
| - stack.Push(folder); |
174 |
| - break; |
175 |
| - } |
176 |
| - } |
177 |
| - } |
178 |
| - else |
179 |
| - { |
180 |
| - sb.Append(ch); |
181 |
| - } |
182 |
| - } |
| 186 | +### **TypeScript** |
183 | 187 |
|
184 |
| - if (stack.Count == 0) |
185 |
| - { |
186 |
| - sb.Append('/'); |
| 188 | +```ts |
| 189 | +function simplifyPath(path: string): string { |
| 190 | + const stk: string[] = []; |
| 191 | + for (const s of path.split('/')) { |
| 192 | + if (s === '' || s === '.') { |
| 193 | + continue; |
187 | 194 | }
|
188 |
| - foreach (var folder in ((IEnumerable<string>)stack.ToList()).Reverse()) |
189 |
| - { |
190 |
| - sb.Append('/'); |
191 |
| - sb.Append(folder); |
| 195 | + if (s === '..') { |
| 196 | + if (stk.length) { |
| 197 | + stk.pop(); |
| 198 | + } |
| 199 | + } else { |
| 200 | + stk.push(s); |
192 | 201 | }
|
193 |
| - return sb.ToString(); |
194 | 202 | }
|
| 203 | + return '/' + stk.join('/'); |
195 | 204 | }
|
196 | 205 | ```
|
197 | 206 |
|
198 |
| -### **TypeScript** |
| 207 | +### **C#** |
199 | 208 |
|
200 |
| -```ts |
201 |
| -function simplifyPath(path: string): string { |
202 |
| - // 添加辅助斜线 |
203 |
| - path += '/'; |
204 |
| - |
205 |
| - const stack = []; |
206 |
| - let str = ''; |
207 |
| - for (let i = 1; i < path.length; i++) { |
208 |
| - const c = path[i]; |
209 |
| - if (c === '/') { |
210 |
| - if (str !== '' && str !== '.') { |
211 |
| - if (str === '..') { |
212 |
| - if (stack.length !== 0) { |
213 |
| - stack.pop(); |
214 |
| - } |
215 |
| - } else { |
216 |
| - stack.push(str); |
| 209 | +```cs |
| 210 | +public class Solution { |
| 211 | + public string SimplifyPath(string path) { |
| 212 | + var stk = new Stack<string>(); |
| 213 | + foreach (var s in path.Split('/')) { |
| 214 | + if (s == "" || s == ".") { |
| 215 | + continue; |
| 216 | + } |
| 217 | + if (s == "..") { |
| 218 | + if (stk.Count > 0) { |
| 219 | + stk.Pop(); |
217 | 220 | }
|
| 221 | + } else { |
| 222 | + stk.Push(s); |
218 | 223 | }
|
219 |
| - str = ''; |
220 |
| - } else { |
221 |
| - str += c; |
222 | 224 | }
|
| 225 | + var sb = new StringBuilder(); |
| 226 | + while (stk.Count > 0) { |
| 227 | + sb.Insert(0, "/" + stk.Pop()); |
| 228 | + } |
| 229 | + return sb.Length == 0 ? "/" : sb.ToString(); |
223 | 230 | }
|
224 |
| - |
225 |
| - return '/' + stack.join('/'); |
226 | 231 | }
|
227 | 232 | ```
|
228 | 233 |
|
229 |
| -### **C++** |
| 234 | +### **...** |
| 235 | + |
| 236 | +``` |
230 | 237 |
|
231 |
| -```cpp |
232 |
| -class Solution { |
233 |
| -public: |
234 |
| - string simplifyPath(string path) { |
235 |
| - deque<string> stk; |
236 |
| - string res, tmp; |
237 |
| - stringstream ss(path); |
238 |
| - while (getline(ss, tmp, '/')) { |
239 |
| - if (tmp == "" || tmp == ".") continue; |
240 |
| - if (tmp == "..") { |
241 |
| - if (!stk.empty()) |
242 |
| - stk.pop_back(); |
243 |
| - } else |
244 |
| - stk.push_back(tmp); |
245 |
| - } |
246 |
| - for (auto str : stk) |
247 |
| - res += "/" + str; |
248 |
| - return res.empty() ? "/" : res; |
249 |
| - } |
250 |
| -}; |
251 | 238 | ```
|
252 | 239 |
|
253 | 240 | <!-- tabs:end -->
|
0 commit comments