mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-07 09:54:57 +08:00
Add solution 1178
This commit is contained in:
@ -1,10 +1,10 @@
|
||||
package leetcode
|
||||
|
||||
/*
|
||||
匹配跟单词中的字母顺序,字母个数都无关,可以用bitmap压缩
|
||||
1. 记录word中 利用map记录各种bit标示的个数
|
||||
2. puzzles 中各个字母都不相同! 记录bitmap,然后搜索子空间中各种bit标识的个数的和
|
||||
因为puzzles长度最长是7,所以搜索空间 2^7
|
||||
匹配跟单词中的字母顺序,字母个数都无关,可以用 bitmap 压缩
|
||||
1. 记录 word 中 利用 map 记录各种 bit 标示的个数
|
||||
2. puzzles 中各个字母都不相同! 记录 bitmap,然后搜索子空间中各种 bit 标识的个数的和
|
||||
因为 puzzles 长度最长是7,所以搜索空间 2^7
|
||||
*/
|
||||
func findNumOfValidWords(words []string, puzzles []string) []int {
|
||||
wordBitStatusMap, res := make(map[uint32]int, 0), []int{}
|
||||
@ -14,7 +14,7 @@ func findNumOfValidWords(words []string, puzzles []string) []int {
|
||||
for _, p := range puzzles {
|
||||
var bitMap uint32
|
||||
var totalNum int
|
||||
bitMap |= (1 << (p[0] - 'a')) //work中要包含 p 的第一个字母 所以这个bit位上必须是1
|
||||
bitMap |= (1 << (p[0] - 'a')) //work 中要包含 p 的第一个字母 所以这个 bit 位上必须是 1
|
||||
findNum([]byte(p)[1:], bitMap, &totalNum, wordBitStatusMap)
|
||||
res = append(res, totalNum)
|
||||
}
|
||||
@ -29,15 +29,15 @@ func toBitMap(word []byte) uint32 {
|
||||
return res
|
||||
}
|
||||
|
||||
//利用dfs 搜索 pussles的子空间
|
||||
//利用 dfs 搜索 puzzles 的子空间
|
||||
func findNum(puzzles []byte, bitMap uint32, totalNum *int, m map[uint32]int) {
|
||||
if len(puzzles) == 0 {
|
||||
*totalNum = *totalNum + m[bitMap]
|
||||
return
|
||||
}
|
||||
//不包含puzzles[0],即puzzles[0]对应bit是0
|
||||
//不包含 puzzles[0],即 puzzles[0] 对应 bit 是 0
|
||||
findNum(puzzles[1:], bitMap, totalNum, m)
|
||||
//包含puzzles[0],即puzzles[0]对应bit是1
|
||||
//包含 puzzles[0],即 puzzles[0] 对应 bit 是 1
|
||||
bitMap |= (1 << (puzzles[0] - 'a'))
|
||||
findNum(puzzles[1:], bitMap, totalNum, m)
|
||||
bitMap ^= (1 << (puzzles[0] - 'a')) //异或 清零
|
||||
|
@ -68,13 +68,14 @@ There're no valid words for "gaswxyz" cause none of the words in the list cont
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
/*
|
||||
匹配跟单词中的字母顺序,字母个数都无关,可以用bitmap压缩
|
||||
1. 记录word中 利用map记录各种bit标示的个数
|
||||
2. puzzles 中各个字母都不相同! 记录bitmap,然后搜索子空间中各种bit标识的个数的和
|
||||
因为puzzles长度最长是7,所以搜索空间 2^7
|
||||
匹配跟单词中的字母顺序,字母个数都无关,可以用 bitmap 压缩
|
||||
1. 记录 word 中 利用 map 记录各种 bit 标示的个数
|
||||
2. puzzles 中各个字母都不相同! 记录 bitmap,然后搜索子空间中各种 bit 标识的个数的和
|
||||
因为 puzzles 长度最长是7,所以搜索空间 2^7
|
||||
*/
|
||||
func findNumOfValidWords(words []string, puzzles []string) []int {
|
||||
wordBitStatusMap, res := make(map[uint32]int, 0), []int{}
|
||||
@ -84,7 +85,7 @@ func findNumOfValidWords(words []string, puzzles []string) []int {
|
||||
for _, p := range puzzles {
|
||||
var bitMap uint32
|
||||
var totalNum int
|
||||
bitMap |= (1 << (p[0] - 'a')) //work中要包含 p 的第一个字母 所以这个bit位上必须是1
|
||||
bitMap |= (1 << (p[0] - 'a')) //work 中要包含 p 的第一个字母 所以这个 bit 位上必须是 1
|
||||
findNum([]byte(p)[1:], bitMap, &totalNum, wordBitStatusMap)
|
||||
res = append(res, totalNum)
|
||||
}
|
||||
@ -99,18 +100,20 @@ func toBitMap(word []byte) uint32 {
|
||||
return res
|
||||
}
|
||||
|
||||
//利用dfs 搜索 pussles的子空间
|
||||
//利用 dfs 搜索 puzzles 的子空间
|
||||
func findNum(puzzles []byte, bitMap uint32, totalNum *int, m map[uint32]int) {
|
||||
if len(puzzles) == 0 {
|
||||
*totalNum = *totalNum + m[bitMap]
|
||||
return
|
||||
}
|
||||
//不包含puzzles[0],即puzzles[0]对应bit是0
|
||||
//不包含 puzzles[0],即 puzzles[0] 对应 bit 是 0
|
||||
findNum(puzzles[1:], bitMap, totalNum, m)
|
||||
//包含puzzles[0],即puzzles[0]对应bit是1
|
||||
//包含 puzzles[0],即 puzzles[0] 对应 bit 是 1
|
||||
bitMap |= (1 << (puzzles[0] - 'a'))
|
||||
findNum(puzzles[1:], bitMap, totalNum, m)
|
||||
bitMap ^= (1 << (puzzles[0] - 'a')) //异或 清零
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
```
|
Reference in New Issue
Block a user