Leetcode Note: Go - Squares of a Sorted Array

Squares of a Sorted Array - LeetCode
https://leetcode.com/problems/squares-of-a-sorted-array/

  • Go 言語で取り組んだメモ

回答

Go O(n) solution with explanation - LeetCode Discuss
https://leetcode.com/problems/squares-of-a-sorted-array/discuss/1477544/Go-O(n)-solution-with-explanation

// function to get the absolute value
func Abs(x int) int {
    if x < 0 {
        return -x
    }
    
    return x
}

// main function to get the sorted squares array
func sortedSquares(nums []int) []int {
    
    n := len(nums) // get the length of the given array
    result := make([]int, n) // create a new slice of type int that can hold n values
    
    // left and right pointers
    left, right := 0, n - 1
    
    // iterate through our new slice (result) in reverse as we will be adding numbers from largest to smallest
    for i := n - 1; i >= 0; i-- {
        square := 0 // the variable that will store the largest absolute value of the two pointers
        
        // if the absolute value of the left pointer is less than the absolute value of the right pointer, 
        // set square to the larger number from the right pointer and move the right pointer one left.
        // Else set square to the larger number from the left pointer and move the left pointer one right.
        if Abs(nums[left]) < Abs(nums[right]) {
            square = nums[right]
            right--
        } else {
            square = nums[left]
            left ++
        }
        
        // square the value and add it to the appropriate index
        result[i] = square * square
    }
    
    // return the sorted array with the squared values
    return result
}