首页 > 其他分享 >leetcode-492-easy

leetcode-492-easy

时间:2023-01-03 21:44:51浏览次数:44  
标签:web area int page width length easy leetcode 492

Construct the Rectangle

A web developer needs 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:

The area of the rectangular web page you designed must equal to the given target area.
The width W should not be larger than the length L, which means L >= W.
The difference between length L and width W should be as small as possible.
Return an array [L, W] where L and W are the length and width of the web page you designed in sequence.

Example 1:

Input: area = 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.
Example 2:

Input: area = 37
Output: [37,1]
Example 3:

Input: area = 122122
Output: [427,286]
Constraints:

1 <= area <= 107

思路一:遍历 [1, sqrt(n)], 检测是否整除,如果是则符合答案

    public int[] constructRectangle(int area) {
        int[] result = new int[]{area, 1};

        for (int i = 2; i <= Math.sqrt(area); i++) {
            if (area % i == 0) {
                result[0] = area / i;
                result[1] = i;
            }
        }

        return result;
    }

标签:web,area,int,page,width,length,easy,leetcode,492
From: https://www.cnblogs.com/iyiluo/p/17023451.html

相关文章

  • leetcode-144-easy
    BinaryTreePreorderTraversalGiventherootofabinarytree,returnthepreordertraversalofitsnodes'values.Example1:Input:root=[1,null,2,3]Out......
  • [Leetcode Weekly Contest]326
    链接:LeetCode[Leetcode]2520.统计能整除数字的位数给你一个整数num,返回num中能整除num的数位的数目。如果满足nums%val==0,则认为整数val可以整除nums......
  • 【队列】LeetCode 232. 用栈实现队列
    题目链接232.用栈实现队列思路设置一个主栈mainStack和一个辅助栈assistantStack,在进行入队的时候,将mainStack中的元素全部放入assistantStack中,再将x入队,然......
  • [LeetCode] 1325. Delete Leaves With a Given Value 删除给定值的叶子结点
    Givenabinarytree root andaninteger target,deleteallthe leafnodes withvalue target.Notethatonceyoudeletealeafnodewithvalue target, ......
  • 【队列】LeetCode 225. 用队列实现栈
    题目链接225.用队列实现栈思路设置一个主队列mainQueue和一个辅助队列assistantQueue,在进行压栈的时候,将mainQueue中的元素全部放入assistantQueue中,再将x压......
  • Leetcode[LeetCode]4 两个有序数组的中位数
    上图为剑指Offer之字符串的排列,基于回溯法的思想。简单算法01数组中第二大的数02合并排序链表03链表反转04判断链表环05两个链表的首个交点06数组中出现大与一般的数07手写......
  • 【链表】LeetCode 328. 奇偶链表
    题目链接328.奇偶链表思路根据题意,我们只需要将扫描到的索引为奇数的结点不断插入到正确位置。比如1->2->3->4->5==》1->3->2->4->5==》1->3->5->2->4在扫描过......
  • 阿里easyexcel解析百万级大数据量的Excel表格,看这一篇文章就够了
    ​1、应用场景1.1、实际工作中可能会遇到百万条数据量的Excel表格上传到后台进行解析。那么传统的POI,它只适用于数据量较小的Excel表格解析,当数据量到一定级别时,后台程序就......
  • [LeetCode]014-最长公共前缀
    >>>传送门题目编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串""。示例示例1输入:strs=["flower","flow","flight"]输出:"fl"......
  • 【链表】LeetCode 92. 反转链表 II
    题目链接92.反转链表II思路和【链表】LeetCode206.反转链表的思路一样,只不过需要调整一下realHead头结点的位置,同时原题中的null在本题中为rightNode的下一个结点。......