TreeviewCopyright © aleen42 all right reserved, powered by aleen42
492. 构造矩形
https://leetcode-cn.com/problems/construct-the-rectangle/
Java
class Solution {
public int[] constructRectangle(int area) {
int sqrt = (int)Math.sqrt(area);
int width = sqrt, length = sqrt;
while(width <= length) {
if(width * length == area) {
return new int[]{length, width};
}else if(width * length < area) {
length++;
}else {
width--;
}
}
return new int[]{0, 0};
}
}
Python
'''
Author: Goog Tech
Date: 2020-08-28 06:58:52
LastEditTime: 2020-08-28 06:59:29
Description: https://leetcode-cn.com/problems/construct-the-rectangle/
FilePath: \leetcode-googtech\#492. Construct the Rectangle\Solution.py
WebSite: https://algorithm.show/
'''
class Solution(object):
def constructRectangle(self, area):
"""
:type area: int
:rtype: List[int]
"""
sqrt = math.sqrt(area)
intSqrt = int(sqrt)
length, width = intSqrt, intSqrt
while width <= length:
if length * width == area:
return [length, width]
elif length * width < area:
length += 1
else:
width -= 1
return [0, 0]