Squares of a Sorted Array
The snippet can be accessed without any authentication.
Authored by
Kiryuu Sakuya
Program.cs 865 B
public class Solution {
public int[] SortedSquares(int[] nums) {
// 1. reverse
for (int i = 0; i < nums.Count(); i++)
{
if (nums[i] < 0)
nums[i] = Math.Abs(nums[i]);
}
// 2. double
for (int i = 0; i < nums.Count(); i++)
{
nums[i] = (int)Math.Pow(nums[i], 2);
}
// 3. order
for (int j = 0; j < nums.Count(); j++)
{
for (int i = nums.Count() - 1; i > 0; i--)
{
if (nums[i] < nums[i - 1])
{
int temp = nums[i];
nums[i] = nums[i - 1];
nums[i - 1] = temp;
}
}
}
return nums;
}
}
Please register or sign in to comment