Java实现 LeetCode 492 构造矩形
492. 构造矩形
作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的。 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面。要求:
你设计的矩形页面必须等于给定的目标面积。
宽度 W 不应大于长度 L,换言之,要求 L >= W 。
长度 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 构造矩形的更多相关文章
- Leetcode 492. 构造矩形
1.题目描述 作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的. 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面.要求: 1. 你设 ...
- Java实现 LeetCode 391 完美矩形
391. 完美矩形 我们有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域. 每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 ...
- Java实现 LeetCode 85 最大矩形
85. 最大矩形 给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积. 示例: 输入: [ ["1","0","1 ...
- 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 ...
- 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. ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- spring源码解析--上
本文是作者原创,版权归作者所有.若要转载,请注明出处. 首先是配置类 package com.lusai.config; import org.springframework.context.anno ...
- FZU2105 线段树 (按位操作)
题目: Given N integers A={A[0],A[1],...,A[N-1]}. Here we have some operations: (元素和操作元素 < 16) Opera ...
- Echarts关于tree树数据渲染图例最新实例
最近做项目接到新的需求,根据本身系统结构数据做一个图形化展示,要求好看易用,有交互,就说了这么多,然后就要求两天给一版瞅瞅,MMP,真把前端当神了(你倒是把待遇提到神的地位啊...) 唉,吐槽归吐槽, ...
- 关于字符串函数size()的问题
首先如果你是一段语句 for(int i=0;i<a.size();i++)这个时候会报错 comparison between signed and unsigned integer expr ...
- xctf-misc 新手区 wp
目录 this_is_flag pdf SimpalRAR ext3 stegano this_is_flag Most flags are in the form flag{xxx}, for ex ...
- ios]企业开发者账号申请
1. 先打电话到“华夏邓白氏公司”(上海:400-820-3536 北京:400-810-3531 广州:800-830-9032),我打的是北京分部的电话,就说自己因为申请apple开发者账号,需要 ...
- CukeTest+Puppeteer的Web自动化测试(一)
CukeTest+Puppeteer的Web自动化测试 一.初识BDD.Cucumber(黄瓜).CukeTest 行为驱动开发(Behavior Driven Development,BDD).行为 ...
- 苏浪浪 201771010120《面向对象程序设计(java)》第六章学习总结
第五章 主要学习OOP另一个部分----继承,继承使程序员可以使用现有的类,并根据需要进行修改.这是Java程序设计中的一个基础设计. 1.类.超类和子类: (1) 已有类称为:超类(supercla ...
- PAT-1132 Cut Integer (整数分割)
Cutting an integer means to cut a K digits long integer Z into two integers of (K/2) digits long int ...
- PAT-1078 Hashing (散列表 二次探测法)
1078. Hashing The task of this problem is simple: insert a sequence of distinct positive integers in ...