Add solution 1178

This commit is contained in:
halfrost
2021-11-10 21:21:21 +08:00
committed by halfrost
parent ef2f198bd3
commit 79a5d9eba5
7 changed files with 48 additions and 42 deletions

View File

@ -479,7 +479,7 @@
|0338|Counting Bits|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0338.Counting-Bits)|72.0%|Easy||
|0339|Nested List Weight Sum||78.7%|Medium||
|0340|Longest Substring with At Most K Distinct Characters||46.7%|Medium||
|0341|Flatten Nested List Iterator|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0341.Flatten-Nested-List-Iterator)|57.6%|Medium||
|0341|Flatten Nested List Iterator|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0341.Flatten-Nested-List-Iterator)|57.5%|Medium||
|0342|Power of Four|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0342.Power-of-Four)|42.9%|Easy||
|0343|Integer Break|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0343.Integer-Break)|52.7%|Medium||
|0344|Reverse String|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0344.Reverse-String)|72.6%|Easy||
@ -674,7 +674,7 @@
|0533|Lonely Pixel II||48.3%|Medium||
|0534|Game Play Analysis III||81.1%|Medium||
|0535|Encode and Decode TinyURL|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0535.Encode-and-Decode-TinyURL)|83.1%|Medium||
|0536|Construct Binary Tree from String||54.4%|Medium||
|0536|Construct Binary Tree from String||54.5%|Medium||
|0537|Complex Number Multiplication|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0537.Complex-Number-Multiplication)|70.9%|Medium||
|0538|Convert BST to Greater Tree|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0538.Convert-BST-to-Greater-Tree)|61.7%|Medium||
|0539|Minimum Time Difference||53.0%|Medium||
@ -1002,7 +1002,7 @@
|0861|Score After Flipping Matrix||74.4%|Medium||
|0862|Shortest Subarray with Sum at Least K|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0862.Shortest-Subarray-with-Sum-at-Least-K)|25.9%|Hard||
|0863|All Nodes Distance K in Binary Tree|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0863.All-Nodes-Distance-K-in-Binary-Tree)|59.9%|Medium||
|0864|Shortest Path to Get All Keys|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0864.Shortest-Path-to-Get-All-Keys)|43.4%|Hard||
|0864|Shortest Path to Get All Keys|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0864.Shortest-Path-to-Get-All-Keys)|43.5%|Hard||
|0865|Smallest Subtree with all the Deepest Nodes||66.6%|Medium||
|0866|Prime Palindrome||25.3%|Medium||
|0867|Transpose Matrix|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0867.Transpose-Matrix)|61.4%|Easy||
@ -1134,7 +1134,7 @@
|0993|Cousins in Binary Tree|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0993.Cousins-in-Binary-Tree)|53.6%|Easy||
|0994|Rotting Oranges||51.1%|Medium||
|0995|Minimum Number of K Consecutive Bit Flips|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0995.Minimum-Number-of-K-Consecutive-Bit-Flips)|50.3%|Hard||
|0996|Number of Squareful Arrays|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0996.Number-of-Squareful-Arrays)|49.1%|Hard||
|0996|Number of Squareful Arrays|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0996.Number-of-Squareful-Arrays)|49.0%|Hard||
|0997|Find the Town Judge||49.9%|Easy||
|0998|Maximum Binary Tree II||64.8%|Medium||
|0999|Available Captures for Rook|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0999.Available-Captures-for-Rook)|67.6%|Easy||
@ -1406,7 +1406,7 @@
|1265|Print Immutable Linked List in Reverse||94.1%|Medium||
|1266|Minimum Time Visiting All Points|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1266.Minimum-Time-Visiting-All-Points)|79.2%|Easy||
|1267|Count Servers that Communicate||57.9%|Medium||
|1268|Search Suggestions System|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1268.Search-Suggestions-System)|65.8%|Medium||
|1268|Search Suggestions System|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1268.Search-Suggestions-System)|65.7%|Medium||
|1269|Number of Ways to Stay in the Same Place After Some Steps||43.3%|Hard||
|1270|All People Report to the Given Manager||88.2%|Medium||
|1271|Hexspeak||56.1%|Easy||
@ -1424,7 +1424,7 @@
|1283|Find the Smallest Divisor Given a Threshold|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1283.Find-the-Smallest-Divisor-Given-a-Threshold)|52.2%|Medium||
|1284|Minimum Number of Flips to Convert Binary Matrix to Zero Matrix||70.8%|Hard||
|1285|Find the Start and End Number of Continuous Ranges||88.3%|Medium||
|1286|Iterator for Combination||71.6%|Medium||
|1286|Iterator for Combination||71.7%|Medium||
|1287|Element Appearing More Than 25% In Sorted Array|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1287.Element-Appearing-More-Than-25-In-Sorted-Array)|59.7%|Easy||
|1288|Remove Covered Intervals||57.8%|Medium||
|1289|Minimum Falling Path Sum II||62.4%|Hard||
@ -1689,7 +1689,7 @@
|1548|The Most Similar Path in a Graph||56.8%|Hard||
|1549|The Most Recent Orders for Each Product||67.9%|Medium||
|1550|Three Consecutive Odds||64.1%|Easy||
|1551|Minimum Operations to Make Array Equal|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1551.Minimum-Operations-to-Make-Array-Equal)|80.6%|Medium||
|1551|Minimum Operations to Make Array Equal|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1551.Minimum-Operations-to-Make-Array-Equal)|80.7%|Medium||
|1552|Magnetic Force Between Two Balls||52.5%|Medium||
|1553|Minimum Number of Days to Eat N Oranges||32.1%|Hard||
|1554|Strings Differ by One Character||65.6%|Medium||
@ -1793,7 +1793,7 @@
|1652|Defuse the Bomb|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1652.Defuse-the-Bomb)|60.6%|Easy||
|1653|Minimum Deletions to Make String Balanced|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1653.Minimum-Deletions-to-Make-String-Balanced)|54.0%|Medium||
|1654|Minimum Jumps to Reach Home|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1654.Minimum-Jumps-to-Reach-Home)|25.7%|Medium||
|1655|Distribute Repeating Integers|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1655.Distribute-Repeating-Integers)|40.6%|Hard||
|1655|Distribute Repeating Integers|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1655.Distribute-Repeating-Integers)|40.7%|Hard||
|1656|Design an Ordered Stream|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1656.Design-an-Ordered-Stream)|82.6%|Easy||
|1657|Determine if Two Strings Are Close|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1657.Determine-if-Two-Strings-Are-Close)|54.7%|Medium||
|1658|Minimum Operations to Reduce X to Zero|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1658.Minimum-Operations-to-Reduce-X-to-Zero)|33.3%|Medium||
@ -2179,7 +2179,7 @@
|2038|Remove Colored Pieces if Both Neighbors are the Same Color||53.6%|Medium||
|2039|The Time When the Network Becomes Idle||47.0%|Medium||
|2040|Kth Smallest Product of Two Sorted Arrays||23.5%|Hard||
|2041|Accepted Candidates From the Interviews||75.3%|Medium||
|2041|Accepted Candidates From the Interviews||75.4%|Medium||
|2042|Check if Numbers Are Ascending in a Sentence||70.6%|Easy||
|2043|Simple Bank System||64.0%|Medium||
|2044|Count Number of Maximum Bitwise-OR Subsets||74.6%|Medium||
@ -2205,16 +2205,16 @@
|2064|Minimized Maximum of Products Distributed to Any Store||43.5%|Medium||
|2065|Maximum Path Quality of a Graph||56.1%|Hard||
|2066|Account Balance||84.5%|Medium||
|2067|Number of Equal Count Substrings||64.2%|Medium||
|2067|Number of Equal Count Substrings||64.0%|Medium||
|2068|Check Whether Two Strings are Almost Equivalent||67.8%|Easy||
|2069|Walking Robot Simulation II||15.5%|Medium||
|2070|Most Beautiful Item for Each Query||42.6%|Medium||
|2071|Maximum Number of Tasks You Can Assign||29.4%|Hard||
|2072|The Winner University||91.9%|Easy||
|2073|Time Needed to Buy Tickets||56.0%|Easy||
|2074|Reverse Nodes in Even Length Groups||38.3%|Medium||
|2075|Decode the Slanted Ciphertext||46.5%|Medium||
|2076|Process Restricted Friend Requests||37.7%|Hard||
|2072|The Winner University||92.1%|Easy||
|2073|Time Needed to Buy Tickets||56.2%|Easy||
|2074|Reverse Nodes in Even Length Groups||38.7%|Medium||
|2075|Decode the Slanted Ciphertext||46.7%|Medium||
|2076|Process Restricted Friend Requests||38.5%|Hard||
|------------|-------------------------------------------------------|-------| ----------------| ---------------|-------------|
------------------------------------------------------------------

