-
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
5 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
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,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 |
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,3 @@ | ||
module src | ||
|
||
go 1.18 |
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,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 | ||
} |
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,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) | ||
} | ||
}) | ||
} | ||
} |
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