Skip to content

Ruby solutions for 007, 013 and 014, updated solution 002 #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/**/*
28 changes: 7 additions & 21 deletions solution/002.Add Two Numbers/Solution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# @param {ListNode} l1
# @param {ListNode} l2
# @return {ListNode}

def add_two_numbers(l1, l2)
return l2 if l1 == nil
return l1 if l2 == nil
Expand All @@ -20,32 +21,17 @@ def add_two_numbers(l1, l2)

l1 = l1.next
l2 = l2.next
while !l1.nil? && !l2.nil?
cur_val = l1.val + l2.val + add
tmp.next = ListNode.new(cur_val % 10)
tmp = tmp.next
add = cur_val >= 10 ? 1 : 0

l1 = l1.next
l2 = l2.next
end

until l1.nil?
cur_val = l1.val + add
while !l1.nil? || !l2.nil? || add > 0
cur_val = add
cur_val += l1.nil? ? 0 : l1.val
cur_val += l2.nil? ? 0 : l2.val
tmp.next = ListNode.new(cur_val % 10)
tmp = tmp.next
add = cur_val >= 10 ? 1 : 0
l1 = l1.next
end

until l2.nil?
cur_val = l2.val + add
tmp.next = ListNode.new(cur_val % 10)
tmp = tmp.next
add = l2.val + add >= 10 ? 1 : 0
l2 = l2.next
l1 = l1.nil? ? l1 : l1.next
l2 = l2.nil? ? l2 : l2.next
end

tmp.next = ListNode.new(1) if add == 1
l3
end
21 changes: 21 additions & 0 deletions solution/007.Reverse Integer/Solution.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# @param {Integer} x
# @return {Integer}
def reverse(x)
neg = x < 0

x = x.abs
s = ''

x /= 10 while x > 0 && (x % 10).zero?

while x > 0
s += (x % 10).to_s
x /= 10
end

s = neg ? '-' + s : s

# have to explicitly constraint the int boundary as per the dummy test case
res = s.to_i
res <= 214_748_364_7 && res >= -214_748_364_8 ? res : 0
end
32 changes: 32 additions & 0 deletions solution/013.Roman to Integer/Solution.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# @param {String} s
# @return {Integer}
def roman_to_int(s)
hash = Hash[
'I' => 1,
'V' => 5,
'X' => 10,
'L' => 50,
'C' => 100,
'D' => 500,
'M' => 1000,
'IV' => 4,
'IX' => 9,
'XL' => 40,
'XC' => 90,
'CD' => 400,
'CM' => 900
]
res = 0
i = 0
while i < s.length
if i < s.length - 1 && !hash[s[i..i+1]].nil?
res += hash[s[i..i+1]]
i += 2
else
res += hash[s[i]]
i += 1
end
end

res
end
25 changes: 25 additions & 0 deletions solution/014.Longest Common Prefix/Solution.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# @param {String[]} strs
# @return {String}
def longest_common_prefix(strs)
return '' if strs.nil? || strs.length.zero?

return strs[0] if strs.length == 1

idx = 0
while idx < strs[0].length
cur_char = strs[0][idx]

str_idx = 1
while str_idx < strs.length
return idx > 0 ? strs[0][0..idx-1] : '' if strs[str_idx].length <= idx

return '' if strs[str_idx][idx] != cur_char && idx.zero?
return strs[0][0..idx - 1] if strs[str_idx][idx] != cur_char
str_idx += 1
end

idx += 1
end

idx > 0 ? strs[0][0..idx] : ''
end