Skip to content

Commit b393feb

Browse files
committed
Create 0977-squares-of-a-sorted-array.js
1 parent e2aab2b commit b393feb

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var sortedSquares = function (nums) {
6+
let left = 0;
7+
let right = nums.length - 1;
8+
9+
const answer = [];
10+
11+
while (left <= right) {
12+
const leftSqr = Math.pow(nums[left], 2);
13+
const rightSqr = Math.pow(nums[right], 2);
14+
15+
if (leftSqr > rightSqr) {
16+
answer.push(leftSqr);
17+
left++;
18+
} else {
19+
answer.push(rightSqr);
20+
right--;
21+
}
22+
}
23+
return answer.reverse();
24+
};

0 commit comments

Comments
 (0)