Skip to content

Commit dcc454d

Browse files
committed
array - 2 pointers O(n) O(n)
1 parent 7a7ffb6 commit dcc454d

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

_leetcode_from-blind75-26w40h/w1-41-!!1-SquaresOfASortedArray.js renamed to _leetcode_from-blind75-26w40h/w1-41-!!2-SquaresOfASortedArray.js

File renamed without changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
6+
// p: array;
7+
// r: array;
8+
//
9+
// e: [-4,-1,0,3,10 ]
10+
// i
11+
// [16, 1,0,9,100]
12+
// j
13+
var sortedSquares = function (nums) {
14+
const result = [];
15+
let i = 0,
16+
j = nums.length - 1;
17+
18+
while (i <= j) {
19+
const n = nums[i] * nums[i];
20+
const m = nums[j] * nums[j];
21+
if (n < m) {
22+
result.push(m);
23+
j--;
24+
} else {
25+
result.push(n);
26+
i++;
27+
}
28+
}
29+
return result.reverse();
30+
};

0 commit comments

Comments
 (0)