478. 在圆内随机生成点

给定圆的半径和圆心的 x、y 坐标,写一个在圆中产生均匀随机点的函数 randPoint 。

说明:

输入值和输出值都将是浮点数。

圆的半径和圆心的 x、y 坐标将作为参数传递给类的构造函数。

圆周上的点也认为是在圆中。

randPoint 返回一个包含随机点的x坐标和y坐标的大小为2的数组。

示例 1:

输入:

[“Solution”,“randPoint”,“randPoint”,“randPoint”]

[[1,0,0],[],[],[]]

输出: [null,[-0.72939,-0.65505],[-0.78502,-0.28626],[-0.83119,-0.19803]]

示例 2:

输入:

[“Solution”,“randPoint”,“randPoint”,“randPoint”]

[[10,5,-7.5],[],[],[]]

输出: [null,[11.52438,-8.33273],[2.46992,-16.21705],[11.13430,-12.42337]]

输入语法说明:

输入是两个列表:调用成员函数名和调用的参数。Solution 的构造函数有三个参数,圆的半径、圆心的 x 坐标、圆心的 y 坐标。randPoint 没有参数。输入参数是一个列表,即使参数为空,也会输入一个 [] 空列表。

class Solution {

   private double radius;
private double x_center;
private double y_center;
public Solution(double radius, double x_center, double y_center) {
this.radius = radius;
this.x_center = x_center;
this.y_center = y_center;
} public double[] randPoint() {
Random random = new Random();
double x = x_center-radius + 2*radius*random.nextDouble();
double y = y_center-radius + 2*radius*random.nextDouble();
while(Math.sqrt(Math.pow(x - x_center, 2) + Math.pow(y - y_center, 2)) > radius){
x = x_center-radius + 2*radius*random.nextDouble();
y = y_center-radius + 2*radius*random.nextDouble();
}
return new double[]{x, 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();
*/

Java实现 LeetCode 478 在圆内随机生成点的更多相关文章

  1. [Swift]LeetCode478. 在圆内随机生成点 | Generate Random Point in a Circle

    Given the radius and x-y positions of the center of a circle, write a function randPoint which gener ...

  2. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  4. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  5. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  6. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  7. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  8. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. Java for LeetCode 154 Find Minimum in Rotated Sorted Array II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. 关于Fragment的点击切换数据滞留问题

    场景再现:当我使用tabLayout + Fragment 切换不同的fragment时,出现了数据重复显示的问题: 思考逻辑: - 每次切换fragment都会重新获取数据,但是list集合是全局的 ...

  2. dp规划之矩阵连乘问题

    问题描述:给定n个矩阵{A1,A2,...,An},其中Ai与Ai+1是可乘的,我们要计算这n个矩阵的最少计算次数.  将多个矩阵连乘抽象为最后一次计算即两个矩阵相乘. public static v ...

  3. Java TCP案例网络聊天室

    收获:1,加深了对多线程的一边一边的理解,可以将行为写成不同的类然后多线程 2,IO流的复习! 3,多线程中一边读取一边操作时容器最好(CopyOnWriteArrayList); 4,Tcp流程的熟 ...

  4. Mysql 常用函数(1)- 常用函数汇总

    Mysql常用函数的汇总,可看下面系列文章 Mysql常用函数有哪几类 数值型函数 字符串型函数 日期时间函数 聚合函数 流程控制函数 数值型函数 函数名称 作用 ABS 求绝对值 SQRT 求二次方 ...

  5. select嵌套问题

    关于sql语句: SELECT COUNT(ID) FROM dbo.N_Order_BusinessBatch WHERE Mobile='15210235082' And CreateTime=( ...

  6. RabbitMQ及延时队列

    一.简介 我用过RabbirMQ的发布订阅模式,以及一对一的延迟队列. 1.RabbitMQ的有消息确认机制,消费一条则队列中少一条,也有对应的消费到消息及认为是消费成功这样的模式,一般使用前者. 发 ...

  7. 2.3 Go内置函数

    内置函数 Go 语言拥有一些不需要进行导入操作就可以使用的内置函数.它们有时可以针对不同的类型进行操作,例如:len.cap 和 append,或必须用于系统级的操作,例如:panic.因此,它们需要 ...

  8. nginx配置之负载均衡

    nginx负载均衡 nginx代理机分发到多台同一项目的服务机 负载均衡器代理机配置:nginx.conf的http{}: #代理池,运行在不同服务机的程序 upstream loadtest{ se ...

  9. 基于Hdl Coder实现卡尔曼滤波算法

    总所周知,FPGA极其不擅长复杂算法的运算,但是如果项目中又涉及一些高级算法的实现,在没有可封装IP核调用的形式下,我们应该如何进行程序开发呢?今夕已经是2020年,我们一味依赖于用verilog写代 ...

  10. ftp服务器搭建(一)

    先安装vsftpd 发现安装yum不行 root命令下也不行 那么可以去 /var/lib/dpkg 把lock文件rm掉 然后在yum install vsftpd 发现可以了 注意:如果发现yum ...