From b2122ef9e2306aa6e097446897195e7114dd09d0 Mon Sep 17 00:00:00 2001 From: reoooh Date: Sun, 15 Aug 2021 00:42:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A00977=E6=9C=89=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9=20Ruby=20?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0977.有序数组的平方.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index 9e71ec0d..71c46401 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -252,6 +252,24 @@ func sortedSquares(_ nums: [Int]) -> [Int] { } ``` +Ruby: + +```ruby +def sorted_squares(nums) + left, right, result = 0, nums.size - 1, [] + while left <= right + if nums[left]**2 > nums[right]**2 + result << nums[left]**2 + left += 1 + else + result << nums[right]**2 + right -= 1 + end + end + result.reverse +end +``` + -----------------------