题目:
解答:
lef和rig分别指向左右的数,比较并从最大位开始装。
1 class Solution { 2 public: 3 vector<int> sortedSquares(vector<int>& A) 4 { 5 int len = A.size(); 6 vector<int> ans(len); 7 int a = 0, b = len - 1, i = b; 8 while (i >= 0) 9 { 10 int lef = A[a] * A[a]; 11 int rig = A[b] * A[b]; 12 if (lef > rig) 13 { 14 ans[i] = lef; 15 a++; 16 } 17 else 18 { 19 ans[i] = rig; 20 b--; 21 } 22 i--; 23 } 24 return ans; 25 } 26 };