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 ...
随机推荐
- JUC(java.util.concurrent)
在 Java 5.0 提供了 java.util.concurrent (简称JUC )包,在此包中增加了在并发编程中很常用的实用工具类,用于定义类似于线程的自定义子系统,包括线程池.异步 IO 和轻 ...
- javascript学习笔记(二):定义函数、调用函数、参数、返回值、局部和全局变量
定义函数.调用函数.参数.返回值 关键字function定义函数,格式如下: function 函数名(){ 函数体 } 调用函数.参数.返回值的规则和c语言规则类似. <!DOCTYPE ht ...
- Android创建和删除桌面快捷方式
有同学方反馈创建快捷方式后,点击快捷方式后不能启动程序或者提示"未安装程序",貌似是新的rom在快捷方式这块做过修改(由于此文是11年5月所出,估计应该是2.0或2.1的rom), ...
- 打印信息,通过.jasper工具将集合输出到PDF文件 然后利用打印机打印文件
我们上一次成功的利用iReport工具制作了一张报表,并且预览了报表最后的效果,也生成了格式为“jrpxml”.“jrxml”与“jasper”的文件.这次,我们使用jasper提供的java的api ...
- unity缓动插件DOTween Pro v0.9.680
DoTween Pro是一款unity插件,是unity中最好用的tween插件,比起Dotween的免费版要多很多功能,实现脚本和视觉脚本的新功能,支持包括移动,淡出,颜色,旋转,缩放,打孔,摇动, ...
- Codeforces Round #541 (Div. 2)
Codeforces Round #541 (Div. 2) http://codeforces.com/contest/1131 A #include<bits/stdc++.h> us ...
- @ResponseBody使用须知
-------------------siwuxie095 @ResponseBody 使用须知 使用 @ResponseBody 注解映射响应体 @ResponseBody 注解可被应用于方法上,标 ...
- TZOJ 4871 文化之旅(floyd预处理+dfs剪枝)
描述 有一位使者要游历各国,他每到一个国家,都能学到一种文化,但他不愿意学习任何一种文化超过一次,即如果他学习了某种文化,则他就不能到达其他有这种文化的国家.不同的国家可能有相同的文化.不同文化的国家 ...
- 项目总结13:Jav文件压缩-InputStream转化为base64-Base64解码并生成图片
Jav文件压缩-InputStream转化为base64-Base64解码并生成图片 直接上源码,解释见文章尾部 package com.hs.common.util.imgecode; import ...
- bbs项目引入富文本编辑器和处理xss攻击和文章预览
一.富文本编辑上传文章和图片 富文本编辑器我们使用kindeditor,我们首先去官网下载,然后解压,放到我们的static的目录中 然后我们在html中这样使用富文本编辑器 <!DOCTYPE ...