Simple C++


  • 0
    S

    class Solution {
    public:
    vector<int> constructRectangle(int area) {
    vector<int> vec(2,0);
    int a,b;
    a=b=sqrt(area);
    while(ab!=area){
    if(a
    b>area) a--;
    if(a*b<area) b++;
    }
    vec[1]=a; vec[0]=b;
    return vec;
    }
    };


  • 0
    S

    Sorry, as first time submit code, seems lose some ' * ' during the copy


  • 0
    L

    @Shield_David Thank you for sharing your solution.
    I have a question: how do you know that
    while(ab!=area){
    if(ab>area) a--;
    if(a*b<area) b++;
    }
    will make axb converge to the area?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.