国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode/LintCode] Construct the Rectangle

sf_wangchong / 724人閱讀

Problem

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:

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.

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.

Solution
public class Solution {
    /**
     * @param area: web page"s area
     * @return: the length L and the width W of the web page you designed in sequence
     */
    public int[] constructRectangle(int area) {
        int squareRoot = (int) Math.sqrt(area);
        int w = squareRoot;
        while (area%w != 0) {
           w--;
        }
        return new int[] {area/w, w};
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/69550.html

相關(guān)文章

  • [LeetCode/LintCode] Top K Frequent Words

    LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...

    0x584a 評論0 收藏0
  • [LeetCode/LintCode] First Bad Version

    摘要:分析最后一次循環(huán)的時刻當(dāng)與相差小于時,總是那么如果是,下一次直接跳出循環(huán),返回當(dāng)與相差大于時,是,變成,如果是,循環(huán)結(jié)束的條件將是循環(huán)結(jié)束,返回 Problem The code base version is an integer start from 1 to n. One day, someone committed a bad version in the code case,...

    lowett 評論0 收藏0
  • [LeetCode/LintCode] Largest Palindrome Product

    Problem Find the largest palindrome made from the product of two n-digit numbers. Since the result could be very large, you should return the largest palindrome mod 1337. Example Input: 2Output: 987Ex...

    Barry_Ng 評論0 收藏0
  • [LeetCode/LintCode] Remove Nth Node From End of L

    Problem Given a linked list, remove the nth node from the end of list and return its head. Example Given linked list: 1->2->3->4->5->null, and n = 2. After removing the second node from the end, the l...

    Jaden 評論0 收藏0
  • [LeetCode/LintCode] Happy Number

    Problem Write an algorithm to determine if a number is happy.A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squar...

    崔曉明 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<