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

Commitf9a584f

Browse files
authored
0977: Squares of a sorted array (#4)
1 parent53b911f commitf9a584f

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// let nums = [-4, -1, 0, 3, 10];
2+
letnums=[-7,-3,2,3,11];
3+
4+
functionsortedSquaresByMapping(nums:number[]):number[]{
5+
returnnums.map((i)=>i*i).sort((a,b)=>a-b);
6+
}
7+
8+
functionsortedSquaresFromTwoEnds(nums:number[]):number[]{
9+
constans=newArray(nums.length);
10+
letleft=0,
11+
right=nums.length-1;
12+
13+
for(leti=nums.length-1;i>=0;i--){
14+
if(Math.abs(nums[left])>Math.abs(nums[right])){
15+
ans[i]=nums[left]*nums[left];
16+
left++;
17+
}else{
18+
ans[i]=nums[right]*nums[right];
19+
right--;
20+
}
21+
}
22+
23+
returnans;
24+
}
25+
26+
functionsortedSquaresByUnshifting(nums:number[]):number[]{
27+
constans:number[]=[];
28+
letleft=0,
29+
right=nums.length-1;
30+
31+
while(left<=right){
32+
if(Math.abs(nums[left])>nums[right]){
33+
ans.unshift(nums[left]**2);
34+
left++;
35+
}else{
36+
ans.unshift(nums[right]**2);
37+
right--;
38+
}
39+
}
40+
41+
returnans;
42+
}
43+
44+
console.time("sortedSquaresByMapping");
45+
letresultByMapping=sortedSquaresByMapping(nums);
46+
console.timeEnd("sortedSquaresByMapping");
47+
48+
console.time("sortedSquaresFromTwoEnds");
49+
letresultFromTwoEnds=sortedSquaresFromTwoEnds(nums);
50+
console.timeEnd("sortedSquaresFromTwoEnds");
51+
52+
console.time("sortedSquaresByUnshifting");
53+
letresultByUnshifting=sortedSquaresByUnshifting(nums);
54+
console.timeEnd("sortedSquaresByUnshifting");
55+
56+
console.log(resultByUnshifting);
57+
console.log(resultByMapping);
58+
console.log(resultFromTwoEnds);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#977. Squares of a Sorted Array
2+
3+
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>.
4+
5+
##Example 1:
6+
7+
><spanstyle="color:white;">Input</span>: nums =[-4, -1, 0, 3, 10]<br>
8+
><spanstyle="color:white;">Output</span>:[0, 1, 9, 16, 100]<br>
9+
><spanstyle="color:white;">Explanation</span>: After squaring, the array becomes[16, 1, 0, 9, 100].<br>
10+
>After sorting, it becomes[0, 1, 9, 16, 100].
11+
12+
##Example 2:
13+
14+
><spanstyle="color:white;">Input</span>: nums =[-7, -3, 2, 3, 11]<br>
15+
><spanstyle="color:white;">Output</span>:[4, 9, 9, 49, 121]
16+
17+
##Constraints:
18+
19+
- 1 <= nums.length <= 10<sup>4</sup>
20+
- -10<sup>4</sup> <= nums[i] <= 10<sup>4</sup>
21+
-`nums` is sorted in <b>non-decreasing</b> order.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp