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 ...
随机推荐
- Failed to acquire connection "SAP_PRD_NEW.SAPSR3". Connection may not be configured correctly or you may not have the right permissions
SQLSERVER JOB无法执行 错误提示: Message Executed as user: WORKGROUP\NSDZHSCMFP01$. Microsoft (R) SQL Server ...
- spring jpa nativequery in与修改
参考 https://blog.csdn.net/a3025056/article/details/79022816 @Modifying@Transactional /* 如果在事务中使用需加上此注 ...
- 【Linux 线程】常用线程函数复习《一》
1.pthread_create以及pthread_self函数 /****************************************************************** ...
- mysql定时删除6个月前的表
查看定时是否开启: 查看event是否开启 : SHOW VARIABLES LIKE '%event_sche%'; 将事件计划开启 : ; 将事件计划关闭 : ; 代码: BEGIN -- 保存表 ...
- how2j网站前端项目——天猫前端(第一次)学习笔记4
开始产品页面的学习.项目里面有900多种商品,但是每种商品的布局是一致的:1.产品图片 2.基本信息 3.产品详情 4.累计评价 5.交互.从第一个产品图片开始吧! 一.产品图片 产品图片用到了分类页 ...
- Service里边启动Activity注意事项
Intent intentv = new Intent(Intent.ACTION_VIEW); intentv.setData(uri); intentv.putExtra("keepTi ...
- golang 创建一个简单的资源池,重用资源,减少GC负担
package main; import ( "sync" "errors" "fmt" ) //代码参考<Go语言实战>中第7 ...
- html5的地理位置定位
html5提供的地理位置定位使开发人员不用借助其他软件就能轻松实现位置查找,地图应用,导航等功能. 地理位置定位基本原理GPS, WIFI, IP, 手机信号基站 核心对象Geolocation是wi ...
- localstorage和vue结合使用
父组件 <template> <div class="hello"> <p>Original message:"{{message}} ...
- python 三元表达式、列表推导式、生成器表达式、递归、匿名函数、内置函数
http://www.cnblogs.com/linhaifeng/articles/7580830.html 三元表达式.列表推导式.生成器表达式.递归.匿名函数.内置函数