478. Generate Random Point in a Circle Medium
1/**
2 * [478] Generate Random Point in a Circle
3 *
4 * Given the radius and the position of the center of a circle, implement the function randPoint which generates a uniform random point inside the circle.
5 * Implement the Solution class:
6 *
7 * Solution(double radius, double x_center, double y_center) initializes the object with the radius of the circle radius and the position of the center (x_center, y_center).
8 * randPoint() returns a random point inside the circle. A point on the circumference of the circle is considered to be in the circle. The answer is returned as an array [x, y].
9 *
10 *
11 * Example 1:
12 *
13 * Input
14 * ["Solution", "randPoint", "randPoint", "randPoint"]
15 * [[1.0, 0.0, 0.0], [], [], []]
16 * Output
17 * [null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]]
18 * Explanation
19 * Solution solution = new Solution(1.0, 0.0, 0.0);
20 * solution.randPoint(); // return [-0.02493, -0.38077]
21 * solution.randPoint(); // return [0.82314, 0.38945]
22 * solution.randPoint(); // return [0.36572, 0.17248]
23 *
24 *
25 * Constraints:
26 *
27 * 0 < radius <= 10^8
28 * -10^7 <= x_center, y_center <= 10^7
29 * At most 3 * 10^4 calls will be made to randPoint.
30 *
31 */
32pub struct Solution {}
33
34// problem: https://leetcode.com/problems/generate-random-point-in-a-circle/
35// discuss: https://leetcode.com/problems/generate-random-point-in-a-circle/discuss/?currentPage=1&orderBy=most_votes&query=
36
37// submission codes start here
38
39struct Solution {
40 vec![]
41 }
42
43
44/**
45 * `&self` means the method takes an immutable reference.
46 * If you need a mutable reference, change it to `&mut self` instead.
47 */
48impl Solution {
49
50 fn new(radius: f64, x_center: f64, y_center: f64) -> Self {
51
52 }
53
54 fn rand_point(&self) -> Vec<f64> {
55
56 }
57}
58
59/**
60 * Your Solution object will be instantiated and called as such:
61 * let obj = Solution::new(radius, x_center, y_center);
62 * let ret_1: Vec<f64> = obj.rand_point();
63 */
64
65// submission codes end
66
67#[cfg(test)]
68mod tests {
69 use super::*;
70
71 #[test]
72 fn test_478() {
73 }
74}
75
Back
© 2025 bowen.ge All Rights Reserved.