diff --git a/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num977.java b/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num977.java new file mode 100644 index 0000000..dad16a7 --- /dev/null +++ b/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num977.java @@ -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; + } +}