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开发人员,了解如何设计网页的大小非常重要.因此 ...
随机推荐
- 前端之 Ajax(补)
概述 对于WEB应用程序:用户浏览器发送请求,服务器接收并处理请求,然后返回结果,往往返回就是字符串(HTML),浏览器将字符串(HTML)渲染并显示浏览器上. 1.传统的Web应用 一个简单操作需要 ...
- 跟踪 twisted 里deferred 的Callback
twisted 提供了 deferred 机制,而关键点就是回调.通过查看deferred 源码 (version 8.2.0)我们可以 看到 deferred的addCallback是怎么工作的,以 ...
- Linux下32位与64位数据类型大小
Redhat Enterprise Linux 32 Redhat Enterprise Linux 64
- 【leetcode刷题笔记】Excel Sheet Column Number
Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, retur ...
- 一个由自增运算符以及C语法顺序细节引起的bug
一.问题描述 在编写modbus代码时发生一件由语法细节引起的bug,起因是自增运算符以及C语法顺序. 输入的数据是2233=0X08B9,高低字节顺序是0x08 0xB9, 使用modbus po ...
- java屏幕截取
CaptureScreen.java ```import java.awt.Dimension; import java.awt.Rectangle; import java.awt.Robot; i ...
- iOS_UIAlertController
ViewController.m文件 #import "ViewController.h" @interface ViewController () // @property (n ...
- 为多个文件夹下的C源代码编写Makefile文件
上一篇文章写了如何为在同一个文件夹下的C源代码,本篇文章为多个文件夹下的C源代码编写Makefile文件. 建立两个文件夹,分别为abs与src.其最终目录结构如下: 1 $ ls * 2 jun.c ...
- QT Creator 代码自动补全
QT Creator 代码自动补全 用QT Creater编程,如果没有自动补全是很痛苦的事情,于是便查阅了QT的文档,发现CTRL+SPACE是自 动补全的快捷键;但是在 Creater里使用居然没 ...
- 查看后台PHP进程(非PHP-FPM)
ps -ef | grep php | grep -v php-fpm