LeetCode "492. Construct the Rectangle"

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;
    }
};
原文地址:https://www.cnblogs.com/tonix/p/6346677.html