-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy path0977-SquaresOfASortedArray.cs
More file actions
38 lines (33 loc) · 986 Bytes
/
0977-SquaresOfASortedArray.cs
File metadata and controls
38 lines (33 loc) · 986 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//-----------------------------------------------------------------------------
// Runtime: 284ms
// Memory Usage: 39.9 MB
// Link: https://leetcode.com/submissions/detail/328294820/
//-----------------------------------------------------------------------------
using System;
namespace LeetCode
{
public class _0977_SquaresOfASortedArray
{
public int[] SortedSquares(int[] A)
{
var left = 0;
var right = A.Length - 1;
var result = new int[A.Length];
var current = right;
while (left <= right)
{
if (Math.Abs(A[left]) > Math.Abs(A[right]))
{
result[current--] = A[left] * A[left];
left++;
}
else
{
result[current--] = A[right] * A[right];
right--;
}
}
return result;
}
}
}