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. Android 8.1 关机充电动画(三)Android模式

    system:Android 8.1 platform:RK3326/PX30 uboot kernel system/core/healthd Android 8.1 关机充电动画(一)模式选择 A ...

  2. FastDFS安装(mac)|文件存储方案

    目录 FastDFS安装(mac)|文件存储方案 1 FastDFS介绍 1.1 FastDFS架构 1.2 工作原理实例介绍 1.3 FastDFS上传和下载流程 1.4 FastDFS文件索引 2 ...

  3. C# Html转pdf文件

    using iTextSharp.text; using iTextSharp.text.pdf; using System; using System.Collections.Generic; us ...

  4. 切片原型[start:stop:step]

    切片操作符在Python中的原型是 [start:stop:step] 步长值:默认是一个接着一个切取,如果为2,则表示进行隔一取一操作.步长值为正时表示从左向右取,如果为负,则表示从右向左取.步长值 ...

  5. 10大Web漏洞扫描工具

    Web scan tool 推荐10大Web漏洞扫描程序 Nikto 这是一个开源的Web服务器扫描程序,它可以对Web服务器的多种项目(包括3500个潜在的危险文件/CGI,以及超过900个服务器版 ...

  6. Django之ORM中事务和锁

    ORM事务: 事务: 数据库事务(简称:事务)是数据库管理系统执行过程中的一个逻辑单位,由一个有限的数据库操作序列构成. 事务的特点: 并非任意的对数据库的操作序列都是数据库事务.数据库事务拥有以下四 ...

  7. kali下一些代理工具的简单描述

    前言 最近几天了解了kali中一些代理工具的基本使用,做一个小小的总结,kali操作系统的官网为 www.kali.org,感兴趣的可以去官网下载镜像,如何安装这里就不在讲解了,百度有很多教程.新手这 ...

  8. MySQL(7)— 索引

    七.索引 MySQL官方对索引的定义为:索引(Index)是帮助 MySQL 高效 获取数据的数据结构. 7-1.索引的分类 主键索引 (primary key) 唯一的标识,主键不可重复,只能有一个 ...

  9. TCO14286 TriangleTriples

    题目链接:https://vjudge.net/problem/TopCoder-14286 知识点: 组合数学.容斥原理 题目大意: 给出 \(A,B,C\),问有多少个有序三元组 \((a,b,c ...

  10. 【JUC】synchronizated和lock的区别&新lock的优势

    原始构成 synchronized是关键字,属于JVM层面 javap -c 的结果显示 synchronized是可重入锁 11:是正常退出 17:是异常退出[保证不产生死锁和底层故障] Lock是 ...