Leetcode - Find Numbers with Even Number of Digits
Leetcode - Sort Transformed Array

Leetcode - Squares of a Sorted Array

violet posted @ Jul 14, 2020 02:00:51 AM in 算法 with tags Algorithm Golang array , 296 阅读

https://leetcode.com/problems/squares-of-a-sorted-array/

Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.

 

Example 1:

Input: [-4,-1,0,3,10]
Output: [0,1,9,16,100]

 

func sortedSquares(A []int) []int {
    result := make([]int, len(A))
    start := 0
    for start < len(A) && A[start] < 0 {
        start++
    }
    left := start - 1
    right := start
    index := 0
    for left >= 0 && right < len(A) {
        leftSquare := A[left] * A[left]
        rightSquare := A[right] * A[right]
        if leftSquare < rightSquare {
            result[index] = leftSquare
            left--
        } else {
            result[index] = rightSquare
            right++
        }
        index++
    }
    for left >= 0 {
        result[index] = A[left] * A[left]
        index++
        left--
    }
    for right < len(A) {
        result[index] = A[right] * A[right]
        index++
        right++
    }
    return result
}
PNB Net Banking 说:
Jan 28, 2023 11:40:09 PM

Punjab National Bank is a popular yet most preferred National Bank in India, and the Net Banking service for the bank allows its customers to utilize different services online without investing in the branch. PNB Net Banking As the modern days are invoking with the technology, the services from Banks through their Net Banking feature are in good spike, and there are multiple benefits for the customer who does get the access to the PNB Net Banking service.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter