LeetCode题解之Squares of a Sorted Array

1、题目描述

2、问题分析

使用过两个计数器。

3、代码

 1 class Solution {
 2 public:
 3     vector<int> sortedSquares(vector<int>& A) {
 4        int left = 0, right = A.size() - 1;
 5         vector<int> res;
 6         while (left <= right) {
 7             if (abs(A[left]) >= abs(A[right])) {
 8                 res.push_back(A[left] * A[left]);
 9                 left++;
10             } else {
11                 res.push_back(A[right] * A[right]);
12                 right--;
13             }
14         }
15         
16         reverse(res.begin(), res.end());
17         return res;
18     }
19 };
原文地址:https://www.cnblogs.com/wangxiaoyong/p/10507471.html