Merge pull request #609 from Reoooh/master

添加0977有序数组的平方 Ruby 版本
This commit is contained in:
程序员Carl 2021-08-17 15:08:39 +08:00 committed by GitHub
commit 66c22c015e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 0 deletions

View File

@ -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
```
-----------------------