-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.cs
52 lines (41 loc) · 1.17 KB
/
Solution.cs
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
namespace LeetCode.Problem977{
//977. Squares of a Sorted Array
//https://leetcode.com/problems/squares-of-a-sorted-array/description/
/*
Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.
*/
public class Solution {
public int[] SortedSquares(int[] nums) {
Stack<(int, int)> stack = new Stack<(int, int)>();
stack.Push((0, nums.Length - 1));
for (int k = 0; k < nums.Length; k++)
nums[k] *= nums[k];
while (stack.Count > 0)
{
(int left, int right) = stack.Pop();
int i = left, j = right;
int pivot = nums[(left + right) / 2];
while (i <= j)
{
while (nums[i] < pivot)
i++;
while (nums[j] > pivot)
j--;
if (i <= j)
{
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
i++;
j--;
}
}
if (left < j)
stack.Push((left, j));
if (i < right)
stack.Push((i, right));
}
return nums;
}
}
}