【leetcode刷题】[简单]492. 构造矩形(construct the rectangle)-java

构造矩形 construct the rectangle

  • 题目
  • 分析
  • 解答

题目

作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的。 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面。要求:

1. 你设计的矩形页面必须等于给定的目标面积。

2. 宽度 W 不应大于长度 L,换言之,要求 L >= W 。

3. 长度 L 和宽度 W 之间的差距应当尽可能小。

你需要按顺序输出你设计的页面的长度 L 和宽度 W。

示例:

输入: 4
输出: [2, 2]
解释: 目标面积是 4, 所有可能的构造方案有 [1,4], [2,2], [4,1]。
但是根据要求2,[1,4] 不符合要求; 根据要求3,[2,2] 比 [4,1] 更能符合要求. 所以输出长度 L 为 2, 宽度 W 为 2。

代码模板:

class Solution {
    public int[] constructRectangle(int area) {
        
    }
}

分析

首先求出平方根,再从平方根来找整除,找得到输出出来。

解答

class Solution {
    public int[] constructRectangle(int area) {
        int width=(int) Math.sqrt(area);
        int[] result = new int [2];
        while(width>0){
            if(area % width == 0){
                result[0] = area/width;
                result[1] = width;
                return result;
            }
            width--;
        }
        return result;
    }
}

你可能感兴趣的:(Java基础学习,算法)