АД
Size: a a a
АД
SP
АД
S
DM
SP
АД
IS
IS
АД
АД
DP
IM
A
A
S
DP
D
Ruby
это язык не для вычислительных задачь, а для быстрого создания бизнес решений. Но то что на Leetcode
дают измерение скорости работы программы делает интересным оптимизацию.40ms
совсем не оптимально с позиции количества проходов:# https://leetcode.com/problems/move-zeroes/
# Runtime: 40 ms, faster than 68.40% of Ruby online submissions for Move Zeroes.
# Memory Usage: 9.9 MB, less than 50.00% of Ruby online submissions for Move Zeroes.
def move_zeroes(nums)
size = nums.count(0)
answer = nums.select{|x| x != 0} + ([0]*size)
nums.clear()
answer.each{|x| nums.push(x)}
end
# https://leetcode.com/problems/move-zeroes/
# Runtime: 44 ms, faster than 27.91% of Ruby online submissions for Move Zeroes.
# Memory Usage: 9.8 MB, less than 100.00% of Ruby online submissions for Move Zeroes.
def move_zeroes(nums)
i = 0
nums.each do |x|
if x != 0
nums[i] = x
i += 1
end
end
(i...nums.size).each do |j|
nums[j] = 0
end
end
Ruby
ES
Ruby
это язык не для вычислительных задачь, а для быстрого создания бизнес решений. Но то что на Leetcode
дают измерение скорости работы программы делает интересным оптимизацию.40ms
совсем не оптимально с позиции количества проходов:# https://leetcode.com/problems/move-zeroes/
# Runtime: 40 ms, faster than 68.40% of Ruby online submissions for Move Zeroes.
# Memory Usage: 9.9 MB, less than 50.00% of Ruby online submissions for Move Zeroes.
def move_zeroes(nums)
size = nums.count(0)
answer = nums.select{|x| x != 0} + ([0]*size)
nums.clear()
answer.each{|x| nums.push(x)}
end
# https://leetcode.com/problems/move-zeroes/
# Runtime: 44 ms, faster than 27.91% of Ruby online submissions for Move Zeroes.
# Memory Usage: 9.8 MB, less than 100.00% of Ruby online submissions for Move Zeroes.
def move_zeroes(nums)
i = 0
nums.each do |x|
if x != 0
nums[i] = x
i += 1
end
end
(i...nums.size).each do |j|
nums[j] = 0
end
end
Ruby
ES