当前位置:网站首页>Array - 977. Square of ordered array
Array - 977. Square of ordered array
2022-07-23 22:15:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
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 .
2 Title Example
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]
3 Topic tips
- 1 <= nums.length <= 104
- -104 <= nums[i] <= 104
- nums Pressed Non decreasing order Sort
4 Ideas
Arrays are actually ordered , It's just that the negative square may be the maximum .
Then the maximum square of the array is at both ends of the array , Either the far left or the far right , It can't be in the middle .
At this point, we can consider the double pointer method ,i Point to the starting position ,j Point to the end position .
Define a new array result, and A Array size , Give Way k Point to result Array end position .
5 My answer
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]) {
// The relative position of positive numbers is constant , What needs to be adjusted is the relative position after the square of the negative number
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;
}
}
边栏推荐
- Sixty final review questions of software architecture
- 实时监控Mysql数据库变化_进行数据同步_了解Canal_---Canal工作笔记001
- 小说里的编程 【连载之十七】元宇宙里月亮弯弯
- 还在为XShell破解烦恼,试试tabby
- JMeter performance comprehensive practice - sign in and batch sign in
- Jmeter性能综合实战——签到及批量签到
- [golang learning notes] is parameter transfer in go language value transfer or reference transfer
- ADB 命令结合 monkey 的简单使用,超详细
- STM32+ESP8266+MQTT协议连接阿里云物联网平台
- Introduction and project development of MVVM and mvvmlight (I)
猜你喜欢

The simple use of ADB command combined with monkey is super detailed
![Use of [golang learning notes] package](/img/be/71cb066f44ce4bdab13c79c2da2652.png)
Use of [golang learning notes] package

How does MySQL prepare SQL (solve the problem that in query SQL preprocessing can only query one record)

Golang invalid argument to intn报错的解决

机器学习习题——对率回归

大淘营批量采集商品,如何将未上传的宝贝保存下来等后面再导入采集上传

Profit logic of DFI project 2021-04-26

Can Verilog of synthetizable be integrated

10 basic written interview questions, how many questions can you answer?

Memory search - DP
随机推荐
Distributed transaction scheme: best effort notification scheme
Taoying collects goods in batches. How to save the babies that have not been uploaded and then import them later
ADB 命令结合 monkey 的简单使用,超详细
Quick review of interview (III): probability theory and mathematical statistics
jedis 6---redisson和jedis的入门和不同
U++学习笔记 TSubclassOf()
【HiFlow】腾讯云新一代自动化助手,我用它完成了企业疫情提示(无代码)
初探POC编写
软件体系结构期末复习六十题
Uniapp uses canvas to write a circular progress bar
Zhongbang technology devotes itself to another work -- gyro maker OA system
JS object array de duplication
[golang learning notes] is parameter transfer in go language value transfer or reference transfer
vip股票账户在手机开通安全吗?
MySQL index transaction
U++ 事件
[learning notes] diameter and center of gravity of the tree
Yolo7 mask recognition practice
Explain NAT technology in detail
Comment forcer complètement le meurtre de processus indépendants de l'arrière - plan?