492. 构造矩形

作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的。 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面。要求:

  1. 你设计的矩形页面必须等于给定的目标面积。

  2. 宽度 W 不应大于长度 L,换言之,要求 L >= W 。

  3. 长度 L 和宽度 W 之间的差距应当尽可能小。

    你需要按顺序输出你设计的页面的长度 L 和宽度 W。

示例:

输入: 4

输出: [2, 2]

解释: 目标面积是 4, 所有可能的构造方案有 [1,4], [2,2], [4,1]。

但是根据要求2,[1,4] 不符合要求; 根据要求3,[2,2] 比 [4,1] 更能符合要求. 所以输出长度 L 为 2, 宽度 W 为 2。

说明:

给定的面积不大于 10,000,000 且为正整数。

你设计的页面的长度和宽度必须都是正整数。

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

Java实现 LeetCode 492 构造矩形的更多相关文章

  1. Leetcode 492. 构造矩形

    1.题目描述 作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的. 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面.要求: 1. 你设 ...

  2. Java实现 LeetCode 391 完美矩形

    391. 完美矩形 我们有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域. 每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 ...

  3. Java实现 LeetCode 85 最大矩形

    85. 最大矩形 给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积. 示例: 输入: [ ["1","0","1 ...

  4. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  5. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  6. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  7. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  8. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  9. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

随机推荐

  1. 【Hadoop离线基础总结】Hadoop的架构模型

    Hadoop的架构模型 1.x的版本架构模型介绍 架构图 HDFS分布式文件存储系统(典型的主从架构) NameNode:集群当中的主节点,主要用于维护集群当中的元数据信息,以及接受用户的请求,处理用 ...

  2. 【Linux基础总结】Shell 基础编程

    Shell 基础编程 重启虚拟机遇到磁盘损坏如何解决 Shell编程中变量的声明.引用及作用域 Shell程序 概述 以文件形式存放批量的Linux命令集合,该文件能够被Shell解释执行,这种文件就 ...

  3. CF#214 C. Dima and Salad 01背包变形

    C. Dima and Salad 题意 有n种水果,第i个水果有一个美味度ai和能量值bi,现在要选择部分水果做沙拉,假如此时选择了m个水果,要保证\(\frac{\sum_{i=1}^ma_i}{ ...

  4. [hdu4552]最长公共前缀

    题意:给一个串s,求s的每个前缀出现次数之和. 思路:对于一个后缀i,设i和原串的最长公共前缀为k,则当前总共可以产生k个答案.因此原题转化为求所有后缀与原串的最长公共前缀之和.模板题.以下为通过模板 ...

  5. python之unittest验证函数功能

    一.待验证脚本 首先,有如下三个脚本,分别对应三个函数 分别导入模块行,如下: from fuction1 import fu1 from fuction2 import fu2 from fucti ...

  6. JS 面向对象封装 无限轮播 插件。

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  7. 【Leetcode】164. Maximum Gap 【基数排序】

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  8. 使用QQ同步助手备份同步手机数据

    QQ同步助手官网:https://pim.qq.com/ QQ同步助手,由腾讯精心打造的云端备份工具.能实现手机之间传输文件,并备份文件.照片.视频.联系人.短信.通讯记录.应用程序到云端的换手机必备 ...

  9. 30分钟快速上手Docker,看这篇就对了!

    一.历史演化 1.演化史 2.物理机时代 2.1.图解 一个物理机上安装操作系统,然后直接运行我们的软件.也就是说你电脑上直接跑了一个软件,并没有开虚拟机什么的,资源极其浪费. 2.2.缺点 部署慢 ...

  10. HDU1160

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 题目大意:给出多个数据组(最多1000个),terminated by end of file, ...