• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode478.在圆内随机生成点|GenerateRandomPointinaCircle

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10348304.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Given the radius and x-y positions of the center of a circle, write a function randPoint which generates a uniform random point in the circle.

Note:

  1. input and output values are in floating-point.
  2. radius and x-y position of the center of the circle is passed into the class constructor.
  3. a point on the circumference of the circle is considered to be in the circle.
  4. randPoint returns a size 2 array containing x-position and y-position of the random point, in that order.

Example 1:

Input: 
[[1,0,0],[],[],[]]
Output: [null,[-0.72939,-0.65505],[-0.78502,-0.28626],[-0.83119,-0.19803]]

Example 2:

Input: 
[[10,5,-7.5],[],[],[]]
Output: [null,[11.52438,-8.33273],[2.46992,-16.21705],[11.13430,-12.42337]]

Explanation of Input Syntax:

The input is two lists: the subroutines called and their arguments. Solution's constructor has three arguments, the radius, x-position of the center, and y-position of the center of the circle. randPoint has no arguments. Arguments are always wrapped with a list, even if there aren't any.


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

说明:

  1. 输入值和输出值都将是浮点数
  2. 圆的半径和圆心的 x、y 坐标将作为参数传递给类的构造函数。
  3. 圆周上的点也认为是在圆中。
  4. 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 没有参数。输入参数是一个列表,即使参数为空,也会输入一个 [] 空列表。


[Swift]LeetCode470. 用 Rand7() 实现 Rand10() | Implement Rand10() Using Rand7()
Runtime: 736 ms
Memory Usage: 17.5 MB
 1 class Solution {
 2     var r:Double
 3     var centerX:Double
 4     var centerY:Double
 5 
 6     init(_ radius: Double, _ x_center: Double, _ y_center: Double) {
 7         self.r = radius
 8         self.centerX = x_center
 9         self.centerY = y_center        
10     }
11     
12     func randPoint() -> [Double] {
13         while(true)
14         {
15             var x:Double = (2 * Double.random(in: 0..<1) - 1.0) * r
16             var y:Double = (2 * Double.random(in: 0..<1) - 1.0) * r
17 
18             if x * x + y * y <= r * r
19             {
20                 return [centerX + x, centerY + y]
21             }
22         }
23     }
24 }
25 
26 /**
27  * Your Solution object will be instantiated and called as such:
28  * let obj = Solution(radius, x_center, y_center)
29  * let ret_1: [Double] = obj.randPoint()
30  */
31  

Runtime: 748 ms

Memory Usage: 17.5 MB
 1 class Solution {
 2     var r:Double
 3     var centerX:Double
 4     var centerY:Double
 5 
 6     init(_ radius: Double, _ x_center: Double, _ y_center: Double) {
 7         self.r = radius
 8         self.centerX = x_center
 9         self.centerY = y_center        
10     }
11     
12     func randPoint() -> [Double] {
13         var theta:Double = 2 * M_PI * (Double.random(in: 0..<1))
14         var len:Double = sqrt(Double.random(in: 0..<1)) * r
15         return [centerX + len * cos(theta), centerY + len * sin(theta)]      
16     }
17 }
18 
19 /**
20  * Your Solution object will be instantiated and called as such:
21  * let obj = Solution(radius, x_center, y_center)
22  * let ret_1: [Double] = obj.randPoint()
23  */
24  

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap