【leetcode】492. Construct the Rectangle

problem

492. Construct the Rectangle

 solution1:

class Solution {
public:
    vector<int> constructRectangle(int area) {
        int r = sqrt(area);
        while(area % r != 0) r--;
        return {area/r, r};
    }
};

 

solution2:

class Solution {
public:
    vector<int> constructRectangle(int area) {
        int r = 1;
        for(int i=1; i*i <=area; i++)
        {
            if(area % i == 0) r = i;
        }
        return {area/r, r};
    }
};

 

 

参考

1. Leetcode_492. Construct the Rectangle;

2. GrandYang;

posted on 2019-03-29 19:07  鹅要长大  阅读(168)  评论(0编辑  收藏  举报

导航