Skip to content

Commit bb67ac3

Browse files
authored
Create 977-squares-of-a-sorted-array.js
1 parent 734f964 commit bb67ac3

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

977-squares-of-a-sorted-array.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} A
3+
* @return {number[]}
4+
*/
5+
const sortedSquares = function(A) {
6+
const result = [];
7+
let i = A.length - 1;
8+
let left = 0;
9+
let right = A.length -1;
10+
while (left <= right) {
11+
if (Math.abs(A[left]) < A[right]) {
12+
result.unshift(A[right] * A[right])
13+
right--;
14+
} else {
15+
result.unshift(A[left] * A[left])
16+
left++
17+
}
18+
}
19+
return result;
20+
};

0 commit comments

Comments
 (0)