View File

@ -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]对应bit0
//不包含 puzzles[0],即 puzzles[0] 对应 bit0
findNum(puzzles[1:], bitMap, totalNum, m)
//包含puzzles[0],即puzzles[0]对应bit1
//包含 puzzles[0],即 puzzles[0] 对应 bit1
bitMap |= (1 << (puzzles[0] - 'a'))
findNum(puzzles[1:], bitMap, totalNum, m)
bitMap ^= (1 << (puzzles[0] - 'a')) //异或 清零

View File

@ -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]对应bit0
//不包含 puzzles[0],即 puzzles[0] 对应 bit0
findNum(puzzles[1:], bitMap, totalNum, m)
//包含puzzles[0],即puzzles[0]对应bit1
//包含 puzzles[0],即 puzzles[0] 对应 bit1
bitMap |= (1 << (puzzles[0] - 'a'))
findNum(puzzles[1:], bitMap, totalNum, m)
bitMap ^= (1 << (puzzles[0] - 'a')) //异或 清零
return
}
```

View File

@ -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,20 +100,22 @@ func toBitMap(word []byte) uint32 {
return res
}
//利用dfs 搜索 puzzles的子空间
//利用 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]对应bit0
//不包含 puzzles[0],即 puzzles[0] 对应 bit0
findNum(puzzles[1:], bitMap, totalNum, m)
//包含puzzles[0],即puzzles[0]对应bit1
//包含 puzzles[0],即 puzzles[0] 对应 bit1
bitMap |= (1 << (puzzles[0] - 'a'))
findNum(puzzles[1:], bitMap, totalNum, m)
bitMap ^= (1 << (puzzles[0] - 'a')) //异或 清零
return
}
```

