规范格式

This commit is contained in:
YDZ
2020-08-07 15:50:06 +08:00
parent 854a339abc
commit 4e11f4028a
1438 changed files with 907 additions and 924 deletions

View File

@ -0,0 +1,42 @@
package leetcode
import (
"strconv"
)
func decodeString(s string) string {
stack, res := []string{}, ""
for _, str := range s {
if len(stack) == 0 || (len(stack) > 0 && str != ']') {
stack = append(stack, string(str))
} else {
tmp := ""
for stack[len(stack)-1] != "[" {
tmp = stack[len(stack)-1] + tmp
stack = stack[:len(stack)-1]
}
stack = stack[:len(stack)-1]
index, repeat := 0, ""
for index = len(stack) - 1; index >= 0; index-- {
if stack[index] >= "0" && stack[index] <= "9" {
repeat = stack[index] + repeat
} else {
break
}
}
nums, _ := strconv.Atoi(repeat)
copyTmp := tmp
for i := 0; i < nums-1; i++ {
tmp += copyTmp
}
for i := 0; i < len(repeat)-1; i++ {
stack = stack[:len(stack)-1]
}
stack[index+1] = tmp
}
}
for _, s := range stack {
res += s
}
return res
}

View File

@ -0,0 +1,57 @@
package leetcode
import (
"fmt"
"testing"
)
type question394 struct {
para394
ans394
}
// para 是参数
// one 代表第一个参数
type para394 struct {
s string
}
// ans 是答案
// one 代表第一个答案
type ans394 struct {
one string
}
func Test_Problem394(t *testing.T) {
qs := []question394{
question394{
para394{"10[a]"},
ans394{"aaaaaaaaaa"},
},
question394{
para394{"3[a]2[bc]"},
ans394{"aaabcbc"},
},
question394{
para394{"3[a2[c]]"},
ans394{"accaccacc"},
},
question394{
para394{"2[abc]3[cd]ef"},
ans394{"abcabccdcdcdef"},
},
}
fmt.Printf("------------------------Leetcode Problem 394------------------------\n")
for _, q := range qs {
_, p := q.ans394, q.para394
fmt.Printf("【input】:%v 【output】:%v\n", p, decodeString(p.s))
}
fmt.Printf("\n\n\n")
}

View File

@ -0,0 +1,29 @@
# [394. Decode String](https://leetcode.com/problems/decode-string/)
## 题目
Given an encoded string, return its decoded string.
The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.
You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.
Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won't be input like 3a or 2[4].
Examples:
```c
s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
```
## 题目大意
给定一个经过编码的字符串,返回它解码后的字符串。编码规则为: k[encoded\_string],表示其中方括号内部的 encoded\_string 正好重复 k 次。注意 k 保证为正整数。你可以认为输入字符串总是有效的;输入字符串中没有额外的空格,且输入的方括号总是符合格式要求的。此外,你可以认为原始数据不包含数字,所有的数字只表示重复的次数 k 例如不会出现像 3a  2[4] 的输入。
## 解题思路
这一题和第 880 题大体类似。用栈处理,遇到 "[",就要开始重复字符串了,另外重复的数字是可能存在多位的,所以需要往前找到不为数字的那一位,把数字转换出来。最后用把 stack 里面的字符串都串联起来即可。