mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加0977有序数组的平方 Ruby 版本
This commit is contained in:
@ -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
|
||||
```
|
||||
|
||||
|
||||
|
||||
-----------------------
|
||||
|
Reference in New Issue
Block a user