Straightforward Java solution


  • 1

    This problem links me to Sqrt(x). Very similar idea:

    
        public int[] constructRectangle(int area) {
            int i = 0, j = area;
            int[] result = new int[2];
            while (i <= j){
                long product = i*j;
                if (product == area){
                    result[0] = j--;
                    result[1] = i++;
                } else if (product > area){
                    j--;
                } else {
                    i++;
                }
            }
            return result;
        }
    

    Also viewable here.


Log in to reply
 

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