mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2026-03-13 10:02:05 +08:00
添加 problem 49
This commit is contained in:
32
Algorithms/0049. Group Anagrams/49. Group Anagrams.go
Normal file
32
Algorithms/0049. Group Anagrams/49. Group Anagrams.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package leetcode
|
||||
|
||||
import "sort"
|
||||
|
||||
type sortRunes []rune
|
||||
|
||||
func (s sortRunes) Less(i, j int) bool {
|
||||
return s[i] < s[j]
|
||||
}
|
||||
|
||||
func (s sortRunes) Swap(i, j int) {
|
||||
s[i], s[j] = s[j], s[i]
|
||||
}
|
||||
|
||||
func (s sortRunes) Len() int {
|
||||
return len(s)
|
||||
}
|
||||
|
||||
func groupAnagrams(strs []string) [][]string {
|
||||
record, res := map[string][]string{}, [][]string{}
|
||||
for _, str := range strs {
|
||||
sByte := []rune(str)
|
||||
sort.Sort(sortRunes(sByte))
|
||||
sstrs := record[string(sByte)]
|
||||
sstrs = append(sstrs, str)
|
||||
record[string(sByte)] = sstrs
|
||||
}
|
||||
for _, v := range record {
|
||||
res = append(res, v)
|
||||
}
|
||||
return res
|
||||
}
|
||||
41
Algorithms/0049. Group Anagrams/49. Group Anagrams_test.go
Normal file
41
Algorithms/0049. Group Anagrams/49. Group Anagrams_test.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question49 struct {
|
||||
para49
|
||||
ans49
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para49 struct {
|
||||
one []string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans49 struct {
|
||||
one [][]string
|
||||
}
|
||||
|
||||
func Test_Problem49(t *testing.T) {
|
||||
|
||||
qs := []question49{
|
||||
|
||||
question49{
|
||||
para49{[]string{"eat", "tea", "tan", "ate", "nat", "bat"}},
|
||||
ans49{[][]string{[]string{"ate", "eat", "tea"}, []string{"nat", "tan"}, []string{"bat"}}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 49------------------------\n")
|
||||
for _, q := range qs {
|
||||
_, p := q.ans49, q.para49
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, groupAnagrams(p.one))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
31
Algorithms/0049. Group Anagrams/README.md
Normal file
31
Algorithms/0049. Group Anagrams/README.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# [49. Group Anagrams](https://leetcode.com/problems/group-anagrams/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given an array of strings, group anagrams together.
|
||||
|
||||
|
||||
Example:
|
||||
|
||||
```c
|
||||
Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
|
||||
Output:
|
||||
[
|
||||
["ate","eat","tea"],
|
||||
["nat","tan"],
|
||||
["bat"]
|
||||
]
|
||||
```
|
||||
|
||||
Note:
|
||||
|
||||
- All inputs will be in lowercase.
|
||||
- The order of your output does not matter.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给出一个字符串数组,要求对字符串数组里面有 Anagrams 关系的字符串进行分组。Anagrams 关系是指两个字符串的字符完全相同,顺序不同,两者是由排列组合组成。
|
||||
|
||||
## 解题思路
|
||||
|
||||
这道题可以将每个字符串都排序,排序完成以后,相同 Anagrams 的字符串必然排序结果一样。把排序以后的字符串当做 key 存入到 map 中。遍历数组以后,就能得到一个 map,key 是排序以后的字符串,value 对应的是这个排序字符串以后的 Anagrams 字符串集合。最后再将这些 value 对应的字符串数组输出即可。
|
||||
Reference in New Issue
Block a user