Skip to content

Commit 1fdb5d9

Browse files
committed
Java solution 71 && 385
1 parent a2268cf commit 1fdb5d9

File tree

2 files changed

+191
-0
lines changed

2 files changed

+191
-0
lines changed

java/_071SimplifyPath.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.util.Arrays;
2+
import java.util.Stack;
3+
4+
/**
5+
* Given an absolute path for a file (Unix-style), simplify it.
6+
* <p>
7+
* For example,
8+
* path = "/home/", => "/home"
9+
* path = "/a/./b/../../c/", => "/c"
10+
* click to show corner cases.
11+
* <p>
12+
* Corner Cases:
13+
* Did you consider the case where path = "/../"?
14+
* In this case, you should return "/".
15+
* Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
16+
* In this case, you should ignore redundant slashes and return "/home/foo".
17+
* <p>
18+
* Created by drfish on 6/7/2017.
19+
*/
20+
public class _071SimplifyPath {
21+
public String simplifyPath(String path) {
22+
if (path == null) {
23+
return "";
24+
}
25+
String[] parts = path.split("/");
26+
Stack<String> stack = new Stack<>();
27+
for (String part : parts) {
28+
switch (part) {
29+
case ".":
30+
break;
31+
case "":
32+
break;
33+
case "..":
34+
if (!stack.isEmpty()) {
35+
stack.pop();
36+
}
37+
break;
38+
default:
39+
stack.push(part);
40+
}
41+
}
42+
43+
String result = "";
44+
for (String s : stack) {
45+
result += "/" + s;
46+
}
47+
return result.length() == 0 ? "/" : result;
48+
}
49+
50+
public static void main(String[] args) {
51+
_071SimplifyPath solution = new _071SimplifyPath();
52+
assert "/home".equals(solution.simplifyPath("/home/"));
53+
assert "/c".equals(solution.simplifyPath("/a/./b/../../c/"));
54+
assert "/".equals(solution.simplifyPath("/home/../../.."));
55+
}
56+
}

java/_385MiniParser.java

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import java.util.List;
2+
import java.util.Stack;
3+
4+
/**
5+
* Given a nested list of integers represented as a string, implement a parser to deserialize it.
6+
* Each element is either an integer, or a list -- whose elements may also be integers or other lists.
7+
* <p>
8+
* Note: You may assume that the string is well-formed:
9+
* String is non-empty.
10+
* String does not contain white spaces.
11+
* String contains only digits 0-9, [, - ,, ].
12+
* <p>
13+
* Example 1:
14+
* Given s = "324",
15+
* You should return a NestedInteger object which contains a single integer 324.
16+
* <p>
17+
* Example 2:
18+
* Given s = "[123,[456,[789]]]",
19+
* Return a NestedInteger object containing a nested list with 2 elements:
20+
* <p>
21+
* 1. An integer containing value 123.
22+
* 2. A nested list containing two elements:
23+
* i. An integer containing value 456.
24+
* ii. A nested list with one element:
25+
* a. An integer containing value 789.
26+
* <p>
27+
* Created by drfish on 6/9/2017.
28+
*/
29+
public class _385MiniParser {
30+
/**
31+
* // This is the interface that allows for creating nested lists.
32+
* // You should not implement it, or speculate about its implementation
33+
**/
34+
public interface INestedInteger {
35+
// // Constructor initializes an empty nested list.
36+
// public NestedInteger();
37+
//
38+
// // Constructor initializes a single integer.
39+
// public NestedInteger(int value);
40+
41+
// @return true if this NestedInteger holds a single integer, rather than a nested list.
42+
public boolean isInteger();
43+
44+
// @return the single integer that this NestedInteger holds, if it holds a single integer
45+
// Return null if this NestedInteger holds a nested list
46+
public Integer getInteger();
47+
48+
// Set this NestedInteger to hold a single integer.
49+
public void setInteger(int value);
50+
51+
// Set this NestedInteger to hold a nested list and adds a nested integer to it.
52+
public void add(NestedInteger ni);
53+
54+
// @return the nested list that this NestedInteger holds, if it holds a nested list
55+
// Return null if this NestedInteger holds a single integer
56+
public List<NestedInteger> getList();
57+
}
58+
59+
public class NestedInteger implements INestedInteger {
60+
public NestedInteger() {
61+
62+
}
63+
64+
public NestedInteger(int value) {
65+
66+
}
67+
68+
@Override
69+
public boolean isInteger() {
70+
return false;
71+
}
72+
73+
@Override
74+
public Integer getInteger() {
75+
return null;
76+
}
77+
78+
@Override
79+
public void setInteger(int value) {
80+
81+
}
82+
83+
@Override
84+
public void add(NestedInteger ni) {
85+
86+
}
87+
88+
@Override
89+
public List<NestedInteger> getList() {
90+
return null;
91+
}
92+
}
93+
94+
public class Solution {
95+
public NestedInteger deserialize(String s) {
96+
if (s == null || s.isEmpty()) {
97+
return null;
98+
}
99+
if (s.charAt(0) != '[') {
100+
return new NestedInteger(Integer.valueOf(s));
101+
}
102+
Stack<NestedInteger> stack = new Stack<>();
103+
int start = 0;
104+
NestedInteger curr = null;
105+
for (int i = 0; i < s.length(); i++) {
106+
char c = s.charAt(i);
107+
if (c == '[') {
108+
if (curr != null) {
109+
stack.push(curr);
110+
}
111+
curr = new NestedInteger();
112+
start = i + 1;
113+
} else if (c == ']') {
114+
String num = s.substring(start, i);
115+
if (!num.isEmpty()) {
116+
curr.add(new NestedInteger(Integer.valueOf(num)));
117+
}
118+
if (!stack.isEmpty()) {
119+
NestedInteger top = stack.pop();
120+
top.add(curr);
121+
curr = top;
122+
}
123+
start = i + 1;
124+
} else if (c == ',') {
125+
if (s.charAt(i - 1) != ']') {
126+
String num = s.substring(start, i);
127+
curr.add(new NestedInteger(Integer.valueOf(num)));
128+
}
129+
start = i + 1;
130+
}
131+
}
132+
return curr;
133+
}
134+
}
135+
}

0 commit comments

Comments
 (0)