mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2026-03-13 10:02:05 +08:00
添加 problem 839
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
Reference in New Issue
Block a user