LeetCode#360。変換された配列を並べ替える
Leetcode 360 Sort Transformed Array
件名の説明:
与えられた ソート済み 整数の配列 nums および整数値 に 、 b そして c 。 f(の形式の2次関数を適用します バツ )= 斧 2 + bx + c 各要素に バツ 配列内。
返される配列は次の場所にある必要があります ソートされた順序 。
予想される時間の複雑さ: OR( n )。
例1:
class Solution { public: vector sortTransformedArray(vector& nums, int a, int b, int c) { // ax ^ 2 + bx + c = a (x + bx / 2a) ^ 2 + c-b ^ 2 / 4a, axis of symmetry is -b / 2a // So when a greater than or equal to 0, define two pointers moves to the middle, the end of the sequence into the result array // a is less than 0, moves to the middle define two pointers, the result into the beginning of the array in sequence int n=nums.size() vector result(n) int i=0, j=n-1 int count=0 while(i=0) { if(x>y) result[n-1-count]=x, i++ else result[n-1-count]=y, j-- } else { if(x>y) result[count]=y, j-- else result[count]=x, i++ } count++ } return result } }
例2:
Input: nums = [-4,-2,2,4], a = 1, b = 3, c = 5 Output: [3,9,15,33]
Input: nums = [-4,-2,2,4], a = -1, b = 3, c = 5 Output: [-23,-5,1,7]