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] <= 10000Ais 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 ...
随机推荐
- openvpn 使用账户密码认证
OpenVPN使用user/passwd完成验证登录 OpenVPN使用user/passwd完成验证登录1,为什么要使用user/passwd?比常规openvpn管理方便,删除用户只需要在pwd. ...
- eclipse中没有tomcat小猫
安装了tomcat,按网上的说明也使用了tomcatPluginV331 配置文件,还是没有小猫,后来我发现,网上的tomcatPluginV331 针对eclipse 4.4版本,所以应该是插件的版 ...
- leetcode 443. String Compression
下面反向遍历,还是正向好. void left(vector<char>& v, bool p(int)) { ; ; ; while (del < max_index) { ...
- f5 irules
1.插入XFF when HTTP_REQUEST { if { [HTTP::header exists X-Forward-For] } { set old_xff [HTTP::header v ...
- lendinghome oa 准备
hardcode版本 估计只能过一个吧 import java.util.*; public class NextServer { Map<Integer, Integer> server ...
- gridview 显示数据库中的超链接
gridview默认情况下,数据库中存储的超链接,是不会显示的.它会直接把超链接字符显示出来. 例如:选定数据源后,gridview自动生成的列是这样的: <asp:BoundField Dat ...
- TableViewCell去除选中效果
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableV ...
- nginx: [emerg] mkdir() "/var/temp/nginx/client" failed (2: No such file or directory)
报错信息 [root@bogon sbin]# ./nginx nginx: [emerg] mkdir() : No such file or directory) 解决方法 [root@bogon ...
- javascript 的原型与原型链的理解
javascript中一切皆对象,但是由于没有Class类的概念,所以就无法很好的表达对象与对象之间的关系了. 比如对象A与对象B之间,它们两个是相对独立的个体,互不干扰,对象A修改自身的属性不会影响 ...
- js实现各种复制到剪贴板的方法
一.实现点击按钮,复制文本框中的的内容 <script type="text/javascript"> function copyUrl2() { var Url2=d ...