作者: 负雪明烛
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. R语言与医学统计图形-【10】ggplot2图形映射

    ggplot2绘图系统--图形映射 颜色的映射. #aes中映射变量 ggplot()+geom_point(aes(x=carat,y=price,color='blue'),#color视为单一变 ...

  2. Python编译工具Anaconda(含有spyder+jupyter)

    Anaconda的下载和安装 官方的下载地址:https://www.anaconda.com/distribution/ 安装程序为一个可执行程序文件,下载完成后双击执行程序即可完成安装.安装过程一 ...

  3. EXCEL excel中运用ctrl+D、ctrl+enter、ctrl+E批量填充数据

    在excel中,利用鼠标拖动可以快速向下或者向右填充序列或者复制单元格.但是利用快捷键也可以实现多种填充方式,本文就为大家介绍一些ctrl系列快捷键的填充,一起来看看吧. 封面tu 一,ctrl+D/ ...

  4. UE4类型数据自动注册

    Version:4.26.2 UE4 C++工程名:MyProject 在<宏GENERATED_BODY做了什么?>中,简单分析了GENERATED_BODY宏给一个简单的.继承自UOb ...

  5. C语言中的各种字符串输入方法

    C语言从stdin读取一行字符串的几种方法 gets gets函数的头文件是<stdio.h>,原型如下: char *gets(char *s); gets从stdin中读入一行内容到s ...

  6. idea 启动debug的时候throw new ClassNotFoundException(name)

    idea 启动debug的时候throw new ClassNotFoundException(name) 启动debug就跳转到此界面 解决办法 这个方法只是忽略了抛异常的点,并没有真正解决问题.后 ...

  7. 'this' pointer in C++

    The 'this' pointer is passed as a hidden argument to all nonstatic member function calls and is avai ...

  8. MyBatis(1):实现MyBatis程序

    一,MyBatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...

  9. 理解inode以及软硬连接,和inode磁盘爆满的解决方案以及文件权限

    理解Linux的软硬链接 创建硬链接的命令 [root@centos6 data]#ln /data/f1 /data/f2 [root@centos6 data]#ll -itotal 1613 - ...

  10. 玩转 Mockjs,前端也能跑的很溜

    ​ mockjs作用就是,生成随机模拟数据,拦截 ajax 请求,可以对数据进行增删改查.在生成数据时,我们就需要能够熟练使用 mock.js 的语法. Mockjs 的语法规范包括两部分:数据模板定 ...