在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given the radius and x-y positions of the center of a circle, write a function Note:
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. 给定圆的半径和圆心的 x、y 坐标,写一个在圆中产生均匀随机点的函数 说明:
示例 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]] 输入语法说明: 输入是两个列表:调用成员函数名和调用的参数。 [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
|
请发表评论