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]

Example 2:

Input: [-7,-3,2,3,11]
Output: [4,9,9,49,121]

Note:

  1. 1 <= A.length <= 10000
  2. -10000 <= A[i] <= 10000
  3. A is sorted in non-decreasing order.

Since the arry is non-decreasingly sorted, we can use two pointers, either both pointers start from the middle, the negative one goes to the left and the non-negative goes to the right, as the magnitude of numbers increase from the middle,

result[dest++] = A[positive++] if Math.abs(A[negative]) <= Math.abs(A[positve])

result[dest++] = A[negative--] otherwise

Be careful: postive < A.length && negative >= 0, also need to deal with cases when there are more negative or positve left, copy the leftover to the result array.

Time complexity: O(n) two passes

Space complexity: O(n) to store result

class Solution {
int findNonNegativeIndex(int[] A) {
int index = 0;
while( index < A.length && A[index] < 0) {
++index;
}
return index;
}
public int[] sortedSquares(int[] A) {
int sz = A.length;
int[] result = new int[sz]; int nonNegative = findNonNegativeIndex(A);
int negative = nonNegative - 1;
int destination = 0; while(nonNegative < sz && negative >= 0) {
if(Math.abs(A[negative]) >= Math.abs(A[nonNegative])) {
result[destination++] = A[nonNegative] * A[nonNegative];
++nonNegative;
}
else {
result[destination++] = A[negative] * A[negative];
--negative;
}
} while(nonNegative < sz) {
result[destination++] = A[nonNegative] * A[nonNegative];
++nonNegative;
} while(negative >= 0) {
result[destination++] = A[negative] * A[negative];
--negative;
} return result;
}
}

Another way, the two pointers start from two ends and walk towards to each other. Save the effort to deal with the extra cases caused by boundary and also the effor to find the divider between positive and negative numbers.

Time complexity: O(n) one pass

Space complexity: O(n) to store result

class Solution {
public int[] sortedSquares(int[] A) {
int sz = A.length;
int[] result = new int[sz]; for(int left = 0, right = sz-1, destination = sz-1; left <= right; ) {
if(Math.abs(A[left]) >= Math.abs(A[right])) {
result[destination] = A[left] * A[left];
++left;
--destination;
}
else {
result[destination] = A[right] * A[right];
--right;
--destination;
}
} return result;
}
}

use the destination index as check condition, slightly more concise

class Solution {
public int[] sortedSquares(int[] A) {
int sz = A.length;
int[] result = new int[sz]; for(int left = 0, right = sz-1, destination = sz-1; destination >= 0; --destination) {
if(Math.abs(A[left]) >= Math.abs(A[right])) {
result[destination] = A[left] * A[left];
++left;
}
else {
result[destination] = A[right] * A[right];
--right;
}
} return result;
}
}

Squares of a Sorted Array LT977的更多相关文章

  1. LeetCode977.Squares of a Sorted Array

    题目 977. Squares of a Sorted Array Given an array of integers A sorted in non-decreasing order, retur ...

  2. 【Leetcode_easy】977. Squares of a Sorted Array

    problem 977. Squares of a Sorted Array solution: class Solution { public: vector<int> sortedSq ...

  3. [Swift]LeetCode977. 有序数组的平方 | Squares of a Sorted Array

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

  4. #Leetcode# 977. Squares of a Sorted Array

    https://leetcode.com/problems/squares-of-a-sorted-array/ Given an array of integers A sorted in non- ...

  5. LeetCode 977 Squares of a Sorted Array 解题报告

    题目要求 Given an array of integers A sorted in non-decreasing order, return an array of the squares of ...

  6. 977. Squares of a Sorted Array

    题目描述: Given an array of integers A sorted in non-decreasing order, return an array of the squares of ...

  7. Squares of a Sorted Array

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

  8. 【leetcode】977. Squares of a Sorted Array

    题目如下: Given an array of integers A sorted in non-decreasing order, return an array of the squares of ...

  9. 【LeetCode】977. Squares of a Sorted Array 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...

随机推荐

  1. 关于订单BOM替换组件不成功的问题

    替换成功的案例: SELECT * FROM IN_BOM_DETAILS WHERE BOM_ID='00161815_2023_01P19'; --成品编码:000000101011006433P ...

  2. MySQL实现中文拼音排序

    MySQL下新建一个表,默认采用utf8字符集,中文不能直接按照拼音进行排序. 例如以下语句: SELECT * FROM `tb_fixedassets` order by C_FANAME 得到的 ...

  3. MVC001之mvcpager简单分页

    描述:用mvcpager实现简单分页功能 参考网址: http://www.cnblogs.com/iamlilinfeng/archive/2013/03/11/2951460.html http: ...

  4. logic:iterate(转)

    logic:iterate struts标签<logic:iterate>的用法 StrutsBeanJSPWeb脚本  <logic:iterate>主要用来处理在页面上输出 ...

  5. mysql 5.7.21 主从集群恢复GTID方式(不锁库)

    从库损坏后,进行恢复 1.查看主加标记点 show master status\G 记录下POST的值 2.备注主库数据 mysqldump -u root -p -S /data/mysql/mys ...

  6. windows下python文件与文件夹操作

    一.导入模块 imoprt os 二.获取python当前执行的目录 s=os.getcwd() 三.创建文件 import datetime import os dtime=datetime.dat ...

  7. centos 配置Openssl并创建证书

    具体详情参考:http://wiki.centos.org/HowTos/Https 一.安装软件 yum install mod_ssl openssl 二.创建证书: # Generate pri ...

  8. 上海大都会赛 I Matrix Game(最大流)

    At the start of the matrix game, we have an N x M matrix. Each grid has some balls.The grid in (i,j) ...

  9. iOS耳机监听

    1 .插入耳机的时候并没有切换到耳机播放 仍然是扬声器播放 2 .当一开始手机上已经插入耳机时 ,这时候开启音频播放时 仍然是扬声器播放 因此今天主要谈的就是从这两个问题: 先来解决第一个问题:其实解 ...

  10. TestSuite测试报告生成

    简介:HTMLTestRuner介绍 1, 无法使用pip安装,手工下载 2, python3和python2语法不一致导致了HTMLTestRunner在py3中不兼容 解决办法:导入下面的HTML ...