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开发人员,了解如何设计网页的大小非常重要.因此 ...
随机推荐
- selection createTextRange setSelectionRange
http://www.cnblogs.com/rainman/archive/2011/02/27/1966482.html http://www.zhangxinxu.com/wordpress/2 ...
- php 跨域 form提交 2种方法
出于安全因素考虑,直接跨域访问是不允许的,下面介绍二种跨域的方法. 一,通过php curl function curlPost($url,$params) { $postData = ''; for ...
- linux音频 DAPM之二:audio paths与dapm kcontrol
转:https://blog.csdn.net/wh_19910525/article/details/12749293 在用alsa_amixer controls时,除了我们之前提到的snd_so ...
- linux 基础二---用户群租权限
用户&群组&权限 一.用户 1.用户及passwd文件 1) 掌握/etc/passwd文件的功能:存储所有用户的相关信息,该文件也被称为用户信息数据库(Database). 2) / ...
- no xxx find in java.library.path
JAVA系统运行时候load native lib时候会遇到下面错误,如 java.lang.UnsatisfiedLinkError: no JSTAF in java.library.path这可 ...
- linux的文件,目录操作命令(mv,rm,cp)
1.mv :用于重命名文件或目录:用于转移文件或目录 重命名文件或目录:$mv filename overfile ; $mv dirname overdir(必须是当前目录下没有的,否则操作的是转移 ...
- 让iOS项目允许使用http协议请求
苹果官方已经默认不让开发者使用不安全的http通信协议了,而是建议开发者使用安全的https协议.若我们还是需要使用http协议可以这样配置XCode: 1.打开info.plist文件 2.点击加号 ...
- HttpClient技术
1.什么是HttpClient? 2.HttpClient特点? 特点: 2.1. 基于标准.纯净的Java语言.实现了Http1.0和Http1.1 2.2. 以可扩展的面向对象的结构实现了Http ...
- 80X86寄存器详解<转载>
引子 打算写几篇稍近底层或者说是基础的博文,浅要介绍或者说是回顾一些基础知识, 自然,还是得从最基础的开始,那就从汇编语言开刀吧, 从汇编语言开刀的话,我们必须还先要了解一些其他东西, 像 CPU ...
- javascript(jquery)_匿名函数
一.什么是匿名函数 匿名函数:就是没有函数名的函数 二.为什么使用匿名函数 1.最大的用处就是创建闭包 三.匿名函数定义(怎么使用匿名函数) 1.这种方法使用了Function构造函数,把参数列表和函 ...