mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-24 10:37:33 +08:00
Update README
This commit is contained in:
@ -135,5 +135,5 @@ func romanToInt(s string) int {
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0012.Integer-to-Roman/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0015.3Sum/">下一页➡️</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0014.Longest-Common-Prefix/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
@ -0,0 +1,71 @@
|
||||
# [14. Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/)
|
||||
|
||||
## 题目
|
||||
|
||||
Write a function to find the longest common prefix string amongst an array of strings.
|
||||
|
||||
If there is no common prefix, return an empty string "".
|
||||
|
||||
**Example 1**:
|
||||
|
||||
Input: strs = ["flower","flow","flight"]
|
||||
Output: "fl"
|
||||
|
||||
**Example 2**:
|
||||
|
||||
Input: strs = ["dog","racecar","car"]
|
||||
Output: ""
|
||||
Explanation: There is no common prefix among the input strings.
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- 1 <= strs.length <= 200
|
||||
- 0 <= strs[i].length <= 200
|
||||
- strs[i] consists of only lower-case English letters.
|
||||
|
||||
## 题目大意
|
||||
|
||||
编写一个函数来查找字符串数组中的最长公共前缀。
|
||||
|
||||
如果不存在公共前缀,返回空字符串 ""。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 对 strs 按照字符串长度进行升序排序,求出 strs 中长度最小字符串的长度 minLen
|
||||
- 逐个比较长度最小字符串与其它字符串中的字符,如果不相等就返回 commonPrefix,否则就把该字符加入 commonPrefix
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
import "sort"
|
||||
|
||||
func longestCommonPrefix(strs []string) string {
|
||||
sort.Slice(strs, func(i, j int) bool {
|
||||
return len(strs[i]) <= len(strs[j])
|
||||
})
|
||||
minLen := len(strs[0])
|
||||
if minLen == 0 {
|
||||
return ""
|
||||
}
|
||||
var commonPrefix []byte
|
||||
for i := 0; i < minLen; i++ {
|
||||
for j := 1; j < len(strs); j++ {
|
||||
if strs[j][i] != strs[0][i] {
|
||||
return string(commonPrefix)
|
||||
}
|
||||
}
|
||||
commonPrefix = append(commonPrefix, strs[0][i])
|
||||
}
|
||||
return string(commonPrefix)
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0013.Roman-to-Integer/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0015.3Sum/">下一页➡️</a></p>
|
||||
</div>
|
@ -120,6 +120,6 @@ func threeSum1(nums []int) [][]int {
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0013.Roman-to-Integer/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0014.Longest-Common-Prefix/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0016.3Sum-Closest/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
@ -80,5 +80,5 @@ func maxProduct318(words []string) int {
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0315.Count-of-Smaller-Numbers-After-Self/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0322.Coin-Change/">下一页➡️</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0319.Bulb-Switcher/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
65
website/content/ChapterFour/0300~0399/0319.Bulb-Switcher.md
Normal file
65
website/content/ChapterFour/0300~0399/0319.Bulb-Switcher.md
Normal file
@ -0,0 +1,65 @@
|
||||
# [319. Bulb Switcher](https://leetcode.com/problems/bulb-switcher/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
There are n bulbs that are initially off. You first turn on all the bulbs, then you turn off every second bulb.
|
||||
|
||||
On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the ith round, you toggle every i bulb. For the nth round, you only toggle the last bulb.
|
||||
|
||||
Return the number of bulbs that are on after n rounds.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
Input: n = 3
|
||||
Output: 1
|
||||
Explanation: At first, the three bulbs are [off, off, off].
|
||||
After the first round, the three bulbs are [on, on, on].
|
||||
After the second round, the three bulbs are [on, off, on].
|
||||
After the third round, the three bulbs are [on, off, off].
|
||||
So you should return 1 because there is only one bulb is on.
|
||||
|
||||
**Example 2:**
|
||||
|
||||
Input: n = 0
|
||||
Output: 0
|
||||
|
||||
**Example 3:**
|
||||
|
||||
Input: n = 1
|
||||
Output: 1
|
||||
|
||||
## 题目大意
|
||||
|
||||
初始时有 n 个灯泡处于关闭状态。第一轮,你将会打开所有灯泡。接下来的第二轮,你将会每两个灯泡关闭一个。
|
||||
|
||||
第三轮,你每三个灯泡就切换一个灯泡的开关(即,打开变关闭,关闭变打开)。第 i 轮,你每 i 个灯泡就切换一个灯泡的开关。直到第 n 轮,你只需要切换最后一个灯泡的开关。
|
||||
|
||||
找出并返回 n 轮后有多少个亮着的灯泡。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 计算 1 到 n 中有奇数个约数的个数
|
||||
- 1 到 n 中的某个数 x 有奇数个约数,也即 x 是完全平方数
|
||||
- 计算 1 到 n 中完全平方数的个数 sqrt(n)
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
import "math"
|
||||
|
||||
func bulbSwitch(n int) int {
|
||||
return int(math.Sqrt(float64(n)))
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0318.Maximum-Product-of-Word-Lengths/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0322.Coin-Change/">下一页➡️</a></p>
|
||||
</div>
|
@ -63,6 +63,6 @@ func coinChange(coins []int, amount int) int {
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0318.Maximum-Product-of-Word-Lengths/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0319.Bulb-Switcher/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0324.Wiggle-Sort-II/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
@ -138,5 +138,5 @@ func (p *pq) Pop() interface{} {
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0377.Combination-Sum-IV/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0385.Mini-Parser/">下一页➡️</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0384.Shuffle-an-Array/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
@ -0,0 +1,89 @@
|
||||
# [384.Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given an integer array nums, design an algorithm to randomly shuffle the array. All permutations of the array should be equally likely as a result of the shuffling.
|
||||
|
||||
Implement the Solution class:
|
||||
|
||||
- Solution(int[] nums) Initializes the object with the integer array nums.
|
||||
- int[] reset() Resets the array to its original configuration and returns it.
|
||||
- int[] shuffle() Returns a random shuffling of the array.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
Input
|
||||
["Solution", "shuffle", "reset", "shuffle"]
|
||||
[[[1, 2, 3]], [], [], []]
|
||||
Output
|
||||
[null, [3, 1, 2], [1, 2, 3], [1, 3, 2]]
|
||||
|
||||
Explanation
|
||||
Solution solution = new Solution([1, 2, 3]);
|
||||
solution.shuffle(); // Shuffle the array [1,2,3] and return its result.
|
||||
// Any permutation of [1,2,3] must be equally likely to be returned.
|
||||
// Example: return [3, 1, 2]
|
||||
solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3]
|
||||
solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2]
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- 1 <= nums.length <= 200
|
||||
- -1000000 <= nums[i] <= 1000000
|
||||
- All the elements of nums are unique.
|
||||
- At most 5 * 10000 calls in total will be made to reset and shuffle.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个整数数组 nums ,设计算法来打乱一个没有重复元素的数组。
|
||||
|
||||
实现 Solution class:
|
||||
|
||||
- Solution(int[] nums) 使用整数数组 nums 初始化对象
|
||||
- int[] reset() 重设数组到它的初始状态并返回
|
||||
- int[] shuffle() 返回数组随机打乱后的结果
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 使用 rand.Shuffle 进行数组随机打乱
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
import "math/rand"
|
||||
|
||||
type Solution struct {
|
||||
nums []int
|
||||
}
|
||||
|
||||
func Constructor(nums []int) Solution {
|
||||
return Solution{
|
||||
nums: nums,
|
||||
}
|
||||
}
|
||||
|
||||
/** Resets the array to its original configuration and return it. */
|
||||
func (this *Solution) Reset() []int {
|
||||
return this.nums
|
||||
}
|
||||
|
||||
/** Returns a random shuffling of the array. */
|
||||
func (this *Solution) Shuffle() []int {
|
||||
arr := make([]int, len(this.nums))
|
||||
copy(arr, this.nums)
|
||||
rand.Shuffle(len(arr), func(i, j int) {
|
||||
arr[i], arr[j] = arr[j], arr[i]
|
||||
})
|
||||
return arr
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0378.Kth-Smallest-Element-in-a-Sorted-Matrix/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0385.Mini-Parser/">下一页➡️</a></p>
|
||||
</div>
|
@ -177,6 +177,6 @@ func deserialize(s string) *NestedInteger {
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0378.Kth-Smallest-Element-in-a-Sorted-Matrix/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0384.Shuffle-an-Array/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0386.Lexicographical-Numbers/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
@ -51,5 +51,5 @@ func findTheDifference(s string, t string) byte {
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0387.First-Unique-Character-in-a-String/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0392.Is-Subsequence/">下一页➡️</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0391.Perfect-Rectangle/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
121
website/content/ChapterFour/0300~0399/0391.Perfect-Rectangle.md
Normal file
121
website/content/ChapterFour/0300~0399/0391.Perfect-Rectangle.md
Normal file
@ -0,0 +1,121 @@
|
||||
# [391. Perfect Rectangle](https://leetcode.com/problems/perfect-rectangle/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given an array rectangles where rectangles[i] = [xi, yi, ai, bi] represents an axis-aligned rectangle. The bottom-left point of the rectangle is (xi, yi) and the top-right point of it is (ai, bi).
|
||||
|
||||
Return true if all the rectangles together form an exact cover of a rectangular region.
|
||||
|
||||
**Example1:**
|
||||
|
||||

|
||||
|
||||
Input: rectangles = [[1,1,3,3],[3,1,4,2],[3,2,4,4],[1,3,2,4],[2,3,3,4]]
|
||||
Output: true
|
||||
Explanation: All 5 rectangles together form an exact cover of a rectangular region.
|
||||
|
||||
**Example2:**
|
||||
|
||||

|
||||
|
||||
Input: rectangles = [[1,1,2,3],[1,3,2,4],[3,1,4,2],[3,2,4,4]]
|
||||
Output: false
|
||||
Explanation: Because there is a gap between the two rectangular regions.
|
||||
|
||||
**Example3:**
|
||||
|
||||

|
||||
|
||||
Input: rectangles = [[1,1,3,3],[3,1,4,2],[1,3,2,4],[3,2,4,4]]
|
||||
Output: false
|
||||
Explanation: Because there is a gap in the top center.
|
||||
|
||||
**Example4:**
|
||||
|
||||

|
||||
|
||||
Input: rectangles = [[1,1,3,3],[3,1,4,2],[1,3,2,4],[2,2,4,4]]
|
||||
Output: false
|
||||
Explanation: Because two of the rectangles overlap with each other.
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- 1 <= rectangles.length <= 2 * 10000
|
||||
- rectangles[i].length == 4
|
||||
- -100000 <= xi, yi, ai, bi <= 100000
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个数组 rectangles ,其中 rectangles[i] = [xi, yi, ai, bi] 表示一个坐标轴平行的矩形。这个矩形的左下顶点是 (xi, yi) ,右上顶点是 (ai, bi) 。
|
||||
|
||||
如果所有矩形一起精确覆盖了某个矩形区域,则返回 true ;否则,返回 false 。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 矩形区域的面积等于所有矩形的面积之和并且满足矩形区域四角的顶点只能出现一次,且其余顶点的出现次数只能是两次或四次则返回 true,否则返回 false
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
type point struct {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
func isRectangleCover(rectangles [][]int) bool {
|
||||
minX, minY, maxA, maxB := rectangles[0][0], rectangles[0][1], rectangles[0][2], rectangles[0][3]
|
||||
area := 0
|
||||
cnt := make(map[point]int)
|
||||
for _, v := range rectangles {
|
||||
x, y, a, b := v[0], v[1], v[2], v[3]
|
||||
area += (a - x) * (b - y)
|
||||
minX = min(minX, x)
|
||||
minY = min(minY, y)
|
||||
maxA = max(maxA, a)
|
||||
maxB = max(maxB, b)
|
||||
cnt[point{x, y}]++
|
||||
cnt[point{a, b}]++
|
||||
cnt[point{x, b}]++
|
||||
cnt[point{a, y}]++
|
||||
}
|
||||
if area != (maxA - minX) * (maxB - minY) ||
|
||||
cnt[point{minX, minY}] != 1 || cnt[point{maxA, maxB}] != 1 ||
|
||||
cnt[point{minX, maxB}] != 1 || cnt[point{maxA, minY}] != 1 {
|
||||
return false
|
||||
}
|
||||
delete(cnt, point{minX, minY})
|
||||
delete(cnt, point{maxA, maxB})
|
||||
delete(cnt, point{minX, maxB})
|
||||
delete(cnt, point{maxA, minY})
|
||||
for _, v := range cnt {
|
||||
if v != 2 && v != 4 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0389.Find-the-Difference/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0392.Is-Subsequence/">下一页➡️</a></p>
|
||||
</div>
|
@ -82,6 +82,6 @@ func isSubsequence1(s string, t string) bool {
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0389.Find-the-Difference/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0391.Perfect-Rectangle/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0393.UTF-8-Validation/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
@ -107,5 +107,5 @@ func getNextIndex(nums []int, index int) int {
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0456.132-Pattern/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0460.LFU-Cache/">下一页➡️</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0458.Poor-Pigs/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
84
website/content/ChapterFour/0400~0499/0458.Poor-Pigs.md
Normal file
84
website/content/ChapterFour/0400~0499/0458.Poor-Pigs.md
Normal file
@ -0,0 +1,84 @@
|
||||
# [458. Poor Pigs](https://leetcode.com/problems/poor-pigs/)
|
||||
|
||||
## 题目
|
||||
|
||||
There are buckets buckets of liquid, where exactly one of the buckets is poisonous. To figure out which one is poisonous, you feed some number of (poor) pigs the liquid to see whether they will die or not. Unfortunately, you only have minutesToTest minutes to determine which bucket is poisonous.
|
||||
|
||||
You can feed the pigs according to these steps:
|
||||
|
||||
- Choose some live pigs to feed.
|
||||
- For each pig, choose which buckets to feed it. The pig will consume all the chosen buckets simultaneously and will take no time.
|
||||
- Wait for minutesToDie minutes. You may not feed any other pigs during this time.
|
||||
- After minutesToDie minutes have passed, any pigs that have been fed the poisonous bucket will die, and all others will survive.
|
||||
- Repeat this process until you run out of time.
|
||||
|
||||
Given buckets, minutesToDie, and minutesToTest, return the minimum number of pigs needed to figure out which bucket is poisonous within the allotted time.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
Input: buckets = 1000, minutesToDie = 15, minutesToTest = 60
|
||||
Output: 5
|
||||
|
||||
**Example 2**:
|
||||
|
||||
Input: buckets = 4, minutesToDie = 15, minutesToTest = 15
|
||||
Output: 2
|
||||
|
||||
**Example 3**:
|
||||
|
||||
Input: buckets = 4, minutesToDie = 15, minutesToTest = 30
|
||||
Output: 2
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- 1 <= buckets <= 1000
|
||||
- 1 <= minutesToDie <= minutesToTest <= 100
|
||||
|
||||
## 题目大意
|
||||
|
||||
有 buckets 桶液体,其中 正好 有一桶含有毒药,其余装的都是水。它们从外观看起来都一样。为了弄清楚哪只水桶含有毒药,你可以喂一些猪喝,通过观察猪是否会死进行判断。不幸的是,你只有 minutesToTest 分钟时间来确定哪桶液体是有毒的。
|
||||
|
||||
喂猪的规则如下:
|
||||
|
||||
- 选择若干活猪进行喂养
|
||||
- 可以允许小猪同时饮用任意数量的桶中的水,并且该过程不需要时间。
|
||||
- 小猪喝完水后,必须有 minutesToDie 分钟的冷却时间。在这段时间里,你只能观察,而不允许继续喂猪。
|
||||
- 过了 minutesToDie 分钟后,所有喝到毒药的猪都会死去,其他所有猪都会活下来。
|
||||
- 重复这一过程,直到时间用完。
|
||||
|
||||
给你桶的数目 buckets ,minutesToDie 和 minutesToTest ,返回在规定时间内判断哪个桶有毒所需的 最小 猪数。
|
||||
|
||||
## 解题思路
|
||||
|
||||
使用数学方法,以 minutesToDie=15, minutesToTest=60, 1 只小猪为例,可以测试 5 只桶
|
||||
|
||||
- 0-15 小猪吃第一个桶中的液体,如果死去,则第一个桶有毒,否则继续测试
|
||||
- 15-30 小猪吃第二个桶中的液体,如果死去,则第二个桶有毒,否则继续测试
|
||||
- 30-45 小猪吃第三个桶中的液体,如果死去,则第三个桶有毒,否则继续测试
|
||||
- 45-60 小猪吃第四个桶中的液体,如果死去,则第四个桶有毒
|
||||
- 如果最后小猪没有死去,则第五个桶有毒
|
||||
|
||||
所以一只小猪在 minutesToDie 和 minutesToTest 时间一定的情况下可以最多判断 base = minutesToTest / minutesToDie + 1 个桶
|
||||
|
||||
假设小猪的数量是 num,那么 pow(base, num) >= buckets,根据对数运算规则,两边分别取对数得到: num >= Log10(buckets) / Log10(base)
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
import "math"
|
||||
|
||||
func poorPigs(buckets int, minutesToDie int, minutesToTest int) int {
|
||||
base := minutesToTest/minutesToDie + 1
|
||||
return int(math.Ceil(math.Log10(float64(buckets)) / math.Log10(float64(base))))
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0457.Circular-Array-Loop/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0460.LFU-Cache/">下一页➡️</a></p>
|
||||
</div>
|
@ -144,6 +144,6 @@ func (this *LFUCache) Put(key int, value int) {
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0457.Circular-Array-Loop/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0458.Poor-Pigs/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0461.Hamming-Distance/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
@ -60,5 +60,5 @@ func revers(s string) string {
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0554.Brick-Wall/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0561.Array-Partition-I/">下一页➡️</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0559.Maximum-Depth-of-N-ary-Tree/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
@ -0,0 +1,85 @@
|
||||
# [559. Maximum Depth of N-ary Tree](https://leetcode.com/problems/maximum-depth-of-n-ary-tree/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given a n-ary tree, find its maximum depth.
|
||||
|
||||
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
|
||||
|
||||
Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).
|
||||
|
||||
**Example 1**:
|
||||
|
||||

|
||||
|
||||
Input: root = [1,null,3,2,4,null,5,6]
|
||||
Output: 3
|
||||
|
||||
**Example 2**:
|
||||
|
||||

|
||||
|
||||
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
|
||||
Output: 5
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- The total number of nodes is in the range [0, 10000].
|
||||
- The depth of the n-ary tree is less than or equal to 1000.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个 N 叉树,找到其最大深度。
|
||||
|
||||
最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。
|
||||
|
||||
N 叉树输入按层序遍历序列化表示,每组子节点由空值分隔(请参见示例)。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 使用广度优先遍历
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
type Node struct {
|
||||
Val int
|
||||
Children []*Node
|
||||
}
|
||||
|
||||
func maxDepth(root *Node) int {
|
||||
if root == nil {
|
||||
return 0
|
||||
}
|
||||
return 1 + bfs(root)
|
||||
}
|
||||
|
||||
func bfs(root *Node) int {
|
||||
var q []*Node
|
||||
var depth int
|
||||
q = append(q, root.Children...)
|
||||
for len(q) != 0 {
|
||||
depth++
|
||||
length := len(q)
|
||||
for length != 0 {
|
||||
ele := q[0]
|
||||
q = q[1:]
|
||||
length--
|
||||
if ele != nil && len(ele.Children) != 0 {
|
||||
q = append(q, ele.Children...)
|
||||
}
|
||||
}
|
||||
}
|
||||
return depth
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0557.Reverse-Words-in-a-String-III/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0561.Array-Partition-I/">下一页➡️</a></p>
|
||||
</div>
|
@ -60,6 +60,6 @@ func arrayPairSum(nums []int) int {
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0557.Reverse-Words-in-a-String-III/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0559.Maximum-Depth-of-N-ary-Tree/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0563.Binary-Tree-Tilt/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
@ -148,5 +148,5 @@ func discretization(positions [][]int) map[int]int {
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0697.Degree-of-an-Array/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0703.Kth-Largest-Element-in-a-Stream/">下一页➡️</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0700.Search-in-a-Binary-Search-Tree/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
@ -0,0 +1,80 @@
|
||||
# [700. Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/)
|
||||
|
||||
## 题目
|
||||
|
||||
You are given the root of a binary search tree (BST) and an integer val.
|
||||
|
||||
Find the node in the BST that the node's value equals val and return the subtree rooted with that node. If such a node does not exist, return null.
|
||||
|
||||
**Example 1**:
|
||||
|
||||

|
||||
|
||||
Input: root = [4,2,7,1,3], val = 2
|
||||
Output: [2,1,3]
|
||||
|
||||
**Example 2**:
|
||||
|
||||

|
||||
|
||||
Input: root = [4,2,7,1,3], val = 5
|
||||
Output: []
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- The number of nodes in the tree is in the range [1, 5000].
|
||||
- 1 <= Node.val <= 10000000
|
||||
- root is a binary search tree.
|
||||
- 1 <= val <= 10000000
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定二叉搜索树(BST)的根节点和一个值。 你需要在BST中找到节点值等于给定值的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 NULL。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 根据二叉搜索树的性质(根节点的值大于左子树所有节点的值,小于右子树所有节点的值),进行递归求解
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func searchBST(root *TreeNode, val int) *TreeNode {
|
||||
if root == nil {
|
||||
return nil
|
||||
}
|
||||
if root.Val == val {
|
||||
return root
|
||||
} else if root.Val < val {
|
||||
return searchBST(root.Right, val)
|
||||
} else {
|
||||
return searchBST(root.Left, val)
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0699.Falling-Squares/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0703.Kth-Largest-Element-in-a-Stream/">下一页➡️</a></p>
|
||||
</div>
|
@ -95,6 +95,6 @@ func (kl *KthLargest) Add(val int) int {
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0699.Falling-Squares/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0700.Search-in-a-Binary-Search-Tree/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0704.Binary-Search/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
@ -105,5 +105,5 @@ func scoreOfParentheses(S string) int {
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0853.Car-Fleet/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0862.Shortest-Subarray-with-Sum-at-Least-K/">下一页➡️</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0859.Buddy-Strings/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
94
website/content/ChapterFour/0800~0899/0859.Buddy-Strings.md
Normal file
94
website/content/ChapterFour/0800~0899/0859.Buddy-Strings.md
Normal file
@ -0,0 +1,94 @@
|
||||
# [859. Buddy Strings](https://leetcode.com/problems/buddy-strings/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given two strings s and goal, return true if you can swap two letters in s so the result is equal to goal, otherwise, return false.
|
||||
|
||||
Swapping letters is defined as taking two indices i and j (0-indexed) such that i != j and swapping the characters at s[i] and s[j].
|
||||
|
||||
For example, swapping at indices 0 and 2 in "abcd" results in "cbad".
|
||||
|
||||
**Example 1**:
|
||||
|
||||
Input: s = "ab", goal = "ba"
|
||||
Output: true
|
||||
Explanation: You can swap s[0] = 'a' and s[1] = 'b' to get "ba", which is equal to goal.
|
||||
|
||||
**Example 2**:
|
||||
|
||||
Input: s = "ab", goal = "ab"
|
||||
Output: false
|
||||
Explanation: The only letters you can swap are s[0] = 'a' and s[1] = 'b', which results in "ba" != goal.
|
||||
|
||||
**Example 3**:
|
||||
|
||||
Input: s = "aa", goal = "aa"
|
||||
Output: true
|
||||
Explanation: You can swap s[0] = 'a' and s[1] = 'a' to get "aa", which is equal to goal.
|
||||
|
||||
**Example 4**:
|
||||
|
||||
Input: s = "aaaaaaabc", goal = "aaaaaaacb"
|
||||
Output: true
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- 1 <= s.length, goal.length <= 2 * 10000
|
||||
- s and goal consist of lowercase letters.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你两个字符串 s 和 goal ,只要我们可以通过交换 s 中的两个字母得到与 goal 相等的结果,就返回 true;否则返回 false 。
|
||||
|
||||
交换字母的定义是:取两个下标 i 和 j (下标从 0 开始)且满足 i != j ,接着交换 s[i] 和 s[j] 处的字符。
|
||||
|
||||
例如,在 "abcd" 中交换下标 0 和下标 2 的元素可以生成 "cbad" 。
|
||||
|
||||
## 解题思路
|
||||
|
||||
分为两种情况进行比较:
|
||||
- s 等于 goal, s 中有重复元素就返回 true,否则返回 false
|
||||
- s 不等于 goal, s 中有两个下标不同的字符与 goal 中对应下标的字符分别相等
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func buddyStrings(s string, goal string) bool {
|
||||
if len(s) != len(goal) || len(s) <= 1 {
|
||||
return false
|
||||
}
|
||||
mp := make(map[byte]int)
|
||||
if s == goal {
|
||||
for i := 0; i < len(s); i++ {
|
||||
if _, ok := mp[s[i]]; ok {
|
||||
return true
|
||||
}
|
||||
mp[s[i]]++
|
||||
}
|
||||
return false
|
||||
}
|
||||
first, second := -1, -1
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] != goal[i] {
|
||||
if first == -1 {
|
||||
first = i
|
||||
} else if second == -1 {
|
||||
second = i
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return second != -1 && s[first] == goal[second] && s[second] == goal[first]
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0856.Score-of-Parentheses/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0862.Shortest-Subarray-with-Sum-at-Least-K/">下一页➡️</a></p>
|
||||
</div>
|
@ -92,6 +92,6 @@ func shortestSubarray(A []int, K int) int {
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0856.Score-of-Parentheses/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0859.Buddy-Strings/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0863.All-Nodes-Distance-K-in-Binary-Tree/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
Reference in New Issue
Block a user