[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 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
思路就是先找sqrt, 然后依次往下减, 一旦可以被整除, 就输出.
Code
class Solution:
def constructRectangle(self, area):
width = int(math.sqrt(area))
while area%width:
width -= 1
return area//width, width
[LeetCode] 492. Construct the Rectangle_Easy tag: Math的更多相关文章
- 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 ...
- LeetCode: 492 Construct the Rectangle(easy)
题目: or a web developer, it is very important to know how to design a web page's size. So, given a sp ...
- [LeetCode] 598. Range Addition II_Easy tag: Math
做个基本思路可以用 brute force, 但时间复杂度较高. 因为起始值都为0, 所以肯定是左上角的重合的最小的长方形就是结果, 所以我们求x, y 的最小值, 最后返回x*y. Code ...
- [LeetCode] 367. Valid Perfect Square_Easy tag:Math
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- [LeetCode] 172. Factorial Trailing Zeroes_Easy tag: Math
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- [LeetCode] 728. Self Dividing Numbers_Easy tag: Math
A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is ...
- [LeetCode] 106. Construct Binary Tree from Postorder and Inorder Traversal_Medium tag: Tree Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 【leetcode】492. Construct the Rectangle
problem 492. Construct the Rectangle 参考 1. Leetcode_492. Construct the Rectangle; 完
- (二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- Flask web开发之路十三
g对象 ### 保存全局变量的g属性:g:global1. g对象是专门用来保存用户的数据的.2. g对象在一次请求中的所有的代码的地方,都是可以使用的. 项目结构: g_demo.py文件代码: f ...
- python多行代码简化
python中,可以把多行代码简化为一行,把for循环和if条件判断都集中到一行里来写,示例如下: >>> from nltk.corpus import stopwords > ...
- python-Beautiful rose
热爱python,热爱生活,python需要浪漫,让我带大家走进浪漫的国度...写的不好的地方请大佬指教 import turtle import time class Rose: def __ini ...
- 阿里云mysql远程登录报ERROR 2027(HY000)
mysql远程登录的命令是: mysql -h数据库地址 -u用户名 -p 但是用这个命令在登录阿里云的mysql时,会报ERROR 2027 (HY000): Malformed packet
- 在ubuntu系统中,python依赖存放的路径
你在使用python,之后你想给python安装一些第三方库,如tensorflow或者tensorrt,那么这些包存放在哪个路径下呢? 该目录下: /usr/local/lib/python3.5/ ...
- hadoop内存配置方案
Configuration File Configuration Setting Value Calculation 8G VM (4G For MR) yarn-site.xml ...
- Page3:组合系统状态空间输入输出描述、矩阵指数函数性质[Linear System Theory]
内容包含组合系统的状态空间描述以及输入输出描述,零输入响应的概念以及矩阵指数函数的性质
- oracle优化:避免全表扫描
http://blog.csdn.net/onetree2010/article/details/6098259
- Shell脚本交互之:自动输入密码
Shell脚本交互之:自动输入密码 2016年04月09日 19:41:26 zhangjikuan 阅读数:58208 标签: Shell交互自动输入密码expect重定向管道 更多 个人分类: A ...
- 《Redis 数据操作》
一:字符串类型(string) - 应用场景 - 用于常规计数,常规的 key-value 存储. - 常用操作 常用操作 设置一个值为(字符串类型) SET key value 设置一个值并设置过 ...