规范格式

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,33 @@
package leetcode
func scoreOfParentheses(S string) int {
res, stack, top, temp := 0, []int{}, -1, 0
for _, s := range S {
if s == '(' {
stack = append(stack, -1)
top++
} else {
temp = 0
for stack[top] != -1 {
temp += stack[top]
stack = stack[:len(stack)-1]
top--
}
stack = stack[:len(stack)-1]
top--
if temp == 0 {
stack = append(stack, 1)
top++
} else {
stack = append(stack, temp*2)
top++
}
}
}
for len(stack) != 0 {
res += stack[top]
stack = stack[:len(stack)-1]
top--
}
return res
}

View File

@ -0,0 +1,66 @@
package leetcode
import (
"fmt"
"testing"
)
type question856 struct {
para856
ans856
}
// para 是参数
// one 代表第一个参数
type para856 struct {
one string
}
// ans 是答案
// one 代表第一个答案
type ans856 struct {
one int
}
func Test_Problem856(t *testing.T) {
qs := []question856{
question856{
para856{"()"},
ans856{1},
},
question856{
para856{"(())"},
ans856{2},
},
question856{
para856{"()()"},
ans856{2},
},
question856{
para856{"(()(()))"},
ans856{6},
},
question856{
para856{"()(())"},
ans856{3},
},
question856{
para856{"((()()))"},
ans856{8},
},
}
fmt.Printf("------------------------Leetcode Problem 856------------------------\n")
for _, q := range qs {
_, p := q.ans856, q.para856
fmt.Printf("【input】:%v 【output】:%v\n", p, scoreOfParentheses(p.one))
}
fmt.Printf("\n\n\n")
}

View File

@ -0,0 +1,54 @@
# [856. Score of Parentheses](https://leetcode.com/problems/score-of-parentheses/)
## 题目
Given a balanced parentheses string S, compute the score of the string based on the following rule:
() has score 1
AB has score A + B, where A and B are balanced parentheses strings.
(A) has score 2 * A, where A is a balanced parentheses string.
Example 1:
```c
Input: "()"
Output: 1
```
Example 2:
```c
Input: "(())"
Output: 2
```
Example 3:
```c
Input: "()()"
Output: 2
```
Example 4:
```c
Input: "(()(()))"
Output: 6
```
Note:
1. S is a balanced parentheses string, containing only ( and ).
2. 2 <= S.length <= 50
## 题目大意
按照以下规则计算括号的分数:() 代表 1 分。AB 代表 A + BA 和 B 分别是已经满足匹配规则的括号组。(A) 代表 2 * A其中 A 也是已经满足匹配规则的括号组。给出一个括号字符串,要求按照这些规则计算出括号的分数值。
## 解题思路
按照括号匹配的原则,一步步的计算每个组合的分数入栈。遇到题目中的 3 种情况,取出栈顶元素算分数。