当前位置:网站首页>997. Square of ordered array (array)
997. Square of ordered array (array)
2022-04-23 10:15:00 【Popuessing's Jersey】
subject :
Here's a button Non decreasing order Sorted array of integers nums, return The square of each number A new array of , According to the requirements Non decreasing order Sort .
Example 1: Input :nums = [-4,-1,0,3,10] Output :[0,1,9,16,100] explain : After square , The array becomes [16,1,0,9,100], After ordering , The array becomes [0,1,9,16,100]
Example 2: Input :nums = [-7,-3,2,3,11] Output :[4,9,9,49,121]
Method 1 : Violence solution
import java.util.Arrays;
public class Youxushuzupingfang {
public int[] sortedSquares(int [] nums){
for (int i = 0; i <nums.length ; i++) {
nums[i]*=nums[i];
}
// Quick line up
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+" ");
}
}
}
Time complexity :O(n+nlogn): among n Is the first for Generated by cyclic square ,nlogn It is generated by quick sort
Spatial complexity :O(logn)
Output results :
4 9 9 49 121
Method 2 : Double finger needling
The maximum square can only be generated at both ends of the array , Can't be in the middle of an array , Therefore, two finger acupuncture can be used , Point to the left and right ends of the array , Compare the size of the square of the number pointed to by the pointer .
public int[] sortedSquares(int [] nums){
int n = nums.length;
// Create a new array and save the squared value
int [] temp = new int [n];
// Set the left and right pointers
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 The value after the square of the element pointed to is equal
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+" ");
}
}
Time complexity :O(n)
Spatial complexity :O(n)
Other versions of the double pointer solution : Compare the code above , There are some things worth improving , Use while The cycle does not need to judge the number of cycles , Left and right pointers use left,right More intuitive expression
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://yzsam.com/2022/04/202204231011141340.html
边栏推荐
- 【无标题】
- DBA常用SQL语句(3)- cache、undo、索引和等待事件
- SQL调优系列文章之—SQL调优简介
- 第二章 Oracle Database In-Memory 体系结构(上) (IM-2.1)
- 454、四数之和(哈希表)
- 计算机网络安全实验二|DNS协议漏洞利用实验
- Realize data value through streaming data integration (3) - real-time continuous data collection
- Chapter 2 Oracle database in memory architecture (I) (im-2.1)
- Sim Api User Guide(6)
- Realizing data value through streaming data integration (5) - stream processing
猜你喜欢
2022 mobile crane driver test question bank simulation test platform operation
Sim Api User Guide(4)
解决方案架构师的小锦囊 - 架构图的 5 种类型
NEC infrared remote control coding description
Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022
Solve the problem of installing VMware after uninstalling
JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer
net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
Shell script interaction free
《Redis设计与实现》
随机推荐
第120章 SQL函数 ROUND
Realize data value through streaming data integration (1)
209、长度最小的子数组(数组)
Sim Api User Guide(8)
通过流式数据集成实现数据价值(2)
Sim Api User Guide(4)
LeetCode-608. Tree node
1、两数之和(哈希表)
Turn: Maugham: reading is a portable refuge
杰理之通常程序异常情况有哪些?【篇】
第三章 启用和调整IM列存储的大小(IM-3.1)
Configuration of LNMP
2022茶艺师(初级)考试试题模拟考试平台操作
Realizing data value through streaming data integration (4) - streaming data pipeline
Failureforwardurl and failureurl
【无标题】
Realize data value through streaming data integration (2)
DBA common SQL statements (1) - overview information
SQL tuning series - SQL performance methodology
0704、ansible----01