---
# System prepended metadata

title: 0360. Sort Transformed Array
tags: [Leetcode, Two Pointers, Medium]

---

# 0360. Sort Transformed Array
###### tags: `Leetcode` `Medium` `Two Pointers`
Link: https://leetcode.com/problems/sort-transformed-array/
## 思路
two pointers分别指向首尾
需要分情况讨论
如果a>0, 最大值在两边，先从最后放最大值
如果a<0, 最小值在两边，先从最前面放最小值
如果a=0，归到任何一种情况都可以
## Code
```java=
class Solution {
    public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
        for(int i=0; i<nums.length; i++){
            nums[i] = a*nums[i]*nums[i]+b*nums[i]+c;
        }
        int[] res = new int[nums.length];
        int l=0, r=nums.length-1;
        int curr=a>=0?res.length-1:0;
        while(l<r){
            if(a>=0) res[curr--]=nums[r]>nums[l]?nums[r--]:nums[l++];
            else res[curr++]=nums[r]<nums[l]?nums[r--]:nums[l++];
        }
        if(l==r) res[curr]=nums[l];
        return res;
    }
}
```