Squares of a Sorted Array LT977
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 <= A.length <= 10000
-10000 <= A[i] <= 10000
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的更多相关文章
- LeetCode977.Squares of a Sorted Array
题目 977. Squares of a Sorted Array Given an array of integers A sorted in non-decreasing order, retur ...
- 【Leetcode_easy】977. Squares of a Sorted Array
problem 977. Squares of a Sorted Array solution: class Solution { public: vector<int> sortedSq ...
- [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 ...
- #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- ...
- 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 ...
- 977. Squares of a Sorted Array
题目描述: Given an array of integers A sorted in non-decreasing order, return an array of the squares of ...
- Squares of a Sorted Array
Given an array of integers A sorted in non-decreasing order, return an array of the squares of each ...
- 【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 ...
- 【LeetCode】977. Squares of a Sorted Array 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
随机推荐
- Cacti日志时区问题
cacti默认是以美国的时间为准的,监测的适合要纠正到UTC+8的时区. 打开vi /home/cacti/include/config.php 文件,在里面加入一行 date_default_tim ...
- DB2 57016报错的解决办法(表状态不正常,导致表无法操作)
新建了一张表,删除了一列,然后执行insert的时候,报错 57016,解释为:因为表不活动. 1.执行db2 "load query table <tabname>" ...
- python 函数的定义及传参
函数是一个通用的程序结构部件,是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 定义一个简单的函数: >>> def test(a): #创建一个函数,函数名是test ...
- Java 16进制转10进制
牛课网上的题目 char '1'和 int 1给我好好上了一课 package suanfa; import java.util.*; public class Main{ public static ...
- 使用Fiddler远程抓包(转载)
转载自 http://www.cnblogs.com/111testing/p/6436372.html Fiddler简介以及web抓包 一.Fiddler简介 简单来说,Fiddler是一个htt ...
- 理解 IAAS、PAAS、SAAS
引用:http://cloud.51cto.com/art/201802/565858.htm 在与相关人士聊云计算的时候,有时会从他们的最终蹦出诸如IaaS.PaaS和SaaS等相关名词,听的人一头 ...
- eCharts 折线图,动态绑定数据不更新图表的问题,
官方文档 : http://echarts.baidu.com/tutorial.html npm install echarts --save let lineChart = echarts.ini ...
- 变态跳台阶(python)
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. # -*- coding:utf-8 -*- class Solution: ...
- Shell教程 之第一个shell脚本
1.第一个shell脚本 打开文本编辑器(可以使用 vi/vim 命令来创建文件),新建一个文件 test.sh,扩展名为 sh(sh代表shell),扩展名并不影响脚本执行 输入一些代码 #!/bi ...
- 如何彻底卸载mysql(xp)
如何彻底卸载mysql 完整的卸载MySQL 5.x 的方法: 1.控制面板里的增加删除程序内进行删除 2.删除MySQL的安装文件夹C:\Program Files\MySQL,如果备份好,可以直接 ...