View File

@ -82,7 +82,7 @@ X & ~X = 0
|0762|Prime Number of Set Bits in Binary Representation|[Go]({{< relref "/ChapterFour/0700~0799/0762.Prime-Number-of-Set-Bits-in-Binary-Representation.md" >}})|Easy| O(n)| O(1)||65.8%|
|0784|Letter Case Permutation|[Go]({{< relref "/ChapterFour/0700~0799/0784.Letter-Case-Permutation.md" >}})|Medium| O(n)| O(1)||70.6%|
|0810|Chalkboard XOR Game|[Go]({{< relref "/ChapterFour/0800~0899/0810.Chalkboard-XOR-Game.md" >}})|Hard||||51.9%|
|0864|Shortest Path to Get All Keys|[Go]({{< relref "/ChapterFour/0800~0899/0864.Shortest-Path-to-Get-All-Keys.md" >}})|Hard||||43.4%|
|0864|Shortest Path to Get All Keys|[Go]({{< relref "/ChapterFour/0800~0899/0864.Shortest-Path-to-Get-All-Keys.md" >}})|Hard||||43.5%|
|0898|Bitwise ORs of Subarrays|[Go]({{< relref "/ChapterFour/0800~0899/0898.Bitwise-ORs-of-Subarrays.md" >}})|Medium| O(n)| O(1)||36.2%|
|0980|Unique Paths III|[Go]({{< relref "/ChapterFour/0900~0999/0980.Unique-Paths-III.md" >}})|Hard||||79.2%|
|0995|Minimum Number of K Consecutive Bit Flips|[Go]({{< relref "/ChapterFour/0900~0999/0995.Minimum-Number-of-K-Consecutive-Bit-Flips.md" >}})|Hard||||50.3%|

