添加 problem 721

This commit is contained in:
YDZ
2019-08-03 21:59:45 +08:00
parent ffd6ee356d
commit 91a574c908
2 changed files with 161 additions and 0 deletions

View File

@ -0,0 +1,88 @@
package leetcode
import "sort"
// 解法一 并查集优化搜索解法
func accountsMerge(accounts [][]string) (r [][]string) {
uf := UnionFind{}
uf.init(len(accounts))
// emailToID 将所有的 email 邮箱都拆开,拆开与 id(数组下标) 对应
// idToName 将 id(数组下标) 与 name 对应
// idToEmails 将 id(数组下标) 与整理好去重以后的 email 组对应
emailToID, idToName, idToEmails, res := make(map[string]int), make(map[int]string), make(map[int][]string), [][]string{}
for id, acc := range accounts {
idToName[id] = acc[0]
for i := 1; i < len(acc); i++ {
pid, ok := emailToID[acc[i]]
if ok {
uf.union(id, pid)
}
emailToID[acc[i]] = id
}
}
for email, id := range emailToID {
pid := uf.find(id)
idToEmails[pid] = append(idToEmails[pid], email)
}
for id, emails := range idToEmails {
name := idToName[id]
sort.Strings(emails)
res = append(res, append([]string{name}, emails...))
}
return res
}
// 解法二 并查集暴力解法
func accountsMerge1(accounts [][]string) [][]string {
if len(accounts) == 0 {
return [][]string{}
}
uf, res, visited := UnionFind{}, [][]string{}, map[int]bool{}
uf.init(len(accounts))
for i := 0; i < len(accounts); i++ {
for j := i + 1; j < len(accounts); j++ {
if accounts[i][0] == accounts[j][0] {
tmpA, tmpB, flag := accounts[i][1:], accounts[j][1:], false
for j := 0; j < len(tmpA); j++ {
for k := 0; k < len(tmpB); k++ {
if tmpA[j] == tmpB[k] {
flag = true
break
}
}
if flag {
break
}
}
if flag {
uf.union(i, j)
}
}
}
}
for i := 0; i < len(accounts); i++ {
if visited[i] {
continue
}
emails, account, tmpMap := accounts[i][1:], []string{accounts[i][0]}, map[string]string{}
for j := i + 1; j < len(accounts); j++ {
if uf.find(j) == uf.find(i) {
visited[j] = true
for _, v := range accounts[j][1:] {
tmpMap[v] = v
}
}
}
for _, v := range emails {
tmpMap[v] = v
}
emails = []string{}
for key := range tmpMap {
emails = append(emails, key)
}
sort.Strings(emails)
account = append(account, emails...)
res = append(res, account)
}
return res
}

View File

@ -0,0 +1,73 @@
package leetcode
import (
"fmt"
"testing"
)
type question721 struct {
para721
ans721
}
// para 是参数
// one 代表第一个参数
type para721 struct {
w [][]string
}
// ans 是答案
// one 代表第一个答案
type ans721 struct {
one [][]string
}
func Test_Problem721(t *testing.T) {
qs := []question721{
question721{
para721{[][]string{[]string{"John", "johnsmith@mail.com", "john00@mail.com"},
[]string{"John", "johnnybravo@mail.com"},
[]string{"John", "johnsmith@mail.com", "john_newyork@mail.com"},
[]string{"Mary", "mary@mail.com"}}},
ans721{[][]string{[]string{"John", "john00@mail.com", "john_newyork@mail.com", "johnsmith@mail.com"},
[]string{"John", "johnnybravo@mail.com"},
[]string{"Mary", "mary@mail.com"}}},
},
question721{
para721{[][]string{[]string{"Alex", "Alex5@m.co", "Alex4@m.co", "Alex0@m.co"},
[]string{"Ethan", "Ethan3@m.co", "Ethan3@m.co", "Ethan0@m.co"},
[]string{"Kevin", "Kevin4@m.co", "Kevin2@m.co", "Kevin2@m.co"},
[]string{"Gabe", "Gabe0@m.co", "Gabe3@m.co", "Gabe2@m.co"},
[]string{"Gabe", "Gabe3@m.co", "Gabe4@m.co", "Gabe2@m.co"}}},
ans721{[][]string{[]string{"Alex", "Alex0@m.co", "Alex4@m.co", "Alex5@m.co"},
[]string{"Ethan", "Ethan0@m.co", "Ethan3@m.co"},
[]string{"Gabe", "Gabe0@m.co", "Gabe2@m.co", "Gabe3@m.co", "Gabe4@m.co"},
[]string{"Kevin", "Kevin2@m.co", "Kevin4@m.co"}}},
},
question721{
para721{[][]string{[]string{"David", "David0@m.co", "David4@m.co", "David3@m.co"},
[]string{"David", "David5@m.co", "David5@m.co", "David0@m.co"},
[]string{"David", "David1@m.co", "David4@m.co", "David0@m.co"},
[]string{"David", "David0@m.co", "David1@m.co", "David3@m.co"},
[]string{"David", "David4@m.co", "David1@m.co", "David3@m.co"}}},
ans721{[][]string{[]string{"David", "David0@m.co", "David1@m.co", "David3@m.co", "David4@m.co", "David5@m.co"}}},
},
question721{
para721{[][]string{}},
ans721{[][]string{}},
},
}
fmt.Printf("------------------------Leetcode Problem 721------------------------\n")
for _, q := range qs {
_, p := q.ans721, q.para721
fmt.Printf("【input】:%v 【output】:%v\n", p, accountsMerge(p.w))
}
fmt.Printf("\n\n\n")
}