Given a sorted list of integers, square the elements and give the output in sorted order.
For example, given [-9, -2, 0, 2, 3], return [0, 4, 4, 9, 81].
Example:
Input: arr = [-9,-2,0,2,3]
Output: [0, 4, 4, 9, 81]
Approach
C++
#include <bits/stdc++.h>using namespace std;vector<int> sortedSquares(vector<int> &nums){vector<int> res;int p = 0;bool flag = false;for (int i = 0; i < nums.size(); i++){if (nums[i] >= 0){flag = true;p = i;break;}}int i = p;int j = p - 1;if (flag == false)j = nums.size() - 1;while (j >= 0 && i < nums.size()){if (nums[i] * nums[i] < nums[j] * nums[j]){res.push_back(nums[i] * nums[i]);i++;}else{res.push_back(nums[j] * nums[j]);j--;}}while (j >= 0){res.push_back(nums[j] * nums[j]);j--;}while (i < nums.size() && flag){res.push_back(nums[i] * nums[i]);i++;}return res;}int main(){vector<int> nums = {-9, -2, 0, 2, 3};vector<int> res = sortedSquares(nums);cout << "[";for (int i = 0; i < res.size(); i++){cout << res[i];if (i != res.size() - 1)cout << ", ";}cout << "]";return 0;}
No comments:
Post a Comment