Skip to content
Snippets Groups Projects

Squares of a Sorted Array

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    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;
        }
    }
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment