当前位置:网站首页>997、有序数组的平方(数组)
997、有序数组的平方(数组)
2022-04-23 10:12:00 【Popuessing's Jersey】
题目:
给你一个按 非递减顺序 排序的整数数组 nums,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序。
示例 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]
方法一:暴力解法
import java.util.Arrays;
public class Youxushuzupingfang {
public int[] sortedSquares(int [] nums){
for (int i = 0; i <nums.length ; i++) {
nums[i]*=nums[i];
}
//快排
Arrays.sort(nums);
return nums;
}
public static void main(String[] args) {
int [] nums = {-7,-3,2,3,11};
Youxushuzupingfang youxushuzupingfang = new Youxushuzupingfang();
int [] res =youxushuzupingfang.sortedSquares(nums);
for (int x:res) {
System.out.print(x+" ");
}
}
}
时间复杂度:O(n+nlogn):其中n是第一个for循环平方产生的,nlogn是快速排序产生的
空间复杂度:O(logn)
输出结果:
4 9 9 49 121
方法二:双指针法
平方的最大值只可能产生于数组的两端,不可能出现在数组中间,因此可以使用双指针法,分别指向数组的左右两端,比较指针指向的数平方的大小。
public int[] sortedSquares(int [] nums){
int n = nums.length;
//创建一个新的数组保存平方后的值
int [] temp = new int [n];
//设置左右指针
for (int i=0,j=n-1,k=n-1; k>=0 ;k--) {
if(nums[i]*nums[i]>nums[j]*nums[j]){
temp[k] = nums[i]*nums[i];
i++;
}else if(nums[i]*nums[i]<nums[j]*nums[j]){
temp[k] = nums[j]*nums[j];
j--;
}else {//i,j指向的元素平方后的值相等
temp[k] = nums[i]*nums[i];
}
}
return temp;
}
public static void main(String[] args) {
int [] nums = {-7,-3,2,3,11};
Youxushuzupingfang youxushuzupingfang = new Youxushuzupingfang();
int [] res =youxushuzupingfang.sortedSquares(nums);
for (int x:res) {
System.out.print(x+" ");
}
}
时间复杂度:O(n)
空间复杂度:O(n)
其他版本的双指针解法:对比上面的代码,值得我改进的地方有,使用while循环不需要判断循环次数,左右指针使用left,right表述更加直观
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;
}
}
版权声明
本文为[Popuessing's Jersey]所创,转载请带上原文链接,感谢
https://blog.csdn.net/CoCo629vanilla/article/details/121365383
边栏推荐
- Understand the new economic model of platofarm and its ecological progress
- 利用多线程按顺序连续输出abc10次
- 2022年流动式起重机司机考试题库模拟考试平台操作
- MapReduce计算流程详解
- [CF 1425d] danger of mad snakes
- 【省选联考 2022 D2T1】卡牌(状态压缩 DP,FWT卷积)
- Exercise questions and simulation test of refrigeration and air conditioning equipment operation test in 2022
- Career planning and implementation in the era of meta universe
- Interviewer: let's talk about some commonly used PHP functions. Fortunately, I saw this article before the interview
- Sim Api User Guide(7)
猜你喜欢

Operation of 2022 tea artist (primary) test question simulation test platform

杰理之更准确地确定异常地址【篇】

NEC infrared remote control coding description

Sim Api User Guide(6)

JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer

Yarn核心参数配置

2022年上海市安全员C证考试题库及答案

C语言——自定义类型

Solve the problem of installing VMware after uninstalling

net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
随机推荐
Question bank and answers of Shanghai safety officer C certificate examination in 2022
24、两两交换链表中的节点(链表)
[COCI] Vje š TICA (subset DP)
第二章 In-Memory 体系结构 (IM-2.2)
Realize data value through streaming data integration (3) - real-time continuous data collection
2022茶艺师(初级)考试试题模拟考试平台操作
Chapter II in memory architecture (im-2.2)
基于PyQt5实现弹出任务进度条功能示例
DBA常用SQL语句(1)— 概况信息
第120章 SQL函数 ROUND
ansible 云计算 自动化 命令行精简版
2022年上海市安全员C证考试题库及答案
第二章 Oracle Database In-Memory 体系结构(上) (IM-2.1)
Using multithreading to output abc10 times in sequence
Juc并发编程06——深入剖析队列同步器AQS源码
DBA common SQL statements (2) - SGA and PGA
构建元宇宙时代敏捷制造的九种能力
Go language practice mode - functional options pattern
Realizing data value through streaming data integration (5) - stream processing
JUC concurrent programming 09 -- source code analysis of condition implementation