2024/11/6 灵茶题单 滑动窗口 相向双指针
This commit is contained in:
parent
6f85ee5738
commit
b731afa787
|
@ -0,0 +1,30 @@
|
||||||
|
package com.cool.ling_cha_mount.sliding_windows;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
*
|
||||||
|
* @Author: Cool
|
||||||
|
* @Date: 2024/11/06/18:06
|
||||||
|
* @Description: 977. 有序数组的平方
|
||||||
|
*/
|
||||||
|
public class Num977 {
|
||||||
|
public int[] sortedSquares(int[] nums) {
|
||||||
|
int index = 0;
|
||||||
|
int j = nums.length - 1;
|
||||||
|
int[] res = new int[nums.length];
|
||||||
|
int rIndex = nums.length - 1;
|
||||||
|
for (int i = 0; i < nums.length; i++) {
|
||||||
|
int a = nums[index] * nums[index];
|
||||||
|
int b = nums[j] * nums[j];
|
||||||
|
if (a > b) {
|
||||||
|
res[rIndex] = a;
|
||||||
|
index++;
|
||||||
|
} else {
|
||||||
|
res[rIndex] = b;
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
rIndex--;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue