[Swift]LeetCode478. 在圓內隨機生成點 | Generate Random Point in a Circle

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-girlylwo-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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.git

Note:github

  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:web

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

Example 2:數組

Input: 
["Solution","randPoint","randPoint","randPoint"]
[[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.app


給定圓的半徑和圓心的 x、y 座標,寫一個在圓中產生均勻隨機點的函數 randPoint 。dom

說明:ide

  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 沒有參數。輸入參數是一個列表,即便參數爲空,也會輸入一個 [] 空列表。


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  
相關文章
相關標籤/搜索