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. 这些Kubernetes常见安全问题,你遇到过几个?

    导语:在 Threat Stack 公布的2020年第一季度安全报告中,列举了在AWS Web服务部署Kubernetes的组织所遇到的最常见安全问题. 该报告建议已部署Kubernetes的IT组织 ...

  2. 页面中js接收tp5 assign方式传过来的数组对象

    <script type="text/javascript"> var arr='<?php echo json_encode($nav) ?>'; var ...

  3. android关机流程

    关机过程的主要实现在ShutdownThread.java中在关机过程中,主要做了三件事:1.发送关机广播 有的模块可能需要监听手机关机事件,所以在关机时发送关机广播,通知相关模块处理.2.关闭一些主 ...

  4. 未联网下,在eclipse中编辑xml文件如何自动提示设置

    断网情况下,用eclipse编辑xml文件如何自动提示? 以编辑hibernate中的xml为例: 首先,我们都知道xml提示是引用.dtd文件的. 1.复制这个dtd路径,设置eclipse属性,搜 ...

  5. 有一分数序列:2/1, 3/2, 5/3, 8/5, 13/8, 21/13....求出这个数列的第M到N项之和(M>2,N>2,N>M)

    package bianchengti; /* * 有一分数序列:2/1, 3/2, 5/3, 8/5, 13/8, 21/13.... * 求出这个数列的第M到N项之和(M>2,N>2, ...

  6. Django的ListView超详细用法(含分页paginate功能)

    开发环境: python 3.6 django 1.11 场景一 经常有从数据库中获取一批数据,然后在前端以列表的形式展现,比如:获取到所有的用户,然后在用户列表页面展示. 解决方案 常规写法是,我们 ...

  7. Jquery动画,排队与并发

    一.事件绑定 1.鼠标事件:模拟触发 什么是模拟触发? 虽然没有点在按钮上,也可以触发按钮的事件处理函数. 如何:$元素.trigger("事件名") 即使没有点在指定的元素上,也 ...

  8. 第四篇:NLP(Natural Language Processing)自然语言处理

    NLP自然语言处理: 百度AI的 NLP自然语言处理python语言--pythonSDK文档: https://ai.baidu.com/docs#/NLP-Python-SDK/top 第三方模块 ...

  9. docker 学习(一)

    1. docker介绍 1)docker的出现 Docker是诞生于2013年,是dotCloud的一个开源项目,基于Google推出的GO语言实现.遵从Apache2.0协议. 2)docker介绍 ...

  10. python3.x 基础一:dict字典

    字典,{key,value} help(dict) 定义一个字典: >>> dict1 {', 'name': 'yzw'} >>> dict2=dict1 > ...