From b731afa78783c93202e23858ac34b94fb6c50757 Mon Sep 17 00:00:00 2001 From: Cool <747682928@qq.com> Date: Wed, 6 Nov 2024 18:06:25 +0800 Subject: [PATCH] =?UTF-8?q?2024/11/6=20=E7=81=B5=E8=8C=B6=E9=A2=98?= =?UTF-8?q?=E5=8D=95=20=E6=BB=91=E5=8A=A8=E7=AA=97=E5=8F=A3=20=E7=9B=B8?= =?UTF-8?q?=E5=90=91=E5=8F=8C=E6=8C=87=E9=92=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sliding_windows/Num977.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/main/java/com/cool/ling_cha_mount/sliding_windows/Num977.java 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; + } +}