|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| - |
4 | 3 | import java.util.Arrays;
|
5 | 4 | import java.util.Deque;
|
6 | 5 | import java.util.HashSet;
|
7 | 6 | import java.util.LinkedList;
|
8 | 7 | import java.util.Set;
|
9 | 8 |
|
10 | 9 | /**
|
11 |
| - * Given an absolute path for a file (Unix-style), simplify it. |
| 10 | + * 70. Climbing Stairs |
| 11 | +
|
| 12 | + Given an absolute path for a file (Unix-style), simplify it. |
12 | 13 |
|
13 | 14 | For example,
|
14 | 15 | path = "/home/", => "/home"
|
|
22 | 23 | */
|
23 | 24 | public class _71 {
|
24 | 25 |
|
| 26 | + public static class Solution1 { |
25 | 27 | public String simplifyPath(String path) {
|
26 |
| - Deque<String> stack = new LinkedList<>(); |
27 |
| - Set<String> skipSet = new HashSet<>(Arrays.asList("..", ".", "")); |
28 |
| - for (String dir : path.split("/")) { |
29 |
| - if (dir.equals("..") && !stack.isEmpty()) { |
30 |
| - stack.pop(); |
31 |
| - } else if (!skipSet.contains(dir)) { |
32 |
| - stack.push(dir); |
33 |
| - } |
| 28 | + Deque<String> stack = new LinkedList<>(); |
| 29 | + Set<String> skipSet = new HashSet<>(Arrays.asList("..", ".", "")); |
| 30 | + for (String dir : path.split("/")) { |
| 31 | + if (dir.equals("..") && !stack.isEmpty()) { |
| 32 | + stack.pop(); |
| 33 | + } else if (!skipSet.contains(dir)) { |
| 34 | + stack.push(dir); |
34 | 35 | }
|
35 |
| - String result = ""; |
36 |
| - for (String dir : stack) { |
37 |
| - result = "/" + dir + result; |
38 |
| - } |
39 |
| - return result.isEmpty() ? "/" : result; |
| 36 | + } |
| 37 | + String result = ""; |
| 38 | + for (String dir : stack) { |
| 39 | + result = "/" + dir + result; |
| 40 | + } |
| 41 | + return result.isEmpty() ? "/" : result; |
40 | 42 | }
|
41 |
| - |
| 43 | + } |
42 | 44 | }
|
0 commit comments