[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 ...
随机推荐
- 极验(geetest)验证码
最近在做项目的时候,需要用到登录验证,在网上看到了一个很不错的验证插件,在此记录一下使用流程. 极限验证码 官网:http://www.geetest.com/,到GitHub下载服务端代码htt ...
- git命令无法自动补全(sles11.3)
找到git-completion bash文件 find / -name 'git-completion' /usr/share/doc/git-1.7.1/contrib/completion/gi ...
- WPF .NET 4.0 OpenClipboard 失败 (异常来自 HRESULT:0x800401D0 (CLIPBRD_E_CANT_OPEN)) BUG解决
在项目 App.xaml 文件下 => Application 节点=> 添加 DispatcherUnhandledException="Application_Dispa ...
- 终于知道什么情况下需要实现.NET Core中的IOptions接口
自从接触 IOptions 之后,一直纠结这样的问题:自己定义的 Options 要不要实现 IOptions 接口. 微软有的项目中实现了,比如 Caching 中的 MemoryCacheOpti ...
- [No0000DD]C# StringEx 扩展字符串类 类封装
using System; using System.Text.RegularExpressions; namespace Helpers { /// <summary> /// 包含常用 ...
- shell脚本的基本结构以及如何执行
1.shell脚本通常以.sh为后缀,不是说不带这个.sh脚本就不会执行,而是大家一种通用的命名规范而已 2.shell脚本中的第一行一定是:#! /bin/bash.该命令说明,该文件使用的是bas ...
- React 60S倒计时
React 60S倒计时 1.设置状态: 2.函数主体: 3.应用: 4..效果图:
- React event
React event 组件: React 自有方法 用户定义方法 一.虚拟事件对象 事件处理器将会传入 虚拟事件对象 的实例,一个对浏览器本地事件的跨浏览器封装.它有和浏览器本地事件相同的属性和方法 ...
- kafka集群扩容后的topic分区迁移
https://www.cnblogs.com/honeybee/p/5691921.html kafka集群扩容后,新的broker上面不会数据进入这些节点,也就是说,这些节点是空闲的:它只有在创建 ...
- C# Winform同一子窗体只允许打开一次
在winform中一个窗口可以一直打开,是不合理的,解决方法: http://blog.csdn.net/kangkang621/article/details/49664295