492. Construct the Rectangle
来源:互联网 发布:哈尔滨商业大学知乎 编辑:程序博客网 时间:2024/06/13 06:26
For a web developer, it is very important to know how to design a web page’s size. So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, whose length L and width W satisfy the following requirements:
1. The area of the rectangular web page you designed must equal to the given target area.2. The width W should not be larger than the length L, which means L >= W.3. The difference between length L and width W should be as small as possible.
You need to output the length L and the width W of the web page you designed in sequence.
Example:
Input: 4Output: [2, 2]Explanation: The target area is 4, and all the possible ways to construct it are [1,4], [2,2], [4,1]. But according to requirement 2, [1,4] is illegal; according to requirement 3, [4,1] is not optimal compared to [2,2]. So the length L is 2, and the width W is 2.
Note:
The given area won’t exceed 10,000,000 and is a positive integer
The web page’s width and length you designed must be positive integers.
public class Solution { public int[] constructRectangle(int area) { int[] res = new int[2]; int w = 0; int l = 0; for(int i = 1;i <= (int)Math.sqrt(area);i++){ if(area % i == 0){ w = i; l = area / i; } } res[0] = l; res[1] = w; return res; }}
阅读全文
0 0
- 492. Construct the Rectangle
- 492. Construct the Rectangle
- 492. Construct the Rectangle
- 492. Construct the Rectangle
- 492. Construct the Rectangle
- 492. Construct the Rectangle
- 492. Construct the Rectangle
- 492. Construct the Rectangle
- 492. Construct the Rectangle
- 492. Construct the Rectangle
- 492. Construct the Rectangle
- 【Leetcode】492. Construct the Rectangle
- LeetCode 492. Construct the Rectangle
- 492. Construct the Rectangle (E)
- 492. Construct the Rectangle [LeetCode]
- LeetCode-492. Construct the Rectangle
- leetcode 492. Construct the Rectangle
- LeetCode | 492. Construct the Rectangle
- php递归遍历所有目录及子目录和文件实例
- 去除inline-block元素间间距的N种方法
- Wdatepicker选择时间后触发事件
- CTF练习资源大全集
- JavaScript Cookies
- 492. Construct the Rectangle
- ios-如何取消按钮的高亮状态
- 利用二叉树的非递归后序遍历求解最近公共祖先问题
- Oracle与MySQL的几点区别
- 机器人的运动范围
- RCNN学习笔记(2):Rich feature hierarchies for accurate object detection and semantic segmentation
- 从今天开始写博客
- 港科大教授张晓泉:目前AI多做预测分类,还需经济学发现因果关系
- Java8 Stream API之创建Stream示例