Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

0977: Squares of a sorted array#4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
golevy merged 1 commit intodevelopfromlevy/squares-of-a-sorted-array
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions0977-Squares_of_a_Sorted_Array/main.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
// let nums = [-4, -1, 0, 3, 10];
let nums = [-7, -3, 2, 3, 11];

function sortedSquaresByMapping(nums: number[]): number[] {
return nums.map((i) => i * i).sort((a, b) => a - b);
}

function sortedSquaresFromTwoEnds(nums: number[]): number[] {
const ans = new Array(nums.length);
let left = 0,
right = nums.length - 1;

for (let i = nums.length - 1; i >= 0; i--) {
if (Math.abs(nums[left]) > Math.abs(nums[right])) {
ans[i] = nums[left] * nums[left];
left++;
} else {
ans[i] = nums[right] * nums[right];
right--;
}
}

return ans;
}

function sortedSquaresByUnshifting(nums: number[]): number[] {
const ans: number[] = [];
let left = 0,
right = nums.length - 1;

while (left <= right) {
if (Math.abs(nums[left]) > nums[right]) {
ans.unshift(nums[left] ** 2);
left++;
} else {
ans.unshift(nums[right] ** 2);
right--;
}
}

return ans;
}

console.time("sortedSquaresByMapping");
let resultByMapping = sortedSquaresByMapping(nums);
console.timeEnd("sortedSquaresByMapping");

console.time("sortedSquaresFromTwoEnds");
let resultFromTwoEnds = sortedSquaresFromTwoEnds(nums);
console.timeEnd("sortedSquaresFromTwoEnds");

console.time("sortedSquaresByUnshifting");
let resultByUnshifting = sortedSquaresByUnshifting(nums);
console.timeEnd("sortedSquaresByUnshifting");

console.log(resultByUnshifting);
console.log(resultByMapping);
console.log(resultFromTwoEnds);
21 changes: 21 additions & 0 deletions0977-Squares_of_a_Sorted_Array/readme.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
# 977. Squares of a Sorted Array

Given an integer array `nums` sorted in <b>non-decreasing</b> order, return <i>an array of <b>the squares of each number</b> sorted in non-decreasing order</i>.

## Example 1:

> <span style="color: white;">Input</span>: nums = [-4, -1, 0, 3, 10]<br>
> <span style="color: white;">Output</span>: [0, 1, 9, 16, 100]<br>
> <span style="color: white;">Explanation</span>: After squaring, the array becomes [16, 1, 0, 9, 100].<br>
> After sorting, it becomes [0, 1, 9, 16, 100].

## Example 2:

> <span style="color: white;">Input</span>: nums = [-7, -3, 2, 3, 11]<br>
> <span style="color: white;">Output</span>: [4, 9, 9, 49, 121]

## Constraints:

- 1 <= nums.length <= 10<sup>4</sup>
- -10<sup>4</sup> <= nums[i] <= 10<sup>4</sup>
- `nums` is sorted in <b>non-decreasing</b> order.

[8]ページ先頭

©2009-2025 Movatter.jp