[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 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.
这道题让我们根据面积来求出矩形的长和宽,要求长和宽的差距尽量的小,那么就是说越接近正方形越好。那么我们肯定是先来判断一下是不是正方行,对面积开方,如果得到的不是整数,说明不是正方形。那么我们取最近的一个整数,看此时能不能整除,如果不行,就自减1,再看能否整除。最坏的情况就是面积是质数,最后减到了1,那么返回结果即可,参见代码如下:
解法一:
class Solution {
public:
vector<int> constructRectangle(int area) {
int r = sqrt(area);
while (area % r != ) --r;
return {area / r, r};
}
};
如果我们不想用开方运算sqrt的话,那就从1开始,看能不能整除,循环的终止条件是看平方值是否小于等于面积,参见代码如下:
解法二:
class Solution {
public:
vector<int> constructRectangle(int area) {
int r = ;
for (int i = ; i * i <= area; ++i) {
if (area % i == ) r = i;
}
return {area / r, r};
}
};
参考资料:
https://discuss.leetcode.com/topic/76469/3ms-concise-c
https://discuss.leetcode.com/topic/76314/3-line-clean-and-easy-understand-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Construct the Rectangle 构建矩形的更多相关文章
- 492 Construct the Rectangle 构建矩形
详见:https://leetcode.com/problems/construct-the-rectangle/description/ C++: class Solution { public: ...
- LeetCode——Construct the Rectangle
LeetCode--Construct the Rectangle Question For a web developer, it is very important to know how to ...
- [LeetCode] 85. Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...
- LeetCode Construct the Rectangle
原题链接在这里:https://leetcode.com/problems/construct-the-rectangle/ 题目: For a web developer, it is very i ...
- Leetcode492.Construct the Rectangle构造矩形
作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的. 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面.要求: 1. 你设计的矩形页面必 ...
- LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)
题目描述 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定的 ...
- 【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 ...
随机推荐
- 数据库(Mongodb)
1.MongoClient()函数 In [8]: import pymongo In [9]: con = pymongo.MongoClient('localhost') #建立连接 In [10 ...
- java基础笔记(3)----函数
前言引入函数前,所有的代码都写在main主函数中,代码过多,代码冗余,可读性差. 引入函数后,函数是实现某一特定功能的代码块.一个类中可以定义多个函数,每个函数和main主函数都是并列关系. 函数: ...
- bootstrap-table分页数据前台不显示
问题:后台返回数据{"total":52,"rows":[{"ztname":"2007年新会计准则科目(李相)",&q ...
- Python数据增强(data augmentation)库--Augmentor 使用介绍
Augmentor 使用介绍 原图 random_distortion(probability, grid_height, grid_width, magnitude) 最终选择参数为 p.rando ...
- 配置tomcat8数据源(采用局部数据源方式)
tomcat提供两种数据源配置方式,全局和局部.全局的话对于所有web应用都生效,局部只对于配置的某一个web生效. 步骤: 1.将mysql的jdbc驱动复制到tomcat的lib路径下. 2.在t ...
- 个人作业2——NBA 2k18案例分析
产品:篮球体育类游戏NBA 2k18 选择理由:这款游戏是<NBA 2k>的正统续作,自己和身边的朋友都对篮球比较感兴趣,经常看NBA,所以近几年的版本都有购买下载,加上游戏中人物动作比较 ...
- org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jsp/jstl/core cannot be res
解决:web项目出现如上问题,据查是版本问题: JSTL 1.0 的声明是: <%@ taglib prefix="c" uri="http://java.sun. ...
- 利用Python爬取新浪微博营销案例库并下载到本地
from bs4 import BeautifulSoup import requests,urllib.request,urllib.parse import json import time im ...
- Python多线程案例
from time import ctime,sleep import threading def music(): for i in range(2): print ("I was lis ...
- 几款有用的AndroidStudio插件
1.Android Parcelable code generator 顾名思义,这是个生成实现了Parcelable接口的代码的插件. 在你的类中,按下alt + insert键弹出插入代码的上下文 ...