★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-zgesqvnw-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
For a web developer, it is very important to know how to design a web page's size. So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, whose length L and width W satisfy the following requirements:git
1. The area of the rectangular web page you designed must equal to the given target area.
2. The width W should not be larger than the length L, which means L >= W.
3. The difference between length L and width W should be as small as possible.
You need to output the length L and the width W of the web page you designed in sequence. github
Example:web
Input: 4 Output: [2, 2] Explanation: The target area is 4, and all the possible ways to construct it are [1,4], [2,2], [4,1]. But according to requirement 2, [1,4] is illegal; according to requirement 3, [4,1] is not optimal compared to [2,2]. So the length L is 2, and the width W is 2.
Note:微信
做爲一位web開發者, 懂得怎樣去規劃一個頁面的尺寸是很重要的。 現給定一個具體的矩形頁面面積,你的任務是設計一個長度爲 L 和寬度爲 W 且知足如下要求的矩形的頁面。要求:ui
1. 你設計的矩形頁面必須等於給定的目標面積。 2. 寬度 W 不該大於長度 L,換言之,要求 L >= W 。 3. 長度 L 和寬度 W 之間的差距應當儘量小。
你須要按順序輸出你設計的頁面的長度 L 和寬度 W。spa
示例:設計
輸入: 4 輸出: [2, 2] 解釋: 目標面積是 4, 全部可能的構造方案有 [1,4], [2,2], [4,1]。 可是根據要求2,[1,4] 不符合要求; 根據要求3,[2,2] 比 [4,1] 更能符合要求. 因此輸出長度 L 爲 2, 寬度 W 爲 2。
說明:code
8mshtm
1 class Solution { 2 func constructRectangle(_ area: Int) -> [Int] { 3 var w = Int(sqrt(Double(area))) 4 while area % w != 0 { 5 w -= 1 6 } 7 return [area / w, w] 8 } 9 }
8ms
1 class Solution { 2 func constructRectangle(_ area: Int) -> [Int] { 3 var minDiff = Int.max 4 var minDiffWidth: Int? 5 let maxWidth = Int(ceil(sqrt(Double(area)))) 6 7 for width in 1 ... maxWidth { 8 guard area % width == 0 else { continue } 9 10 let length = area / width 11 if length >= width, length - width < minDiff { 12 minDiff = length - width 13 minDiffWidth = width 14 } 15 } 16 if let minDiffWidth = minDiffWidth { 17 return [area / minDiffWidth, minDiffWidth] 18 } else { 19 return [area, 1] 20 } 21 } 22 }
12ms
1 class Solution { 2 func constructRectangle(_ area: Int) -> [Int] { 3 let maxW = Int(sqrt(Double(area))) 4 var result = [area,1] 5 if maxW >= 2 { 6 for i in 2...maxW { 7 if area % i == 0 { 8 result = [area / i,i] 9 } 10 } 11 } 12 return result 13 } 14 }
12ms
1 class Solution { 2 func constructRectangle(_ area: Int) -> [Int] { 3 var n = Int(sqrt(Double(area))) 4 while area % n != 0 { 5 n -= 1 6 } 7 return [max(n, area/n), min(n, area/n)] 8 } 9 }
20ms
1 class Solution { 2 func constructRectangle(_ area: Int) -> [Int] { 3 var maxW = Int(sqrt(Double(area))) 4 var result = [area,1] 5 while maxW >= 2 { 6 if area % maxW == 0 { 7 result = [area / maxW,maxW] 8 break 9 } 10 maxW -= 1 11 } 12 return result 13 } 14 }
116ms
1 class Solution { 2 func constructRectangle(_ area: Int) -> [Int] { 3 guard area > 0 else { return [0, 0] } 4 let sqr = sqrt(Double(area)) 5 var i = Int(sqr) 6 var j = Int(sqr) 7 var prod = i*j 8 while prod != area { 9 if prod > area { 10 i -= 1 11 } else if prod < area { 12 j += 1 13 } 14 prod = i*j 15 } 16 return [j, i] 17 18 } 19 }