478. Generate Random Point in a Circle | 478. 在圆内随机生成点(Java)
生活随笔
收集整理的這篇文章主要介紹了
478. Generate Random Point in a Circle | 478. 在圆内随机生成点(Java)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目
https://leetcode.com/problems/generate-random-point-in-a-circle/
題解
class Solution {double r, x, y, xMin, xMax, yMin, yMax;Random rand;public Solution(double r, double x, double y) {rand = new Random();this.r = r;this.x = x;this.y = y;xMin = x - r;xMax = x + r;yMin = y - r;yMax = y + r;}public double[] randPoint() {double x0 = xMin + (xMax - xMin) * rand.nextDouble();double y0 = yMin + (yMax - yMin) * rand.nextDouble();while (Math.pow(x0 - x, 2) + Math.pow(y0 - y, 2) > Math.pow(r, 2)) {x0 = xMin + (xMax - xMin) * rand.nextDouble();y0 = yMin + (yMax - yMin) * rand.nextDouble();}return new double[]{x0, y0};} }/*** Your Solution object will be instantiated and called as such:* Solution obj = new Solution(radius, x_center, y_center);* double[] param_1 = obj.randPoint();*/拓展:用概率求圓周率
總結(jié)
以上是生活随笔為你收集整理的478. Generate Random Point in a Circle | 478. 在圆内随机生成点(Java)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: leetcode 477. Total
- 下一篇: 486. Predict the Win