File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed
src/main/java/algorithm/leetcode Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ package algorithm .leetcode ;
2+
3+ import java .util .LinkedList ;
4+
5+ /**
6+ * @author: mayuan
7+ * @desc: 字符串解码
8+ * @date: 2019/03/15
9+ */
10+ public class Solution394 {
11+
12+ public String decodeString (String s ) {
13+ if (null == s || 0 >= s .length ()) {
14+ return "" ;
15+ }
16+
17+ String ans = "" ;
18+ LinkedList <String > resStack = new LinkedList <>();
19+ LinkedList <Integer > numberStack = new LinkedList <>();
20+ int idx = 0 ;
21+ while (idx < s .length ()) {
22+ char c = s .charAt (idx );
23+ if (Character .isDigit (c )) {
24+ int num = 0 ;
25+ while (Character .isDigit (s .charAt (idx ))) {
26+ num = num * 10 + (s .charAt (idx ) - '0' );
27+ ++idx ;
28+ }
29+ numberStack .push (num );
30+ } else if ('[' == c ) {
31+ resStack .push (ans );
32+ ans = "" ;
33+ ++idx ;
34+ } else if (']' == c ) {
35+ StringBuilder stringBuilder = new StringBuilder (resStack .pop ());
36+ int repeat = numberStack .pop ();
37+ while (0 < repeat --) {
38+ stringBuilder .append (ans );
39+ }
40+ ans = stringBuilder .toString ();
41+ ++idx ;
42+ } else {
43+ ans += c ;
44+ ++idx ;
45+ }
46+ }
47+
48+ return ans ;
49+ }
50+ }
You can’t perform that action at this time.
0 commit comments