Java Concise


  • 0
    public class Solution {
        private int a1, b1, c1;
        public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
            a1 = a;
            b1 = b;
            c1 = c;
            if (nums == null || nums.length == 0) return new int[0];
            int n = nums.length;
            int l = 0, r = n - 1;
            int[] res = new int[n];
            int i = 0;
            while (l <= r) {
                if (a >= 0) {
                    res[n - i - 1] = solve(nums[l]) >= solve(nums[r]) ? solve(nums[l++]) : solve(nums[r--]);
                }
                else {
                    res[i] = solve(nums[l]) >= solve(nums[r]) ? solve(nums[r--]) : solve(nums[l++]);
                }
                i++;
            }
            return res;
        }
        private int solve(int x) {
            return a1 * x * x + b1 * x + c1;
        }
    }
    

Log in to reply
 

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