478. 在圆内随机生成点
给定圆的半径和圆心的位置,实现函数 randPoint
,在圆中产生均匀随机点。
实现 Solution
类:
Solution(double radius, double x_center, double y_center)
用圆的半径 radius
和圆心的位置 (x_center, y_center)
初始化对象randPoint()
返回圆内的一个随机点。圆周上的一点被认为在圆内。答案作为数组返回 [x, y]
。
示例 1:
输入: ["Solution","randPoint","randPoint","randPoint"] [[1.0, 0.0, 0.0], [], [], []] 输出: [null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]] 解释: Solution solution = new Solution(1.0, 0.0, 0.0); solution.randPoint ();//返回[-0.02493,-0.38077] solution.randPoint ();//返回[0.82314,0.38945] solution.randPoint ();//返回[0.36572,0.17248]
提示:
0 < radius <= 108
-107 <= x_center, y_center <= 107
randPoint
最多被调用 3 * 104
次相似题目
原站题解
python3 解法, 执行用时: 148 ms, 内存消耗: 25 MB, 提交时间: 2022-12-04 17:58:22
class Solution: def __init__(self, radius: float, x_center: float, y_center: float): self.xc = x_center self.yc = y_center self.r = radius def randPoint(self) -> List[float]: u, theta = random.random(), random.random() * 2 * math.pi r = sqrt(u) return [self.xc + r * math.cos(theta) * self.r, self.yc + r * math.sin(theta) * self.r] # Your Solution object will be instantiated and called as such: # obj = Solution(radius, x_center, y_center) # param_1 = obj.randPoint()
javascript 解法, 执行用时: 136 ms, 内存消耗: 57.1 MB, 提交时间: 2022-12-04 17:57:56
/** * @param {number} radius * @param {number} x_center * @param {number} y_center */ var Solution = function(radius, x_center, y_center) { this.xc = x_center; this.yc = y_center; this.r = radius; }; /** * @return {number[]} */ Solution.prototype.randPoint = function() { const u = Math.random(); const theta = Math.random() * 2 * Math.PI; const r = Math.sqrt(u); return [this.xc + r * Math.cos(theta) * this.r, this.yc + r * Math.sin(theta) * this.r]; }; /** * Your Solution object will be instantiated and called as such: * var obj = new Solution(radius, x_center, y_center) * var param_1 = obj.randPoint() */
golang 解法, 执行用时: 40 ms, 内存消耗: 12.7 MB, 提交时间: 2022-12-04 17:57:31
type Solution struct { radius, xCenter, yCenter float64 } func Constructor(radius, xCenter, yCenter float64) Solution { return Solution{radius, xCenter, yCenter} } func (s *Solution) RandPoint() []float64 { r := math.Sqrt(rand.Float64()) sin, cos := math.Sincos(rand.Float64() * 2 * math.Pi) return []float64{s.xCenter + r*cos*s.radius, s.yCenter + r*sin*s.radius} } /** * Your Solution object will be instantiated and called as such: * obj := Constructor(radius, x_center, y_center); * param_1 := obj.RandPoint(); */
java 解法, 执行用时: 230 ms, 内存消耗: 50.1 MB, 提交时间: 2022-12-04 17:57:16
class Solution { Random random; double xc, yc, r; public Solution(double radius, double x_center, double y_center) { random = new Random(); xc = x_center; yc = y_center; r = radius; } public double[] randPoint() { double u = random.nextDouble(); double theta = random.nextDouble() * 2 * Math.PI; double r = Math.sqrt(u); return new double[]{xc + r * Math.cos(theta) * this.r, yc + r * Math.sin(theta) * this.r}; } } /** * Your Solution object will be instantiated and called as such: * Solution obj = new Solution(radius, x_center, y_center); * double[] param_1 = obj.randPoint(); */
java 解法, 执行用时: 213 ms, 内存消耗: 50.4 MB, 提交时间: 2022-12-04 17:56:43
class Solution { Random random; double xc, yc, r; public Solution(double radius, double x_center, double y_center) { random = new Random(); xc = x_center; yc = y_center; r = radius; } public double[] randPoint() { while (true) { double x = random.nextDouble() * (2 * r) - r; double y = random.nextDouble() * (2 * r) - r; if (x * x + y * y <= r * r) { return new double[]{xc + x, yc + y}; } } } } /** * Your Solution object will be instantiated and called as such: * Solution obj = new Solution(radius, x_center, y_center); * double[] param_1 = obj.randPoint(); */
javascript 解法, 执行用时: 136 ms, 内存消耗: 57.2 MB, 提交时间: 2022-12-04 17:56:05
/** * @param {number} radius * @param {number} x_center * @param {number} y_center */ var Solution = function(radius, x_center, y_center) { this.xc = x_center; this.yc = y_center; this.r = radius; }; /** * @return {number[]} */ Solution.prototype.randPoint = function() { while (true) { const x = Math.random() * (2 * this.r) - this.r; const y = Math.random() * (2 * this.r) - this.r; if (x * x + y * y <= this.r * this.r) { return [this.xc + x, this.yc + y]; } } }; /** * Your Solution object will be instantiated and called as such: * var obj = new Solution(radius, x_center, y_center) * var param_1 = obj.randPoint() */
golang 解法, 执行用时: 44 ms, 内存消耗: 12.7 MB, 提交时间: 2022-12-04 17:55:12
type Solution struct { radius, xCenter, yCenter float64 } func Constructor(radius, xCenter, yCenter float64) Solution { return Solution{radius, xCenter, yCenter} } func (s *Solution) RandPoint() []float64 { for { x := rand.Float64()*2 - 1 y := rand.Float64()*2 - 1 // [-1,1) 的随机数 if x*x+y*y < 1 { return []float64{s.xCenter + x*s.radius, s.yCenter + y*s.radius} } } } /** * Your Solution object will be instantiated and called as such: * obj := Constructor(radius, x_center, y_center); * param_1 := obj.RandPoint(); */
python3 解法, 执行用时: 148 ms, 内存消耗: 24.7 MB, 提交时间: 2022-12-04 17:54:58
class Solution: def __init__(self, radius: float, x_center: float, y_center: float): self.xc = x_center self.yc = y_center self.r = radius def randPoint(self) -> List[float]: while True: x, y = random.uniform(-self.r, self.r), random.uniform(-self.r, self.r) if x * x + y * y <= self.r * self.r: return [self.xc + x, self.yc + y] # Your Solution object will be instantiated and called as such: # obj = Solution(radius, x_center, y_center) # param_1 = obj.randPoint()