Skip to content

Commit 6b269e4

Browse files
author
falldio
committed
Go for Decode String
1 parent 8f68c7c commit 6b269e4

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Stack.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,40 @@ public:
582582
};
583583
```
584584

585+
```go
586+
type item struct {
587+
n int
588+
bytes []byte
589+
}
590+
591+
func decodeString(str string) string {
592+
num := 0
593+
st := []item{{1, []byte{}}}
594+
595+
for i := range str {
596+
switch {
597+
case str[i] == '0':
598+
num *= 10
599+
case str[i] > '0' && str[i] <= '9':
600+
num = num*10 + int(str[i]-'0')
601+
case str[i] == '[':
602+
st = append(st, item{num, []byte{}})
603+
num = 0
604+
case str[i] == ']':
605+
tmp := st[len(st)-1]
606+
st = st[:len(st)-1]
607+
for j := 0; j < tmp.n; j++ {
608+
st[len(st)-1].bytes = append(st[len(st)-1].bytes, tmp.bytes...)
609+
}
610+
default:
611+
st[len(st)-1].bytes = append(st[len(st)-1].bytes, str[i])
612+
}
613+
}
614+
615+
return string(st[0].bytes)
616+
}
617+
```
618+
585619
## [Remove K Digits](https://leetcode.com/problems/remove-k-digits)
586620

587621
A: 升序栈,仅当字符串出现降序时需要移除降序部分中较大的数字。

0 commit comments

Comments
 (0)