View File

@ -65,7 +65,7 @@ weight: 10
|0839|Similar String Groups|[Go]({{< relref "/ChapterFour/0800~0899/0839.Similar-String-Groups.md" >}})|Hard||||44.1%|
|0841|Keys and Rooms|[Go]({{< relref "/ChapterFour/0800~0899/0841.Keys-and-Rooms.md" >}})|Medium||||67.8%|
|0863|All Nodes Distance K in Binary Tree|[Go]({{< relref "/ChapterFour/0800~0899/0863.All-Nodes-Distance-K-in-Binary-Tree.md" >}})|Medium||||59.9%|
|0864|Shortest Path to Get All Keys|[Go]({{< relref "/ChapterFour/0800~0899/0864.Shortest-Path-to-Get-All-Keys.md" >}})|Hard||||43.4%|
|0864|Shortest Path to Get All Keys|[Go]({{< relref "/ChapterFour/0800~0899/0864.Shortest-Path-to-Get-All-Keys.md" >}})|Hard||||43.5%|
|0909|Snakes and Ladders|[Go]({{< relref "/ChapterFour/0900~0999/0909.Snakes-and-Ladders.md" >}})|Medium||||39.7%|
|0924|Minimize Malware Spread|[Go]({{< relref "/ChapterFour/0900~0999/0924.Minimize-Malware-Spread.md" >}})|Hard||||41.8%|
|0928|Minimize Malware Spread II|[Go]({{< relref "/ChapterFour/0900~0999/0928.Minimize-Malware-Spread-II.md" >}})|Hard||||41.8%|

View File

@ -124,7 +124,7 @@ weight: 12
|1442|Count Triplets That Can Form Two Arrays of Equal XOR|[Go]({{< relref "/ChapterFour/1400~1499/1442.Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR.md" >}})|Medium||||73.8%|
|1486|XOR Operation in an Array|[Go]({{< relref "/ChapterFour/1400~1499/1486.XOR-Operation-in-an-Array.md" >}})|Easy||||84.0%|
|1512|Number of Good Pairs|[Go]({{< relref "/ChapterFour/1500~1599/1512.Number-of-Good-Pairs.md" >}})|Easy||||87.7%|
|1551|Minimum Operations to Make Array Equal|[Go]({{< relref "/ChapterFour/1500~1599/1551.Minimum-Operations-to-Make-Array-Equal.md" >}})|Medium||||80.6%|
|1551|Minimum Operations to Make Array Equal|[Go]({{< relref "/ChapterFour/1500~1599/1551.Minimum-Operations-to-Make-Array-Equal.md" >}})|Medium||||80.7%|
|1573|Number of Ways to Split a String|[Go]({{< relref "/ChapterFour/1500~1599/1573.Number-of-Ways-to-Split-a-String.md" >}})|Medium||||31.4%|
|1648|Sell Diminishing-Valued Colored Balls|[Go]({{< relref "/ChapterFour/1600~1699/1648.Sell-Diminishing-Valued-Colored-Balls.md" >}})|Medium||||31.5%|
|1680|Concatenation of Consecutive Binary Numbers|[Go]({{< relref "/ChapterFour/1600~1699/1680.Concatenation-of-Consecutive-Binary-Numbers.md" >}})|Medium||||52.5%|