LeetCode Construct the Rectangle
原题链接在这里:https://leetcode.com/problems/construct-the-rectangle/
题目:
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: 4
Output: [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.
题解:
从Math.sqrt(area) 开始往下一个一个试width, 看能否让area被width整除.
Time Complexity: O(Math.sqrt(area)). Space: O(1).
AC Java:
public class Solution {
public int[] constructRectangle(int area) {
if(area == 0){
return new int [] {0,0};
}
int w = (int)Math.sqrt(area);
while(area%w != 0){
w--;
}
return new int [] {area/w, w};
}
}
LeetCode Construct the Rectangle的更多相关文章
- LeetCode——Construct the Rectangle
LeetCode--Construct the Rectangle Question For a web developer, it is very important to know how to ...
- [LeetCode] Construct the Rectangle 构建矩形
For a web developer, it is very important to know how to design a web page's size. So, given a speci ...
- 【leetcode】492. Construct the Rectangle
problem 492. Construct the Rectangle 参考 1. Leetcode_492. Construct the Rectangle; 完
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- 1209. Construct the Rectangle
1209. Construct the Rectangle class Solution { public: /** * @param area: web page’s area * @retur ...
- 【LeetCode】492. Construct the Rectangle 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 python解法 日期 题目地址:ht ...
- LeetCode - 492. Construct the Rectangle
For a web developer, it is very important to know how to design a web page's size. So, given a speci ...
- LeetCode算法题-Construct the Rectangle(Java实现)
这是悦乐书的第243次更新,第256篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第110题(顺位题号是492).对于Web开发人员,了解如何设计网页的大小非常重要.因此 ...
随机推荐
- Linux的XServer
Moblin Core是在Gnome Mobile的平台上建立.我以前玩Linux,提交的都和图像没有关系,连Xwindows都不用启动,开机后直接进入文本命令行,所以这方面了解得很少,需要学习一下, ...
- jq中ajax的dataType:"json"是指什么?
dataType String 预期服务器返回的数据类型.如果不指定,jQuery 将自动根据 HTTP 包 MIME 信息来智能判断,比如XML MIME类型就被识别为XML.在1.4中,JSON就 ...
- perl FAQ(zz)
1. Why do you write a program in Perl? Ans : Easy to use and fast execution since perl script underg ...
- 20145230 《Java程序设计》第8周学习总结
20145230 <Java程序设计>第8周学习总结 教材学习内容 NIO与NIO2 NIO使用频道(Channel)来衔接数据节点,在处理数据时,NIO可以设定缓冲区(Buffer)容量 ...
- 克隆VMware虚拟机
虚拟机使用过程需要用到多个进行实验.重新安装时间又太长,通过vmware虚拟机自带软件能够很好的快速克隆出完全相同的系统. 环境:关闭VMware,不要在开启状态哦~ 步骤: 选中需要被克隆的系统 菜 ...
- Vue.js学习笔记 第二篇 样式绑定
Class绑定的对象语法 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- QQ空间如何设置被删除的好友不能访问空间
原来一直都很在乎.自己看着办. 经过简单的测试这种方法还是可以的啊,有问题到时候在说吧.理论上无论是否删除好友都可以限制对方访问空间. 旁边还有可以设置不能访问的名单(看仔细点-设置限制名单).
- 淘宝分类常见---部分显示和全部显示的js效果
需求就是,点击“更多按钮”,显示全部的分类详情,再次点击,显示部分分类. 展开: 收起: 结构: <div class="SubBox" id="SubBox&qu ...
- 我到 vim 配置文件---------修改从---http://www.cnblogs.com/ma6174/archive/2011/12/10/2283393.html
""""""""""""""""&quo ...
- HDU 2480 Steal the Treasure (并查集+贪心)
题意:给你n个点,m条边,包括有向边与无向边,每条边都有一个权值.在每个点上都有一个人,他可以走与这个点直接相连的所有边中任意一条边一次,并且得到这个权值,就不能走了,注意这条路也只能被一个人走.问最 ...