Skip to content

Commit

Permalink
完成Valid-Parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
Autumn-qy committed Aug 31, 2022
1 parent 1a5a5f4 commit e64115c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
20 changes: 20 additions & 0 deletions 5-kyu/Valid-Parentheses/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Multiples of 3 or 5

### 任务详情

Write a function that takes a string of parentheses, and determines if the order of the parentheses is valid. The function should return true if the string is valid, and false if it's invalid.

### 示例


```golang
"()" => true
")(()))" => false
"(" => false
"(())((()())())" => true
```


### 任务链接

https://www.codewars.com/kata/52774a314c2333f0a7000688
3 changes: 3 additions & 0 deletions 5-kyu/Valid-Parentheses/src/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module src

go 1.18
31 changes: 31 additions & 0 deletions 5-kyu/Valid-Parentheses/src/valid_parentheses.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package src

import (
"strings"
)

//第一种
func ValidParentheses(parens string) bool {
// Your code goes here
for strings.Contains(parens, "()") {
parens = strings.Replace(parens, "()", "", -1)
}
return len(parens) == 0
}

//第二种
func ValidParentheses2(parens string) bool {
// Your code goes here
count := 0
for _, c := range parens {
if c == '(' {
count++
} else if c == ')' {
count--
}
if count < 0 {
return false
}
}
return count == 0
}
24 changes: 24 additions & 0 deletions 5-kyu/Valid-Parentheses/src/valid_parentheses_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package src

import "testing"

func TestValidParentheses(t *testing.T) {
type args struct {
parens string
}
tests := []struct {
name string
args args
want bool
}{
{"", args{"()"}, true},
{"", args{"())"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ValidParentheses(tt.args.parens); got != tt.want {
t.Errorf("Multiple3And5() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
#### [7 kyu](https://github.com/zhaoqy1/codewars/tree/master/7-kyu)

#### [6 kyu](https://github.com/zhaoqy1/codewars/tree/master/6-kyu)

#### [5 kyu](https://github.com/zhaoqy1/codewars/tree/master/5-kyu)

0 comments on commit e64115c

Please sign in to comment.