您的位置:首页 > 其它

LeetCode "492. Construct the Rectangle"

2017-01-24 11:05 495 查看
Idea is, among all factors of the int, we pick the two that is the closest pair. And searching from sqrt(area) is a better idea:
https://discuss.leetcode.com/topic/76314/3-line-clean-and-easy-understand-solution
class Solution {
public:
vector<int> constructRectangle(int area) {
//defactor
vector<int> ret;
if(area<1) return ret;

int i = 1;
while(i <= area)
{
if(area % i == 0)
{
int a = i;
int b = area /i;
if(a>b) break;
if(a==b) return {a,b};
ret = {b, a};
}
i++;
}

return ret;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: