-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path525.3245.duplicate-zeros.rb
More file actions
70 lines (57 loc) · 1.52 KB
/
525.3245.duplicate-zeros.rb
File metadata and controls
70 lines (57 loc) · 1.52 KB
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
62
63
64
65
66
67
68
69
70
# https://leetcode.com/explore/learn/card/fun-with-arrays/525/inserting-items-into-an-array/3245/
# Test cases:
# arr = [1,0,2,3,0,4,5,0]
# arr = [1,2,3]
# arr = [0,0,0,0,0,0,0]
# arr = [9,9,9,4,8,0,0,3,7,2,0,0,0,0,9,1,0,0,1,1,0,5,6,3,1,6,0,0,2,3,4,7,0,3,9,3,6,5,8,9,1,1,3,2,0,0,7,3,3,0,5,7,0,8,1,9,6,3,0,8,8,8,8,0,0,5,0,0,0,3,7,7,7,7,5,1,0,0,8,0,0]
# Expected results:
# [1,0,0,2,3,0,0,4]
# [1,2,3]
# [0,0,0,0,0,0,0]
# [9,9,9,4,8,0,0,0,0,3,7,2,0,0,0,0,0,0,0,0,9,1,0,0,0,0,1,1,0,0,5,6,3,1,6,0,0,0,0,2,3,4,7,0,0,3,9,3,6,5,8,9,1,1,3,2,0,0,0,0,7,3,3,0,0,5,7,0,0,8,1,9,6,3,0,0,8,8,8,8,0]
# @param {Integer[]} arr
# @return {Void} Do not return anything, modify arr in-place instead.
# With shift elements
def duplicate_zeros(arr)
length = arr.length
shift = false
length.times do |i|
if shift
shift = false
next
end
if arr[i].zero? && arr[i + 1]
shift = true
tmp = arr[i + 1..length - 2]
arr[i + 1] = 0
arr[i + 2..length - 1] = tmp
end
end
end
# With new array
def duplicate_zeros(arr)
arr_init = arr.dup
arr_result = []
until arr_init.index(0).nil?
index = arr_init.index(0)
arr_result.push(*arr_init[0..index])
arr_result.push(0)
arr_init = arr_init[index + 1..arr_init.length - 2]
end
length = arr.length - 1
arr_result += arr_init
arr[0..length] = arr_result[0..length]
end
# With insert
def duplicate_zeros(arr)
i = 0
while i < arr.length
if arr[i] == 0
arr.insert(i + 1, 0)
arr.pop
i += 2
else
i += 1
end
end
end