摘要:題目鏈接這是個(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
摘要:題目解答還是數(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...
閱讀 2232·2021-09-04 16:40
閱讀 1540·2021-08-13 15:07
閱讀 3670·2019-08-30 15:53
閱讀 3269·2019-08-30 13:11
閱讀 1148·2019-08-29 17:22
閱讀 1865·2019-08-29 12:47
閱讀 1540·2019-08-29 11:27
閱讀 2337·2019-08-26 18:42