Clean c++ solution


  • 0

    If a > 0, maximum value is guaranteed to be one end.
    Otherwise, minimum value is guaranteed to be one end.

    vector<int> sortTransformedArray(vector<int>& nums, int a, int b, int c) {
            int size = nums.size();
            for (int k = 0; k < size; k++) {
                nums[k] = (a * nums[k] + b) * nums[k] + c;
            }
            int i = 0, j = size - 1;
            vector<int> result;
            while (i <= j) {
                if ((a > 0 && nums[i] >= nums[j]) || (a <= 0 && nums[i] <= nums[j])) {
                    result.push_back(nums[i++]);
                } else {
                    result.push_back(nums[j--]);
                }
            }
            if (a > 0)
                reverse(result.begin(), result.end());
            return result;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.