Construct the Rectangle

来源:互联网 发布:安卓app推荐 知乎 编辑:程序博客网 时间:2024/06/07 22:41

题目:https://leetcode.com/submissions/detail/99862054/

先找到平方根,再用类似二分法的方式查找遍历,得到最优解。

vector<int> constructRectangle(int area) {        vector<int> res;        int w = sqrt(area);        if(w*w == area)        {            res.push_back(w);            res.push_back(w);            return res;        }        int l = w+1;        while(w>=1 && l<=area)        {            int tmp = w*l;            if(tmp == area)            {                res.push_back(l);                res.push_back(w);                return res;            }            if(tmp > area)            {                w--;            }            else            {                l++;            }        }        return res;    }
0 0
原创粉丝点击