Skip to content

Commit c1ffca5

Browse files
committed
change
1 parent d83e3a3 commit c1ffca5

File tree

2 files changed

+75
-32
lines changed

2 files changed

+75
-32
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.ArrayList;
2+
3+
public class LongestIncreasingSubsequence {
4+
public int lengthOfLIS(int[] nums) {
5+
ArrayList<Integer> sub = new ArrayList<>();
6+
sub.add(nums[0]);
7+
8+
for (int i = 1; i < nums.length; i++) {
9+
int num = nums[i];
10+
if (num > sub.get(sub.size() - 1)) {
11+
sub.add(num);
12+
} else {
13+
// Find the first element in sub that is greater than or equal to num
14+
int j = 0;
15+
while (num > sub.get(j)) {
16+
j += 1;
17+
}
18+
19+
sub.set(j, num);
20+
}
21+
}
22+
23+
return sub.size();
24+
}
25+
}

src/Solution.java

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,63 @@
1-
import java.util.*;
1+
import java.util.Stack;
22

3+
class Pair{
4+
String s;
5+
int n;
6+
public Pair(String s, int n) {
7+
this.s = s;
8+
this.n = n;
9+
}
10+
}
311
class Solution {
4-
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
5-
HashMap<Integer, HashSet<Integer>> connect = new HashMap<>();
6-
for(int [] edge : edges){
7-
HashSet<Integer> set1 = connect.getOrDefault(edge[0], new HashSet<Integer>());
8-
set1.add(edge[1]);
9-
connect.put(edge[0], set1);
10-
HashSet<Integer> set2 =connect.getOrDefault(edge[1], new HashSet<Integer>());
11-
set2.add(edge[0]);
12-
connect.put(edge[1], set2);
13-
}
14-
List<Integer> leaves = new LinkedList<>();
15-
for(Integer key: connect.keySet()){
16-
if(connect.get(key).size() == 1){
17-
leaves.add(key);
12+
public String decodeString(String s) {
13+
int p = 0;
14+
int i = 0;
15+
StringBuilder sb = new StringBuilder();
16+
Stack<Pair> stack = new Stack<>();
17+
String ans = "";
18+
while(p < s.length()){
19+
while(s.charAt(p) - '0' < 10 && s.charAt(p) - '0' > -1){
20+
i = i * 10 + (s.charAt(p) - '0');
21+
p ++;
1822
}
19-
}
20-
int remains = n;
21-
while(remains > 2){
22-
List<Integer> newLeaves = new LinkedList<>();
23-
for(Integer leaf: leaves){
24-
connect.remove(leaf);
25-
remains --;
26-
for(Integer key: connect.keySet()){
27-
if(connect.get(key).contains(leaf)){
28-
connect.get(key).remove(leaf);
29-
if(connect.get(key).size() == 1){
30-
newLeaves.add(key);
31-
}
23+
if(s.charAt(p) == '[') p ++;
24+
while(p < s.length() && s.charAt(p) - 'a' < 27 && s.charAt(p) - 'a' > -1){
25+
sb.append(s.charAt(p));
26+
p ++;
27+
}
28+
if(p < s.length() && s.charAt(p) == ']'){
29+
StringBuilder t = new StringBuilder();
30+
if(sb.length() > 0) {
31+
for (int x = 0; x < i; x++) {
32+
t.append(sb.toString());
3233
}
3334
}
35+
else{
36+
Pair pa = stack.pop();
37+
for (int x = 0; x < pa.n; x++) {
38+
t.append(pa.s);
39+
}
40+
}
41+
if (stack.empty()) {
42+
ans += t.toString();
43+
} else {
44+
stack.peek().s += t;
45+
}
46+
p++;
3447
}
35-
leaves = newLeaves;
48+
else{
49+
if(i > 0)stack.add(new Pair(sb.toString(),i));
50+
else ans += sb.toString();
51+
}
52+
sb = new StringBuilder();
53+
i = 0;
3654
}
37-
return leaves;
55+
return ans;
3856
}
3957

4058
public static void main(String[] args) {
4159
Solution s = new Solution();
42-
int [][] edges = {{1,0},{1,2},{1,3}};
43-
System.out.println(s.findMinHeightTrees(4,edges));
60+
System.out.println(s.decodeString("3[z]2[2[y]pq4[2[jk]e1[f]]]ef"));
61+
4462
}
4563
}

0 commit comments

Comments
 (0)