-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path283.2_move_zeros.rb
61 lines (45 loc) · 1.15 KB
/
283.2_move_zeros.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# frozen_string_literal: true
# https://leetcode.com/problems/move-zeroes/
require_relative '../test_helper'
# Runtime: 130 ms, faster than 78.37% of Ruby online submissions for Move Zeroes.
# Memory Usage: 213.2 MB, less than 5.09% of Ruby online submissions for Move Zeroes.
class Solution
class << self
def move_zeroes(nums)
zero = 0
nums.size.times do |i|
next if nums[i] == 0
nums[zero], nums[i] = nums[i], nums[zero]
zero += 1
end
nums
end
end
end
class TestSolution < Minitest::Test
def test_1
nums = [0, 1, 0, 3, 12]
output = [1, 3, 12, 0, 0]
assert_equal output, Solution.move_zeroes(nums)
end
def test_2
nums = [0]
output = [0]
assert_equal output, Solution.move_zeroes(nums)
end
def test_3
nums = [0, 0]
output = [0, 0]
assert_equal output, Solution.move_zeroes(nums)
end
def test_4
nums = [2, 1]
output = [2, 1]
assert_equal output, Solution.move_zeroes(nums)
end
def test_5
nums = [4, 2, 4, 0, 0, 3, 0, 5, 1, 0]
output = [4, 2, 4, 3, 5, 1, 0, 0, 0, 0]
assert_equal output, Solution.move_zeroes(nums)
end
end