作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的。 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面。要求:

1. 你设计的矩形页面必须等于给定的目标面积。 2. 宽度 W 不应大于长度 L,换言之,要求 L >= W 。 3. 长度 L 和宽度 W 之间的差距应当尽可能小。

你需要按顺序输出你设计的页面的长度 L 和宽度 W。

示例:

输入: 4 输出: [2, 2] 解释: 目标面积是 4, 所有可能的构造方案有 [1,4], [2,2], [4,1]。 但是根据要求2,[1,4] 不符合要求; 根据要求3,[2,2] 比 [4,1] 更能符合要求. 所以输出长度 L 为 2, 宽度 W 为 2。

说明:

  1. 给定的面积不大于 10,000,000 且为正整数。
  2. 你设计的页面的长度和宽度必须都是正整数。
class Solution {
public:
vector<int> constructRectangle(int area) {
int x = sqrt(area);
vector<int> res;
int l, w;
for(int i = x; i >= 1; i++)
{
if(area % i == 0)
{
l = max(i, area / i);
w = min(i, area / i);
break;
}
}
res.push_back(l);
res.push_back(w);
return res;
}
};

Leetcode492.Construct the Rectangle构造矩形的更多相关文章

  1. [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 ...

  2. 492 Construct the Rectangle 构建矩形

    详见:https://leetcode.com/problems/construct-the-rectangle/description/ C++: class Solution { public: ...

  3. 【leetcode】492. Construct the Rectangle

    problem 492. Construct the Rectangle 参考 1. Leetcode_492. Construct the Rectangle; 完

  4. LeetCode——Construct the Rectangle

    LeetCode--Construct the Rectangle Question For a web developer, it is very important to know how to ...

  5. 1209. Construct the Rectangle

    1209. Construct the Rectangle class Solution { public: /** * @param area: web page’s area * @retur ...

  6. Java实现 LeetCode 492 构造矩形

    492. 构造矩形 作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的. 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面.要求: 你设 ...

  7. [Swift]LeetCode492. 构造矩形 | 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 ...

  8. [LeetCode] Maximal Rectangle 最大矩形

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  9. [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 ...

随机推荐

  1. Java核心-03 谈谈final、finally、 finalize有什么不同?

    今天,我要问你的是一个经典的 Java 基础题目,谈谈 final.finally. finalize 有什么不同? 典型回答 final 可以用来修饰类.方法.变量,分别有不同地意义,final修饰 ...

  2. Maven使用初步

    一.安装: 1.下载:http://maven.apache.org/download.cgi 2.设置环境变量 1.创建"M2_HOME",值为Maven安装路径(如:D:\Ja ...

  3. JConsole&VisualVM监控总结

    简介JConsole(以下写作jconsole),VisualVM(以下写作jvisualvm ) 都是比较好的JVM调优工具,且都为JDK自带,可在命令行直接启动. 监控示例Server端(需要监控 ...

  4. matlab之原始处理图像几何变换

    (一)图像几何变换理论知识 (1)图像的平移与比例 图像的平移很简单,平移前后的坐标分别为(x,y)和(x',y'),则满足的关系式为 x'= x +Tx: y'= y +Ty: 其中Tx与Ty分别为 ...

  5. Android开发 音视频开发需要了解的专业术语知识

    前言 在摸索一段时间的音视频开发后,越来越发现这个坑的深度真是特别的深. 除了了解Android自带的音视频处理API以外,还得了解一些视频与音频方面的知识.这篇博客就是主要讲解这方面的专业术语.内容 ...

  6. signed main()

    主函数由int main()改成signed main() 好处:把int改成long long 的时候不用单独把它改成int了,懂的人都懂(滑稽

  7. 并发和多线程(九)--AbstractQueuedSynchronizer排他锁基本原理

    AbstractQueuedSynchronizer简称为AQS,AQS是ReentrantLock.CountdownLatch.CycliBarrier等并发工具的原理/基础,所以了解AQS的原理 ...

  8. 记录一次dubbo不能正常抛出特定异常

    BUG场景 今天同事的代码中出现一个问题,让我帮忙排查一下.原代码大致如下 dubbo服务消费者: @Resource private IPayWayService payWayService; @R ...

  9. BZOJ 3245 最快路线

    和道路升级差不多,只是用的spfa; 十分有毒,在BZOJ上一直WA,对拍拍出来是一样的却告诉我不一样,然后发现自己把'\n'写成了‘\b’... #include<cstdio> #in ...

  10. Sql Server 中查询存储过程的修改时间

    1.按最近修改排序所有存储过程 SELECT [name], [create_date], [modify_date] FROM [sys].[objects] WHERE [type] = 'P' ...