Skip to content

Commit eda8b0a

Browse files
committed
fd
1 parent 554c400 commit eda8b0a

File tree

2 files changed

+58
-17
lines changed

2 files changed

+58
-17
lines changed

solution/src/main/java/com/inuker/solution/DecodeString.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,38 @@
88

99
public class DecodeString {
1010

11+
/**
12+
* 一个函数搞定,思路很简单,利用一个栈,遇到不是']'则不断入栈,遇到']'则不断出栈,
13+
* 直到遇到'[',然后继续出栈数字,再将重复的字符串入栈
14+
*/
15+
public static String decodeString(String s) {
16+
StringBuilder stack = new StringBuilder();
17+
for (char c : s.toCharArray()) {
18+
if (c != ']') {
19+
stack.append(c);
20+
} else {
21+
StringBuilder sb = new StringBuilder();
22+
while (stack.charAt(stack.length() - 1) != '[') {
23+
sb.insert(0, stack.charAt(stack.length() - 1));
24+
stack.setLength(stack.length() - 1);
25+
}
26+
stack.setLength(stack.length() - 1);
27+
int n = 0, t = 1;
28+
while (stack.length() > 0 && Character.isDigit(stack.charAt(stack.length() - 1))) {
29+
n += t * (stack.charAt(stack.length() - 1) - '0');
30+
t *= 10;
31+
stack.setLength(stack.length() - 1);
32+
}
33+
for (int i = 0; i < n; i++) {
34+
for (int j = 0; j < sb.length(); j++) {
35+
stack.append(sb.charAt(j));
36+
}
37+
}
38+
}
39+
}
40+
return stack.toString();
41+
}
42+
1143
// 耗时3ms,思路很直观,且不容易错,面试推荐写法
1244
public String decodeString2(String s) {
1345
StringBuilder sb = new StringBuilder();

test/src/main/java/com/inuker/test/main.java

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,35 @@
1919
public class main {
2020

2121
public static void main(String[] args) {
22+
String s = decodeString("2[abc]3[cd]ef");
23+
System.out.println(s);
2224
}
2325

24-
class MovingAverage {
25-
26-
private Deque<Integer> mQueue = new LinkedList<>();
27-
private int mSize;
28-
private int mSum;
29-
30-
/** Initialize your data structure here. */
31-
public MovingAverage(int size) {
32-
mSize = size;
33-
}
34-
35-
public double next(int val) {
36-
mQueue.offerLast(val);
37-
mSum += val;
38-
if (mQueue.size() > mSize) {
39-
mSum -= mQueue.pollFirst();
26+
public static String decodeString(String s) {
27+
StringBuilder stack = new StringBuilder();
28+
for (char c : s.toCharArray()) {
29+
if (c != ']') {
30+
stack.append(c);
31+
} else {
32+
StringBuilder sb = new StringBuilder();
33+
while (stack.charAt(stack.length() - 1) != '[') {
34+
sb.insert(0, stack.charAt(stack.length() - 1));
35+
stack.setLength(stack.length() - 1);
36+
}
37+
stack.setLength(stack.length() - 1);
38+
int n = 0, t = 1;
39+
while (stack.length() > 0 && Character.isDigit(stack.charAt(stack.length() - 1))) {
40+
n += t * (stack.charAt(stack.length() - 1) - '0');
41+
t *= 10;
42+
stack.setLength(stack.length() - 1);
43+
}
44+
for (int i = 0; i < n; i++) {
45+
for (int j = 0; j < sb.length(); j++) {
46+
stack.append(sb.charAt(j));
47+
}
48+
}
4049
}
41-
return (double) mSum / mQueue.size();
4250
}
51+
return stack.toString();
4352
}
4453
}

0 commit comments

Comments
 (0)