添加 problem 839

This commit is contained in:
YDZ
2019-08-02 20:08:09 +08:00
parent 4f3e7e5b98
commit 3e0e34e4d9
2 changed files with 69 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
package leetcode
func numSimilarGroups(A []string) int {
uf := UnionFind{}
uf.init(len(A))
for i := 0; i < len(A); i++ {
for j := i + 1; j < len(A); j++ {
if isSimilar(A[i], A[j]) {
uf.union(i, j)
}
}
}
return uf.totalCount()
}
func isSimilar(a, b string) bool {
var n int
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
n++
if n > 2 {
return false
}
}
}
return true
}

View File

@@ -0,0 +1,42 @@
package leetcode
import (
"fmt"
"testing"
)
type question839 struct {
para839
ans839
}
// para 是参数
// one 代表第一个参数
type para839 struct {
one []string
}
// ans 是答案
// one 代表第一个答案
type ans839 struct {
one int
}
func Test_Problem839(t *testing.T) {
qs := []question839{
question839{
para839{[]string{"tars", "rats", "arts", "star"}},
ans839{2},
},
}
fmt.Printf("------------------------Leetcode Problem 839------------------------\n")
for _, q := range qs {
_, p := q.ans839, q.para839
fmt.Printf("【input】:%v 【output】:%v\n", p, numSimilarGroups(p.one))
}
fmt.Printf("\n\n\n")
}