-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6_zigzag_conversion.rb
More file actions
43 lines (42 loc) · 913 Bytes
/
6_zigzag_conversion.rb
File metadata and controls
43 lines (42 loc) · 913 Bytes
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
#faster solution
def convert(s, num_rows)
return s if num_rows >= s.size || num_rows == 1
s = s.chars
rows = ''
num_rows.times do |n|
row = ''
pos = n
while s[pos]
row << s[pos]
if n != 0 && n != num_rows - 1
zig_pos = pos + (num_rows - n - 1) * 2
row << s[zig_pos] if s[zig_pos]
end
pos += 2 * num_rows - 2
end
rows << row
end
rows
end
#slower sulotion
def convert(s, num_rows)
return s if num_rows >= s.size
cols = []
while s.size != 0
col = s.slice!(0...num_rows).chars
if col.size == num_rows
cols << col
else
col.insert(-1, *([nil] * (num_rows - col.size)))
cols << col
break
end
(num_rows - 2).downto(1).each do |i|
break if s.size == 0
cols << ([nil] * (num_rows - 1)).insert(i, s.slice!(0, 1))
end
end
cols.transpose.map do |a|
a.compact.join
end.join
end