short c++ solution using std::merge


  • 0
    L
    class Solution {
    public:
        vector<int> sortTransformedArray(vector<int>& nums, int a, int b, int c) {
            for (int &x : nums)
                x = a * x * x + b * x + c;
            vector<int>::iterator it;
            if (a < 0) {
                it = max_element(nums.begin(), nums.end()); // +++---
                reverse(it, nums.end());
            }
            else {
                it = min_element(nums.begin(), nums.end()); // ---+++
                reverse(nums.begin(), it);
            }
            vector<int> ret(nums.size());
            merge(nums.begin(), it, it, nums.end(), ret.begin());
            return ret;
        }
    };
    

Log in to reply
 

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