diff --git a/.gitignore b/.gitignore index e69de29bb2d1d..03cd44e99ed7e 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1 @@ +.idea/**/* \ No newline at end of file diff --git a/solution/002.Add Two Numbers/Solution.rb b/solution/002.Add Two Numbers/Solution.rb index 1bed3e860aaf5..009b48b99f84f 100644 --- a/solution/002.Add Two Numbers/Solution.rb +++ b/solution/002.Add Two Numbers/Solution.rb @@ -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 @@ -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 \ No newline at end of file diff --git a/solution/007.Reverse Integer/Solution.rb b/solution/007.Reverse Integer/Solution.rb new file mode 100644 index 0000000000000..641671a8e4f84 --- /dev/null +++ b/solution/007.Reverse Integer/Solution.rb @@ -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 \ No newline at end of file diff --git a/solution/013.Roman to Integer/Solution.rb b/solution/013.Roman to Integer/Solution.rb new file mode 100644 index 0000000000000..327f6931bee24 --- /dev/null +++ b/solution/013.Roman to Integer/Solution.rb @@ -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 \ No newline at end of file diff --git a/solution/014.Longest Common Prefix/Solution.rb b/solution/014.Longest Common Prefix/Solution.rb new file mode 100644 index 0000000000000..d19d8118e128c --- /dev/null +++ b/solution/014.Longest Common Prefix/Solution.rb @@ -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 \ No newline at end of file