From ed7e252e14ce9d2ba151d8080d7098e428350233 Mon Sep 17 00:00:00 2001 From: KailokFung Date: Thu, 10 Jun 2021 11:21:03 +0800 Subject: [PATCH] =?UTF-8?q?feat(977):=20=E6=96=B0=E5=A2=9Ejava=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0977.有序数组的平方.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index b5d392e6..d28a9427 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -96,9 +96,27 @@ public: ## 其他语言版本 - Java: +```java +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; + } +} +``` + Python: ```Python class Solution: