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. 【Hadoop离线基础总结】大数据集群环境准备

    大数据集群环境准备 三台虚拟机关闭防火墙 centOS 7 service firewalld stop ->关闭防火墙 chkconfig firewalld off ->开机关闭防火墙 ...

  2. Git使用教程之新手也能看懂(一)

    首先我写这篇文章的初衷是因为 有一段时间没用Git了,现在突然用起来,很多命令都忘记了,导致去上网查了各种资料和文档(其中廖雪峰老师的文章给我的帮助很大,非常感谢!),花费了大量的时间,等于是又重新学 ...

  3. docker部署微服务遇到的问题二

    自己尝试将微服务部署到docker上面,期间按照周立的微服务架构实战13章进行学习 按照书上的步骤,一切部署成功之后,尝试访问,一直没有成功访问,周五部署了两遍 折腾了一下 还是没有找到为啥,周一继续 ...

  4. SD实现原理学习,以及SD失效的问题解决

    SD失效的问题可能解决方案: 1.有可能是图片的url地址不对,有可能浏览器可以打开,但是这个地址浏览器是做了处理的,所以浏览器能打开. 2.如果图片地址是Http,那么就需要关闭ATS. ATS ( ...

  5. ES6-10笔记(二)

    class类 Javascript是一种基于对象(object-based)的语言,你遇到的所有东西几乎都是对象.但是,它又不是一种真正的面向对象编程(OOP)语言,因为它的语法中没有class(类) ...

  6. sqlservere小计合计总计

    SELECT CASE WHEN GROUPING(F1) = 1 THEN '总计' WHEN GROUPING(F1) = 0 AND GROUPING(F2) = 1 THEN F1+'合计' ...

  7. spark aggregate函数

    aggregate函数将每个分区里面的元素进行聚合,然后用combine函数将每个分区的结果和初始值(zeroValue)进行combine操作.这个函数最终返回的类型不需要和RDD中元素类型一致. ...

  8. ES6---面向对象上的简化

    一.ES6---面向对象上的简化 1.单个对象提供了2处简化: 1.1 如果对象的属性来自于对象外的变量,且变量名刚好和属性名相同.则不用写两遍相同的名字, 只要写一遍即可. 1.2 所有对象的方法, ...

  9. 使用基于MVC2模式创建新闻网站

    1.什么是MVC MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示 ...

  10. search(16)- elastic4s-内嵌文件:nested and join

    从SQL领域来的用户,对于ES的文件关系维护方式会感到很不习惯.毕竟,ES是分布式数据库只能高效处理独个扁平类型文件,无法支持关系式数据库那样的文件拼接.但是,任何数据库应用都无法避免树型文件关系,因 ...