题目:

or 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:

  1. The given area won't exceed 10,000,000 and is a positive integer
  2. The web page's width and length you designed must be positive integers.

代码:

思路:求0到平方根范围内的最大因子。

 class Solution {
public:
vector<int> constructRectangle(int area) {
int j = sqrt(area);
for (j; j>; j--){
if (area%j == )
break;
}
vector<int> result = {area/j, j};
return result;
}
};

别人的:

 class Solution {
public:
vector<int> constructRectangle(int area) {
int w = sqrt(area);
while (area % w) w--;
return vector<int>({area/w, w});
}
};

LeetCode: 492 Construct the Rectangle(easy)的更多相关文章

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

  2. 【leetcode】492. Construct the Rectangle

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

  3. 【LeetCode】492. Construct the Rectangle 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 python解法 日期 题目地址:ht ...

  4. [LeetCode&Python] Problem 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 ...

  5. [LeetCode] 492. Construct the Rectangle_Easy tag: Math

    For a web developer, it is very important to know how to design a web page's size. So, given a speci ...

  6. 492 Construct the Rectangle 构建矩形

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

  7. 492. Construct the Rectangle

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  8. LeetCode——Construct the Rectangle

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

  9. leetcode面试准备: Maximal Rectangle

    leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...

随机推荐

  1. cocos2d-js添加百度MSSP插屏(通过jsb反射机制)

    1.导入jar包.... 2.修改AndroidManifest.xml文件 添加: <meta-data android:name="BaiduMobAd_APP_ID" ...

  2. haproxy + keepalived 实现web 双主模型的高可用负载均衡

    参考文章 http://xz159065974.blog.51cto.com/8618592/1405812 http://blog.chinaunix.net/uid-25266990-id-398 ...

  3. mysql一:操作数据库

    创建数据库是指在数据库空间中划出一块空间用来存储相关的数据,表就是存储在对应的数据库里面.首先来看下查看数据库的命令:show databases. 这个是用来查询数据库空间下所有的数据库,其中inf ...

  4. pm2 的使用

    pm2.json 代码如下 [{ "name" : "dingtalk-mobile", "script" : "app.js&q ...

  5. 销售订单增强字段 bapi更新

    如果增强字段在销售订单抬头(vbak)上,则要将增强字段一并append到如下四个表/结构中: VBAKKOZ VBAKKOZX BAPE_VBAK BAPE_VBAKX 在行项目(vbap)上: V ...

  6. 采集练习(十二) python 采集之 xbmc 酷狗电台插件

    前段时间买了个树莓派才知道有xbmc这么强大的影音软件(后来我逐渐在 电脑.手机和机顶盒上安装xbmc),在树莓派上安装xbmc后树莓派就成为了机顶盒,后面在hdpfans论坛发现了jackyspy  ...

  7. 编写你的第一个django应用程序2

    从1停止的地方开始,我们将设置数据库,创建您的第一个模型,并快速介绍django自动生成的管理站点 数据库设置 现在,打开mysite/settings.py.这是一个普通的python模块,其中模块 ...

  8. Django的基础操作总结

    1:准备开始 建立一个新的project: django-admin.py startproject XXXXXX(名称) 建立一个新的App:python manage.py startapp XX ...

  9. h大数据

    安全认证 hw HBase安全认证(创建HBaseHolder时认证) String userPrincipal = FeatureContext.INSTANCE.getOrElse(Constan ...

  10. TensorFlow框架(6)之RNN循环神经网络详解

    1. RNN循环神经网络 1.1 结构 循环神经网络(recurrent neural network,RNN)源自于1982年由Saratha Sathasivam 提出的霍普菲尔德网络.RNN的主 ...