作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/construct-the-rectangle/#/description

题目描述

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.

题目大意

现在要设计一个矩形网页,要求其面积是area,并且要求这个矩形的L >= W,返回L和W尽可能接*的矩形。

解题方法

Java解法

这个题目的意思其实就是找出指定数的约数,并且约数尽量靠**方根,第一个约数的值大于第二个约数。

给定的数字可以很大,千万量级,那么只能用O(n)量级的算法了。我的想法就是首先找出*方根,因为约数肯定尽量靠*了*方根。用到了数学运算sqrt,这个*方根是个double型的,转换成int型会舍去末尾小数。这样,如果强转之后的int的*方等于area,说明area能是个完全*方数,直接把*方根返回就好。否则,说明area不是*方数,只能进行遍历了。遍历的方法是从*方根强转后的int开始越来越小的遍历,这样,保证了尽量靠*了*方根。因为强转是丢失小数的,所以只能往小了遍历,才不会出现错误。然后如果能整除area,计算响应的长宽即可。注意此时的i是小的,那么应该对应宽。

循环的过程中一定不要忘记break,否则会在遍历完到1才停止。

public class Solution {
public int[] constructRectangle(int area) {
double sqrt = Math.sqrt(area);
int int_sqrt = (int) sqrt;
int []answer = new int[2];
if(int_sqrt * int_sqrt == area){
answer[0] = int_sqrt;
answer[1] = int_sqrt;
}else{
for(int i= int_sqrt; i >= 1; i--){
if(area % i == 0){
answer[0] = area / i;
answer[1] = i;
break;//不要忘记
}
}
}
return answer;
}
}

我的上面的想法是可行的,但是有点麻烦。可以这么简化,不用判断强转之后是否是*方根,直接循环判断。

public class Solution {
public int[] constructRectangle(int area) {
int w = (int) Math.sqrt(area);
while(area % w != 0){
w--;
}
return new int[]{area / w, w};
}
}

python解法

class Solution(object):
def constructRectangle(self, area):
"""
:type area: int
:rtype: List[int]
"""
sqrt = int(math.sqrt(area))
for w in range(sqrt, 0, -1):
if area % w == 0:
return [area / w, w]
return [area, 1]

日期

2017 年 4 月 3 日
2018 年 11 月 15 日 —— 时间太快,不忍直视

【LeetCode】492. Construct the Rectangle 解题报告(Java & Python)的更多相关文章

  1. 【LeetCode】383. Ransom Note 解题报告(Java & Python)

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

  2. 【LeetCode】575. Distribute Candies 解题报告(Java & Python)

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

  3. 【LeetCode】136. Single Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...

  4. 【LeetCode】85. Maximal Rectangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximal- ...

  5. 【LeetCode】283. Move Zeroes 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...

  6. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  7. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  8. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  9. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

随机推荐

  1. 日常Java 2021/11/15

    Applet类 每一个Applet都是java.applet Applet类的子类,基础的Applet类提供了供衍生类调用的方法,以此来得到浏览器上下文的信息和服务.这些方法做了如下事情: 得到App ...

  2. day13 cookie与session和中间件

    day13 cookie与session和中间件 今日内容概要 cookie与session简介 django操作cookie与session django中间件简介 如何自定义中间件 csrf跨站请 ...

  3. Linux定时任务crontable简介

    Linux下定时执行任务的方法:Linux之crond 服务介绍:https://www.cnblogs.com/liang-io/p/9596294.html http://www.mamicode ...

  4. oracle 日期语言格式化

    TO_DATE ('17-JUN-87', 'dd-mm-yy', 'NLS_DATE_LANGUAGE = American')

  5. Output of C++ Program | Set 16

    Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...

  6. Mave 下载与安装

    一,Maven 介绍 我们在开发中经常需要依赖第三方的包,包与包之间存在依赖关系,版本间还有兼容性问题,有时还需要将旧的包升级或降级,当项目复杂到一定程度时包管理变得非常重要.Maven是当前最受欢迎 ...

  7. 【编程思想】【设计模式】【创建模式creational】原形模式Prototype

    Python版 https://github.com/faif/python-patterns/blob/master/creational/prototype.py #!/usr/bin/env p ...

  8. RPC、HTTP、RESTful

    RESTful RESTFUL是一种网络应用程序的设计风格和开发方式,基于HTTP,可以使用XML格式定义或JSON格式定义.RESTFUL适用于移动互联网厂商作为业务使能接口的场景,实现第三方OTT ...

  9. C/C++ Qt 数据库SqlRelationalTable关联表

    在上一篇博文中详细介绍了SqlTableModle组件是如何使用的,本篇博文将介绍SqlRelationalTable关联表组件,该组件其实是SqlTableModle组件的扩展类,SqlRelati ...

  10. pipeline option指令

    目录 一.简介 二.参数 buildDiscarder checkoutToSubdirectory disableConcurrentBuilds newContainerPerStage retr ...