成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

360. Sort Transformed Array

ChristmasBoy / 2680人閱讀

摘要:題目鏈接這是個(gè)數(shù)學(xué)問(wèn)題,拋物線,我們知道這時(shí)候是個(gè)凹函數(shù),兩遍的值大于中間,所以從兩遍開(kāi)始哪邊的大就把結(jié)果放到的右邊這時(shí)候是個(gè)凸函數(shù),兩遍的值小于中間,所以兩遍開(kāi)始掃哪邊的值小就把它放到的左邊這時(shí)候是單調(diào)增的函數(shù),用上面任意一種方法都可以。

360. Sort Transformed Array

題目鏈接:https://leetcode.com/problems...

這是個(gè)數(shù)學(xué)問(wèn)題,拋物線,我們知道

a > 0: 這時(shí)候是個(gè)凹函數(shù),兩遍的值大于中間,所以從兩遍開(kāi)始哪邊的大就把結(jié)果放到result的右邊

a < 0: 這時(shí)候是個(gè)凸函數(shù),兩遍的值小于中間,所以兩遍開(kāi)始掃哪邊的值小就把它放到result的左邊

a == 0: 這時(shí)候是單調(diào)增的函數(shù),用上面任意一種方法都可以。

public class Solution {
    public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
        int n = nums.length;
        // 2 points
        int i = 0, j = n - 1;
        int k = a > 0 ? n - 1 : 0;
        
        int[] res = new int[n];
        while(i <= j) {
            int left = getF(nums[i], a, b, c);
            int right = getF(nums[j], a, b, c);
            if(a > 0) {
                if(left > right) {
                    res[k--] = left;  i++;
                }
                else {
                    res[k--] = right;  j--;
                }
            }
            else {
                if(left < right) {
                    res[k++] = left;  i++;
                }
                else {
                    res[k++] = right;  j--;
                }
            }
        }
        
        return res;
    }
    
    private int getF(int x, int a, int b, int c) {
        return a * x * x + b * x + c;
    }
}

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/69850.html

相關(guān)文章

  • 360. Sort Transformed Array

    摘要:題目解答還是數(shù)學(xué)解法,根據(jù)這個(gè)方程圖形的特征來(lái)判斷最大最小值的取向 題目:Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f(x) = ax2 + bx + c to each element x in the array. The ret...

    wua_wua2012 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<