-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
leetcode/leetcode75/Stack/394.DecodeString/decodeString.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func decodeString(s string) string { | ||
s1, s2 := []int{}, []string{} | ||
num, result := 0, "" | ||
|
||
for i := 0; i < len(s); i++ { | ||
if s[i] >= '0' && s[i] <= '9' { | ||
num = num*10 + int(s[i]-'0') | ||
} else if s[i] == '[' { | ||
s1 = append(s1, num) | ||
s2 = append(s2, result) | ||
num = 0 | ||
result = "" | ||
} else if s[i] == ']' { | ||
n := s1[len(s1)-1] | ||
s1 = s1[:len(s1)-1] | ||
temp := "" | ||
for i := 0; i < n; i++ { | ||
temp += result | ||
} | ||
result = s2[len(s2)-1] | ||
s2 = s2[:len(s2)-1] | ||
result += temp | ||
} else { | ||
result += string(s[i]) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
func main() { | ||
s := "3[a2[c]]" | ||
fmt.Println(decodeString(s)) | ||
} |