Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Squares of a Sorted Array

public class Solution {
    // [-4,-1,0,3,10]
    // [3]
    public int[] SortedSquares(int[] nums) {
        if (nums == null)
        {
            return null;
        }
        
        int[] newArray = new int[nums.Length];
        int left = 0; // 2
        int right = nums.Length - 1; // 1
        for (int i = nums.Length - 1; i >= 0; i--) // 0
        {
            int leftSq = nums[left] * nums[left]; // 0
            int rightSq = nums[right] * nums[right]; // 0
            if (leftSq > rightSq)
            {
                newArray[i] = leftSq;
                left++;
            }
            else
            {
                newArray[i] = rightSq;
                right--;
            }
        }
        
        // [0,1,9,16,100]
        return newArray;
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: get sha1 of file c# 
Csharp :: c# sum of array elements# 
Csharp :: c# add object to array 
Csharp :: write last element of dictionary c# 
Csharp :: build cs file 
Csharp :: github action get commit tag 
Csharp :: valid URL check in c# 
Csharp :: destroy the game object if the animator has finished its animation 
Csharp :: get property value from object c# 
Csharp :: unity get game version 
Csharp :: lcm of numbers 
Csharp :: c# get last day of month 
Csharp :: how c# connection 
Csharp :: c# multiple strings are empty 
Csharp :: c# bubble sort 
Csharp :: copy class c# 
Csharp :: How to make game object transparent in unity 
Csharp :: socket io connect to namespace 
Csharp :: unity camera follow with lerp 
Csharp :: if checkbox checked in c# 
Csharp :: unity set sprite image from script 
Csharp :: c# string ends with 
Csharp :: c# list.foreach 
Csharp :: excel isrlgood 
Csharp :: if viewbag is null 
Csharp :: combobox selected name c# 
Csharp :: c# xml get child node by name 
Csharp :: should i learn c # 
Csharp :: encrypt with public key and decrypt with private key c# 
Csharp :: c# show list in richtextbox 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =