Skip to content

Commit 24984c3

Browse files
authored
Create 977. Squares of a Sorted Array (#422)
2 parents c4bacdb + 759efc9 commit 24984c3

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

977. Squares of a Sorted Array

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
vector<int> sortedSquares(vector<int>& nums) {
4+
int left = 0, right = nums.size() - 1;
5+
vector<int> output(nums.size());
6+
7+
for (int i = nums.size() - 1; i >= 0; --i) {
8+
int value;
9+
if (nums[left] * nums[left] >= nums[right] * nums[right]) {
10+
value = nums[left] * nums[left++];
11+
} else {
12+
value = nums[right] * nums[right--];
13+
}
14+
output[i] = value;
15+
}
16+
return output;
17+
}
18+
};

0 commit comments

Comments
 (0)