算法笔记: 力扣#492 构造矩形

问题描述


解法


分析

Python 实现

1
2
3
4
5
6
7
8
9
10
class :
def constructRectangle(self, area):
"""
:type area: int
:rtype: List[int]
"""
width = int(area**0.5)
while area % width:
width -= 1;
return area//width, width

Java 实现

1
2
3
4
5
6
7
8
9
10
11
class {
public int[] constructRectangle(int area) {
int[] ans = new int[2];
ans[1] = (int)Math.sqrt(area);
while(area%ans[1]!=0){
ans[1]--;
}
ans[0] = area / ans[1];
return ans;
}
}

时间复杂度

O(n).

空间复杂度

O(1).

链接


492. Construct the Rectangle
492. 构造矩形
(English version) Algorithm Notes: Leetcode#492 Construct the Rectangle