mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-07 01:44:56 +08:00
Add solution 478
This commit is contained in:
34
README.md
34
README.md
@ -127,15 +127,15 @@
|
||||
| | Easy | Medium | Hard | Total |
|
||||
|:--------:|:--------:|:--------:|:--------:|:--------:|
|
||||
|Optimizing|36|41|16|93|
|
||||
|Accepted|**272**|**349**|**104**|**725**|
|
||||
|Accepted|**272**|**350**|**104**|**726**|
|
||||
|Total|468|945|380|1793|
|
||||
|Perfection Rate|86.8%|88.3%|84.6%|87.2%|
|
||||
|Completion Rate|58.1%|36.9%|27.4%|40.4%|
|
||||
|Completion Rate|58.1%|37.0%|27.4%|40.5%|
|
||||
|------------|----------------------------|----------------------------|----------------------------|----------------------------|
|
||||
|
||||
## 二. 目录
|
||||
|
||||
以下已经收录了 632 道题的题解,还有 12 道题在尝试优化到 beats 100%
|
||||
以下已经收录了 633 道题的题解,还有 12 道题在尝试优化到 beats 100%
|
||||
|
||||
| No. | Title | Solution | Acceptance | Difficulty | Frequency |
|
||||
|:--------:|:--------------------------------------------------------------|:--------:|:--------:|:--------:|:--------:|
|
||||
@ -434,7 +434,7 @@
|
||||
|0293|Flip Game||61.4%|Easy||
|
||||
|0294|Flip Game II||50.7%|Medium||
|
||||
|0295|Find Median from Data Stream||47.1%|Hard||
|
||||
|0296|Best Meeting Point||58.1%|Hard||
|
||||
|0296|Best Meeting Point||58.2%|Hard||
|
||||
|0297|Serialize and Deserialize Binary Tree||50.0%|Hard||
|
||||
|0298|Binary Tree Longest Consecutive Sequence||48.1%|Medium||
|
||||
|0299|Bulls and Cows||44.6%|Medium||
|
||||
@ -616,7 +616,7 @@
|
||||
|0475|Heaters|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0475.Heaters)|33.7%|Medium||
|
||||
|0476|Number Complement|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0476.Number-Complement)|65.1%|Easy||
|
||||
|0477|Total Hamming Distance|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0477.Total-Hamming-Distance)|50.6%|Medium||
|
||||
|0478|Generate Random Point in a Circle||38.9%|Medium||
|
||||
|0478|Generate Random Point in a Circle|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0478.Generate-Random-Point-in-a-Circle)|38.8%|Medium||
|
||||
|0479|Largest Palindrome Product||29.6%|Hard||
|
||||
|0480|Sliding Window Median|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0480.Sliding-Window-Median)|38.8%|Hard||
|
||||
|0481|Magical String||48.1%|Medium||
|
||||
@ -779,7 +779,7 @@
|
||||
|0638|Shopping Offers|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0638.Shopping-Offers)|53.0%|Medium||
|
||||
|0639|Decode Ways II||27.6%|Hard||
|
||||
|0640|Solve the Equation||42.7%|Medium||
|
||||
|0641|Design Circular Deque||56.2%|Medium||
|
||||
|0641|Design Circular Deque||56.3%|Medium||
|
||||
|0642|Design Search Autocomplete System||46.3%|Hard||
|
||||
|0643|Maximum Average Subarray I|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0643.Maximum-Average-Subarray-I)|42.0%|Easy||
|
||||
|0644|Maximum Average Subarray II||34.1%|Hard||
|
||||
@ -1154,7 +1154,7 @@
|
||||
|1013|Partition Array Into Three Parts With Equal Sum||48.5%|Easy||
|
||||
|1014|Best Sightseeing Pair||52.9%|Medium||
|
||||
|1015|Smallest Integer Divisible by K||41.9%|Medium||
|
||||
|1016|Binary String With Substrings Representing 1 To N||58.9%|Medium||
|
||||
|1016|Binary String With Substrings Representing 1 To N||58.8%|Medium||
|
||||
|1017|Convert to Base -2|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1017.Convert-to-Base--2)|59.8%|Medium||
|
||||
|1018|Binary Prefix Divisible By 5|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1018.Binary-Prefix-Divisible-By-5)|47.8%|Easy||
|
||||
|1019|Next Greater Node In Linked List|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1019.Next-Greater-Node-In-Linked-List)|58.3%|Medium||
|
||||
@ -1466,7 +1466,7 @@
|
||||
|1325|Delete Leaves With a Given Value||73.8%|Medium||
|
||||
|1326|Minimum Number of Taps to Open to Water a Garden||47.6%|Hard||
|
||||
|1327|List the Products Ordered in a Period||77.6%|Easy||
|
||||
|1328|Break a Palindrome||47.0%|Medium||
|
||||
|1328|Break a Palindrome||47.1%|Medium||
|
||||
|1329|Sort the Matrix Diagonally|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1329.Sort-the-Matrix-Diagonally)|81.8%|Medium||
|
||||
|1330|Reverse Subarray To Maximize Array Value||36.7%|Hard||
|
||||
|1331|Rank Transform of an Array||57.4%|Easy||
|
||||
@ -1589,7 +1589,7 @@
|
||||
|1448|Count Good Nodes in Binary Tree||71.0%|Medium||
|
||||
|1449|Form Largest Integer With Digits That Add up to Target||44.2%|Hard||
|
||||
|1450|Number of Students Doing Homework at a Given Time||77.1%|Easy||
|
||||
|1451|Rearrange Words in a Sentence||59.6%|Medium||
|
||||
|1451|Rearrange Words in a Sentence||59.7%|Medium||
|
||||
|1452|People Whose List of Favorite Companies Is Not a Subset of Another List||55.1%|Medium||
|
||||
|1453|Maximum Number of Darts Inside of a Circular Dartboard||35.4%|Hard||
|
||||
|1454|Active Users||38.9%|Medium||
|
||||
@ -1607,7 +1607,7 @@
|
||||
|1466|Reorder Routes to Make All Paths Lead to the City Zero||61.9%|Medium||
|
||||
|1467|Probability of a Two Boxes Having The Same Number of Distinct Balls||60.9%|Hard||
|
||||
|1468|Calculate Salaries||82.0%|Medium||
|
||||
|1469|Find All The Lonely Nodes||80.4%|Easy||
|
||||
|1469|Find All The Lonely Nodes||80.5%|Easy||
|
||||
|1470|Shuffle the Array|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1470.Shuffle-the-Array)|88.2%|Easy||
|
||||
|1471|The k Strongest Values in an Array||58.5%|Medium||
|
||||
|1472|Design Browser History||71.5%|Medium||
|
||||
@ -1695,7 +1695,7 @@
|
||||
|1554|Strings Differ by One Character||63.6%|Medium||
|
||||
|1555|Bank Account Summary||52.8%|Medium||
|
||||
|1556|Thousand Separator||57.4%|Easy||
|
||||
|1557|Minimum Number of Vertices to Reach All Nodes||75.5%|Medium||
|
||||
|1557|Minimum Number of Vertices to Reach All Nodes||75.6%|Medium||
|
||||
|1558|Minimum Numbers of Function Calls to Make Target Array||63.4%|Medium||
|
||||
|1559|Detect Cycles in 2D Grid||44.5%|Hard||
|
||||
|1560|Most Visited Sector in a Circular Track||56.9%|Easy||
|
||||
@ -1756,7 +1756,7 @@
|
||||
|1615|Maximal Network Rank||52.3%|Medium||
|
||||
|1616|Split Two Strings to Make Palindrome||36.1%|Medium||
|
||||
|1617|Count Subtrees With Max Distance Between Cities||63.2%|Hard||
|
||||
|1618|Maximum Font to Fit a Sentence in a Screen||56.5%|Medium||
|
||||
|1618|Maximum Font to Fit a Sentence in a Screen||56.6%|Medium||
|
||||
|1619|Mean of Array After Removing Some Elements|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1619.Mean-of-Array-After-Removing-Some-Elements)|64.9%|Easy||
|
||||
|1620|Coordinate With Maximum Network Quality||37.1%|Medium||
|
||||
|1621|Number of Sets of K Non-Overlapping Line Segments||41.2%|Medium||
|
||||
@ -1820,7 +1820,7 @@
|
||||
|1679|Max Number of K-Sum Pairs|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1679.Max-Number-of-K-Sum-Pairs)|54.0%|Medium||
|
||||
|1680|Concatenation of Consecutive Binary Numbers|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1680.Concatenation-of-Consecutive-Binary-Numbers)|52.3%|Medium||
|
||||
|1681|Minimum Incompatibility|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1681.Minimum-Incompatibility)|35.7%|Hard||
|
||||
|1682|Longest Palindromic Subsequence II||51.7%|Medium||
|
||||
|1682|Longest Palindromic Subsequence II||51.6%|Medium||
|
||||
|1683|Invalid Tweets||90.9%|Easy||
|
||||
|1684|Count the Number of Consistent Strings|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1684.Count-the-Number-of-Consistent-Strings)|82.2%|Easy||
|
||||
|1685|Sum of Absolute Differences in a Sorted Array|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1685.Sum-of-Absolute-Differences-in-a-Sorted-Array)|62.7%|Medium||
|
||||
@ -1835,7 +1835,7 @@
|
||||
|1694|Reformat Phone Number|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1694.Reformat-Phone-Number)|65.7%|Easy||
|
||||
|1695|Maximum Erasure Value|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1695.Maximum-Erasure-Value)|49.5%|Medium||
|
||||
|1696|Jump Game VI|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1696.Jump-Game-VI)|52.7%|Medium||
|
||||
|1697|Checking Existence of Edge Length Limited Paths||54.6%|Hard||
|
||||
|1697|Checking Existence of Edge Length Limited Paths||54.5%|Hard||
|
||||
|1698|Number of Distinct Substrings in a String||60.3%|Medium||
|
||||
|1699|Number of Calls Between Two Persons||86.6%|Medium||
|
||||
|1700|Number of Students Unable to Eat Lunch|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1700.Number-of-Students-Unable-to-Eat-Lunch)|68.0%|Easy||
|
||||
@ -1907,7 +1907,7 @@
|
||||
|1766|Tree of Coprimes||36.7%|Hard||
|
||||
|1767|Find the Subtasks That Did Not Execute||87.8%|Hard||
|
||||
|1768|Merge Strings Alternately||76.8%|Easy||
|
||||
|1769|Minimum Number of Operations to Move All Balls to Each Box||87.5%|Medium||
|
||||
|1769|Minimum Number of Operations to Move All Balls to Each Box||87.6%|Medium||
|
||||
|1770|Maximum Score from Performing Multiplication Operations||29.0%|Medium||
|
||||
|1771|Maximize Palindrome Length From Subsequences||34.1%|Hard||
|
||||
|1772|Sort Features by Popularity||64.9%|Medium||
|
||||
@ -1925,10 +1925,10 @@
|
||||
|1784|Check if Binary String Has at Most One Segment of Ones||41.9%|Easy||
|
||||
|1785|Minimum Elements to Add to Form a Given Sum||38.8%|Medium||
|
||||
|1786|Number of Restricted Paths From First to Last Node||36.0%|Medium||
|
||||
|1787|Make the XOR of All Segments Equal to Zero||35.4%|Hard||
|
||||
|1787|Make the XOR of All Segments Equal to Zero||35.3%|Hard||
|
||||
|1788|Maximize the Beauty of the Garden||70.2%|Hard||
|
||||
|1789|Primary Department for Each Employee||78.9%|Easy||
|
||||
|1790|Check if One String Swap Can Make Strings Equal||45.5%|Easy||
|
||||
|1790|Check if One String Swap Can Make Strings Equal||45.6%|Easy||
|
||||
|1791|Find Center of Star Graph||85.9%|Medium||
|
||||
|1792|Maximum Average Pass Ratio||44.9%|Medium||
|
||||
|1793|Maximum Score of a Good Subarray||44.6%|Hard||
|
||||
|
@ -44,9 +44,7 @@ babgbagbabgbagbabgbagbabgbagbabgbag
|
||||
- 当 `i < len(s)` 且 `j < len(t)` 的时候,如果 `s[i] == t[j]`,有 2 种匹配方式,第一种将 `s[i]` 与 `t[j]` 匹配,那么 `t[j+1:]` 匹配 `s[i+1:]` 的子序列,子序列数为 `dp[i+1][j+1]`;第二种将 `s[i]` 不与 `t[j]` 匹配,`t[j:]` 作为 `s[i+1:]` 的子序列,子序列数为 `dp[i+1][j]`。综合 2 种情况,当 `s[i] == t[j]` 时,`dp[i][j] = dp[i+1][j+1] + dp[i+1][j]`。
|
||||
- 如果 `s[i] != t[j]`,此时 `t[j:]` 只能作为 `s[i+1:]` 的子序列,子序列数为 `dp[i+1][j]`。所以当 `s[i] != t[j]` 时,`dp[i][j] = dp[i+1][j]`。综上分析得:
|
||||
|
||||
{{< katex display >}}
|
||||
dp[i][j] = \left\{\begin{matrix}dp[i+1][j+1]+dp[i+1][j]&,s[i]=t[j]\\ dp[i+1][j]&,s[i]!=t[j]\end{matrix}\right.
|
||||
{{< /katex >}}
|
||||
$$dp[i][j] = \left\{\begin{matrix}dp[i+1][j+1]+dp[i+1][j]&,s[i]=t[j]\\ dp[i+1][j]&,s[i]!=t[j]\end{matrix}\right.$$
|
||||
|
||||
- 最后是优化版本。写出上述代码以后,可以发现填表的过程是从右下角一直填到左上角。填表顺序是 从下往上一行一行的填。行内从右往左填。于是可以将这个二维数据压缩到一维。因为填充当前行只需要用到它的下一行信息即可,更进一步,用到的是下一行中右边元素的信息。于是可以每次更新该行时,先将旧的值存起来,计算更新该行的时候从右往左更新。这样做即可减少一维空间,将原来的二维数组压缩到一维数组。
|
||||
|
||||
|
@ -0,0 +1,46 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"math"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Solution struct {
|
||||
r float64
|
||||
x float64
|
||||
y float64
|
||||
}
|
||||
|
||||
func Constructor(radius float64, x_center float64, y_center float64) Solution {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
return Solution{radius, x_center, y_center}
|
||||
}
|
||||
|
||||
func (this *Solution) RandPoint() []float64 {
|
||||
/*
|
||||
a := angle()
|
||||
r := this.r * math.Sqrt(rand.Float64())
|
||||
x := r * math.Cos(a) + this.x
|
||||
y := r * math.Sin(a) + this.y
|
||||
return []float64{x, y}*/
|
||||
for {
|
||||
rx := 2*rand.Float64() - 1.0
|
||||
ry := 2*rand.Float64() - 1.0
|
||||
x := this.r * rx
|
||||
y := this.r * ry
|
||||
if x*x+y*y <= this.r*this.r {
|
||||
return []float64{x + this.x, y + this.y}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func angle() float64 {
|
||||
return rand.Float64() * 2 * math.Pi
|
||||
}
|
||||
|
||||
/**
|
||||
* Your Solution object will be instantiated and called as such:
|
||||
* obj := Constructor(radius, x_center, y_center);
|
||||
* param_1 := obj.RandPoint();
|
||||
*/
|
@ -0,0 +1,18 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_Problem478(t *testing.T) {
|
||||
obj := Constructor(1, 0, 0)
|
||||
fmt.Printf("RandPoint() = %v\n", obj.RandPoint())
|
||||
fmt.Printf("RandPoint() = %v\n", obj.RandPoint())
|
||||
fmt.Printf("RandPoint() = %v\n", obj.RandPoint())
|
||||
|
||||
obj = Constructor(10, 5, -7.5)
|
||||
fmt.Printf("RandPoint() = %v\n", obj.RandPoint())
|
||||
fmt.Printf("RandPoint() = %v\n", obj.RandPoint())
|
||||
fmt.Printf("RandPoint() = %v\n", obj.RandPoint())
|
||||
}
|
103
leetcode/0478.Generate-Random-Point-in-a-Circle/README.md
Normal file
103
leetcode/0478.Generate-Random-Point-in-a-Circle/README.md
Normal file
@ -0,0 +1,103 @@
|
||||
# [478. Generate Random Point in a Circle](https://leetcode.com/problems/generate-random-point-in-a-circle/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given the radius and x-y positions of the center of a circle, write a function `randPoint` which generates a uniform random point in the circle.
|
||||
|
||||
Note:
|
||||
|
||||
1. input and output values are in [floating-point](https://www.webopedia.com/TERM/F/floating_point_number.html).
|
||||
2. radius and x-y position of the center of the circle is passed into the class constructor.
|
||||
3. a point on the circumference of the circle is considered to be in the circle.
|
||||
4. `randPoint` returns a size 2 array containing x-position and y-position of the random point, in that order.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input:
|
||||
["Solution","randPoint","randPoint","randPoint"]
|
||||
[[1,0,0],[],[],[]]
|
||||
Output: [null,[-0.72939,-0.65505],[-0.78502,-0.28626],[-0.83119,-0.19803]]
|
||||
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input:
|
||||
["Solution","randPoint","randPoint","randPoint"]
|
||||
[[10,5,-7.5],[],[],[]]
|
||||
Output: [null,[11.52438,-8.33273],[2.46992,-16.21705],[11.13430,-12.42337]]
|
||||
```
|
||||
|
||||
**Explanation of Input Syntax:**
|
||||
|
||||
The input is two lists: the subroutines called and their arguments. `Solution`'s constructor has three arguments, the radius, x-position of the center, and y-position of the center of the circle. `randPoint` has no arguments. Arguments are always wrapped with a list, even if there aren't any.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定圆的半径和圆心的 x、y 坐标,写一个在圆中产生均匀随机点的函数 randPoint 。
|
||||
|
||||
说明:
|
||||
|
||||
- 输入值和输出值都将是浮点数。
|
||||
- 圆的半径和圆心的 x、y 坐标将作为参数传递给类的构造函数。
|
||||
- 圆周上的点也认为是在圆中。
|
||||
- randPoint 返回一个包含随机点的x坐标和y坐标的大小为2的数组。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 随机产生一个圆内的点,这个点一定满足定义 `(x-a)^2+(y-b)^2 ≤ R^2`,其中 `(a,b)` 是圆的圆心坐标,`R` 是半径。
|
||||
- 先假设圆心坐标在 (0,0),这样方便计算,最终输出坐标的时候整体加上圆心的偏移量即可。`rand.Float64()` 产生一个 `[0.0,1.0)` 区间的浮点数。`-R ≤ 2 * R * rand() - R < R`,利用随机产生坐标点的横纵坐标 `(x,y)` 与半径 R 的关系,如果 `x^2 + y^2 ≤ R^2`,那么说明产生的点在圆内。最终输出的时候要记得加上圆心坐标的偏移值。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"math"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Solution struct {
|
||||
r float64
|
||||
x float64
|
||||
y float64
|
||||
}
|
||||
|
||||
func Constructor(radius float64, x_center float64, y_center float64) Solution {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
return Solution{radius, x_center, y_center}
|
||||
}
|
||||
|
||||
func (this *Solution) RandPoint() []float64 {
|
||||
/*
|
||||
a := angle()
|
||||
r := this.r * math.Sqrt(rand.Float64())
|
||||
x := r * math.Cos(a) + this.x
|
||||
y := r * math.Sin(a) + this.y
|
||||
return []float64{x, y}*/
|
||||
for {
|
||||
rx := 2*rand.Float64() - 1.0
|
||||
ry := 2*rand.Float64() - 1.0
|
||||
x := this.r * rx
|
||||
y := this.r * ry
|
||||
if x*x+y*y <= this.r*this.r {
|
||||
return []float64{x + this.x, y + this.y}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func angle() float64 {
|
||||
return rand.Float64() * 2 * math.Pi
|
||||
}
|
||||
|
||||
/**
|
||||
* Your Solution object will be instantiated and called as such:
|
||||
* obj := Constructor(radius, x_center, y_center);
|
||||
* param_1 := obj.RandPoint();
|
||||
*/
|
||||
```
|
@ -44,7 +44,9 @@ babgbagbabgbagbabgbagbabgbagbabgbag
|
||||
- 当 `i < len(s)` 且 `j < len(t)` 的时候,如果 `s[i] == t[j]`,有 2 种匹配方式,第一种将 `s[i]` 与 `t[j]` 匹配,那么 `t[j+1:]` 匹配 `s[i+1:]` 的子序列,子序列数为 `dp[i+1][j+1]`;第二种将 `s[i]` 不与 `t[j]` 匹配,`t[j:]` 作为 `s[i+1:]` 的子序列,子序列数为 `dp[i+1][j]`。综合 2 种情况,当 `s[i] == t[j]` 时,`dp[i][j] = dp[i+1][j+1] + dp[i+1][j]`。
|
||||
- 如果 `s[i] != t[j]`,此时 `t[j:]` 只能作为 `s[i+1:]` 的子序列,子序列数为 `dp[i+1][j]`。所以当 `s[i] != t[j]` 时,`dp[i][j] = dp[i+1][j]`。综上分析得:
|
||||
|
||||
$$dp[i][j] = \left\{\begin{matrix}dp[i+1][j+1]+dp[i+1][j]&,s[i]=t[j]\\ dp[i+1][j]&,s[i]!=t[j]\end{matrix}\right.$$
|
||||
{{< katex display >}}
|
||||
dp[i][j] = \left\{\begin{matrix}dp[i+1][j+1]+dp[i+1][j]&,s[i]=t[j]\\ dp[i+1][j]&,s[i]!=t[j]\end{matrix}\right.
|
||||
{{< /katex >}}
|
||||
|
||||
- 最后是优化版本。写出上述代码以后,可以发现填表的过程是从右下角一直填到左上角。填表顺序是 从下往上一行一行的填。行内从右往左填。于是可以将这个二维数据压缩到一维。因为填充当前行只需要用到它的下一行信息即可,更进一步,用到的是下一行中右边元素的信息。于是可以每次更新该行时,先将旧的值存起来,计算更新该行的时候从右往左更新。这样做即可减少一维空间,将原来的二维数组压缩到一维数组。
|
||||
|
||||
|
@ -69,5 +69,5 @@ func totalHammingDistance1(nums []int) int {
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0476.Number-Complement/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0480.Sliding-Window-Median/">下一页➡️</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0478.Generate-Random-Point-in-a-Circle/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
@ -0,0 +1,110 @@
|
||||
# [478. Generate Random Point in a Circle](https://leetcode.com/problems/generate-random-point-in-a-circle/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given the radius and x-y positions of the center of a circle, write a function `randPoint` which generates a uniform random point in the circle.
|
||||
|
||||
Note:
|
||||
|
||||
1. input and output values are in [floating-point](https://www.webopedia.com/TERM/F/floating_point_number.html).
|
||||
2. radius and x-y position of the center of the circle is passed into the class constructor.
|
||||
3. a point on the circumference of the circle is considered to be in the circle.
|
||||
4. `randPoint` returns a size 2 array containing x-position and y-position of the random point, in that order.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input:
|
||||
["Solution","randPoint","randPoint","randPoint"]
|
||||
[[1,0,0],[],[],[]]
|
||||
Output: [null,[-0.72939,-0.65505],[-0.78502,-0.28626],[-0.83119,-0.19803]]
|
||||
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input:
|
||||
["Solution","randPoint","randPoint","randPoint"]
|
||||
[[10,5,-7.5],[],[],[]]
|
||||
Output: [null,[11.52438,-8.33273],[2.46992,-16.21705],[11.13430,-12.42337]]
|
||||
```
|
||||
|
||||
**Explanation of Input Syntax:**
|
||||
|
||||
The input is two lists: the subroutines called and their arguments. `Solution`'s constructor has three arguments, the radius, x-position of the center, and y-position of the center of the circle. `randPoint` has no arguments. Arguments are always wrapped with a list, even if there aren't any.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定圆的半径和圆心的 x、y 坐标,写一个在圆中产生均匀随机点的函数 randPoint 。
|
||||
|
||||
说明:
|
||||
|
||||
- 输入值和输出值都将是浮点数。
|
||||
- 圆的半径和圆心的 x、y 坐标将作为参数传递给类的构造函数。
|
||||
- 圆周上的点也认为是在圆中。
|
||||
- randPoint 返回一个包含随机点的x坐标和y坐标的大小为2的数组。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 随机产生一个圆内的点,这个点一定满足定义 `(x-a)^2+(y-b)^2 ≤ R^2`,其中 `(a,b)` 是圆的圆心坐标,`R` 是半径。
|
||||
- 先假设圆心坐标在 (0,0),这样方便计算,最终输出坐标的时候整体加上圆心的偏移量即可。`rand.Float64()` 产生一个 `[0.0,1.0)` 区间的浮点数。`-R ≤ 2 * R * rand() - R < R`,利用随机产生坐标点的横纵坐标 `(x,y)` 与半径 R 的关系,如果 `x^2 + y^2 ≤ R^2`,那么说明产生的点在圆内。最终输出的时候要记得加上圆心坐标的偏移值。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"math"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Solution struct {
|
||||
r float64
|
||||
x float64
|
||||
y float64
|
||||
}
|
||||
|
||||
func Constructor(radius float64, x_center float64, y_center float64) Solution {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
return Solution{radius, x_center, y_center}
|
||||
}
|
||||
|
||||
func (this *Solution) RandPoint() []float64 {
|
||||
/*
|
||||
a := angle()
|
||||
r := this.r * math.Sqrt(rand.Float64())
|
||||
x := r * math.Cos(a) + this.x
|
||||
y := r * math.Sin(a) + this.y
|
||||
return []float64{x, y}*/
|
||||
for {
|
||||
rx := 2*rand.Float64() - 1.0
|
||||
ry := 2*rand.Float64() - 1.0
|
||||
x := this.r * rx
|
||||
y := this.r * ry
|
||||
if x*x+y*y <= this.r*this.r {
|
||||
return []float64{x + this.x, y + this.y}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func angle() float64 {
|
||||
return rand.Float64() * 2 * math.Pi
|
||||
}
|
||||
|
||||
/**
|
||||
* Your Solution object will be instantiated and called as such:
|
||||
* obj := Constructor(radius, x_center, y_center);
|
||||
* param_1 := obj.RandPoint();
|
||||
*/
|
||||
```
|
||||
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0477.Total-Hamming-Distance/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0480.Sliding-Window-Median/">下一页➡️</a></p>
|
||||
</div>
|
@ -278,6 +278,6 @@ func (h *MaxHeapR) Remove(x int) { heap.Push(&h.hpDel, x) }
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0477.Total-Hamming-Distance/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0478.Generate-Random-Point-in-a-Circle/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0483.Smallest-Good-Base/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
@ -42,7 +42,7 @@ weight: 13
|
||||
|0463|Island Perimeter|[Go]({{< relref "/ChapterFour/0400~0499/0463.Island-Perimeter.md" >}})|Easy||||66.7%|
|
||||
|0500|Keyboard Row|[Go]({{< relref "/ChapterFour/0500~0599/0500.Keyboard-Row.md" >}})|Easy||||65.8%|
|
||||
|0508|Most Frequent Subtree Sum|[Go]({{< relref "/ChapterFour/0500~0599/0508.Most-Frequent-Subtree-Sum.md" >}})|Medium||||59.1%|
|
||||
|0535|Encode and Decode TinyURL|[Go]({{< relref "/ChapterFour/0500~0599/0535.Encode-and-Decode-TinyURL.md" >}})|Medium||||82.3%|
|
||||
|0535|Encode and Decode TinyURL|[Go]({{< relref "/ChapterFour/0500~0599/0535.Encode-and-Decode-TinyURL.md" >}})|Medium||||82.2%|
|
||||
|0575|Distribute Candies|[Go]({{< relref "/ChapterFour/0500~0599/0575.Distribute-Candies.md" >}})|Easy||||64.5%|
|
||||
|0594|Longest Harmonious Subsequence|[Go]({{< relref "/ChapterFour/0500~0599/0594.Longest-Harmonious-Subsequence.md" >}})|Easy||||51.2%|
|
||||
|0599|Minimum Index Sum of Two Lists|[Go]({{< relref "/ChapterFour/0500~0599/0599.Minimum-Index-Sum-of-Two-Lists.md" >}})|Easy||||51.8%|
|
||||
|
@ -42,9 +42,10 @@ weight: 12
|
||||
|0441|Arranging Coins|[Go]({{< relref "/ChapterFour/0400~0499/0441.Arranging-Coins.md" >}})|Easy||||42.4%|
|
||||
|0447|Number of Boomerangs|[Go]({{< relref "/ChapterFour/0400~0499/0447.Number-of-Boomerangs.md" >}})|Medium||||52.5%|
|
||||
|0453|Minimum Moves to Equal Array Elements|[Go]({{< relref "/ChapterFour/0400~0499/0453.Minimum-Moves-to-Equal-Array-Elements.md" >}})|Easy||||50.8%|
|
||||
|0478|Generate Random Point in a Circle|[Go]({{< relref "/ChapterFour/0400~0499/0478.Generate-Random-Point-in-a-Circle.md" >}})|Medium||||38.9%|
|
||||
|0483|Smallest Good Base|[Go]({{< relref "/ChapterFour/0400~0499/0483.Smallest-Good-Base.md" >}})|Hard||||36.3%|
|
||||
|0507|Perfect Number|[Go]({{< relref "/ChapterFour/0500~0599/0507.Perfect-Number.md" >}})|Easy||||36.2%|
|
||||
|0535|Encode and Decode TinyURL|[Go]({{< relref "/ChapterFour/0500~0599/0535.Encode-and-Decode-TinyURL.md" >}})|Medium||||82.3%|
|
||||
|0535|Encode and Decode TinyURL|[Go]({{< relref "/ChapterFour/0500~0599/0535.Encode-and-Decode-TinyURL.md" >}})|Medium||||82.2%|
|
||||
|0537|Complex Number Multiplication|[Go]({{< relref "/ChapterFour/0500~0599/0537.Complex-Number-Multiplication.md" >}})|Medium||||68.3%|
|
||||
|0598|Range Addition II|[Go]({{< relref "/ChapterFour/0500~0599/0598.Range-Addition-II.md" >}})|Easy||||50.2%|
|
||||
|0628|Maximum Product of Three Numbers|[Go]({{< relref "/ChapterFour/0600~0699/0628.Maximum-Product-of-Three-Numbers.md" >}})|Easy| O(n)| O(1)||46.8%|
|
||||
|
Reference in New Issue
Block a user