[HAI] week3 two pointers squares of a sorted array
https://leetcode.com/problems/squares-of-a-sorted-array/
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
export function sortedSquares(nums: number[]): number[] {
|
||||
const result: number[] = [];
|
||||
let L = 0;
|
||||
let R = nums.length - 1;
|
||||
|
||||
while (result.length !== nums.length) {
|
||||
let leftValue = Math.pow(nums[L], 2);
|
||||
let rightValue = Math.pow(nums[R], 2);
|
||||
|
||||
if (leftValue > rightValue) {
|
||||
result.push(leftValue);
|
||||
L++;
|
||||
} else {
|
||||
result.push(rightValue);
|
||||
R--
|
||||
}
|
||||
}
|
||||
|
||||
return result.reverse();
|
||||
};
|
||||
Reference in New Issue
Block a user