当前位置:网站首页>数组——977. 有序数组的平方
数组——977. 有序数组的平方
2022-07-23 19:17:00 【向着百万年薪努力的小赵】
1 题目描述
给你一个按 非递减顺序 排序的整数数组 nums,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序。
2 题目示例
示例 1:
输入:nums = [-4,-1,0,3,10]
输出:[0,1,9,16,100]
解释:平方后,数组变为 [16,1,0,9,100]
排序后,数组变为 [0,1,9,16,100]
示例 2:
输入:nums = [-7,-3,2,3,11]
输出:[4,9,9,49,121]
3 题目提示
- 1 <= nums.length <= 104
- -104 <= nums[i] <= 104
- nums 已按 非递减顺序 排序
4 思路
数组其实是有序的, 只不过负数平方之后可能成为最大数了。
那么数组平方的最大值就在数组的两端,不是最左边就是最右边,不可能是中间。
此时可以考虑双指针法了,i指向起始位置,j指向终止位置。
定义一个新数组result,和A数组一样的大小,让k指向result数组终止位置。
5 我的答案
class Solution {
public int[] sortedSquares(int[] nums) {
int right = nums.length - 1;
int left = 0;
int[] result = new int[nums.length];
int index = result.length - 1;
while (left <= right) {
if (nums[left] * nums[left] > nums[right] * nums[right]) {
// 正数的相对位置是不变的, 需要调整的是负数平方后的相对位置
result[index--] = nums[left] * nums[left];
++left;
} else {
result[index--] = nums[right] * nums[right];
--right;
}
}
return result;
}
}
class Solution {
public int[] sortedSquares(int[] nums) {
int l = 0;
int r = nums.length - 1;
int[] res = new int[nums.length];
int j = nums.length - 1;
while(l <= r){
if(nums[l] * nums[l] > nums[r] * nums[r]){
res[j--] = nums[l] * nums[l++];
}else{
res[j--] = nums[r] * nums[r--];
}
}
return res;
}
}
边栏推荐
- Leetcode 219. duplicate Element II exists (yes, resolved)
- 20.ref与props
- 梅科尔工作室-华为14天鸿蒙设备开发实战笔记六
- 关于网段CIDR的笔记
- 2022DASCTF MAY
- 13. Roman to Integer罗马数字转整数
- scanf()和getchar()的用法讨论
- MySQL data recovery - using the data directory
- [PM2] PM2 common commands
- What if there is no word document in win11? There is no word document solution tutorial in win11
猜你喜欢

Energy principle and variational method note 16: solution of virtual displacement principle
![[激光器原理与应用-8]: 激光器电路的电磁兼容性EMC设计](/img/98/8b7a4fc3f9ef9b7e16c63a8c225b02.png)
[激光器原理与应用-8]: 激光器电路的电磁兼容性EMC设计

干货!神经网络中的隐性稀疏正则效应

Baidu map data visualization

Cannot read properties of null (reading ‘pickAlgorithm‘)

MongoDB-查询语句中$exists以及结合$ne、$nin、$nor、$not使用介绍

梅科尔工作室-小熊派开发笔记2

Attack and defense world web question Fakebook

TASK03|回归
![[untitled]](/img/2f/cfac9dcac7466cf3ac5601b0fadf03.png)
[untitled]
随机推荐
QT with OpenGL (frame cache)
百度地图数据可视化
[PM2] PM2 common commands
【无标题】
Data warehouse 4.0 notes - data warehouse environment construction - DataGrid preparation and data preparation
MySQL data recovery - using the data directory
17.生命周期
I deliberately leave a loophole in the code. Is it illegal?
Compiler llvm MLIR introductions llvm backend instruction
Viewing the "Empathy" energy of iqoo 10 pro from 200W super flash charging
Attack and defense world web question Fakebook
TASK03|回归
Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 6
重装系统后故障(报错:reboot and select proper boot deviceor insert boot media in selected boot device)
Energy principle and variational method note 16: solution of virtual displacement principle
牛客C基础题练习
Lecture 9 of project practice -- operation import and export tool
小程序頭像組樣式
Principe de l'énergie et méthode variationnelle note 19: principe de l'énergie résiduelle minimale + principe du travail possible
非局部均值滤波(NON-LOCAL-mean)/注意力机制