Skip to content

2024-06-21 v. 5.9.4: added "2. Add Two Numbers" #651

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 1 commit into from
Jun 21, 2024
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,3 +454,9 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 2540. Minimum Common Value | [Link](https://leetcode.com/problems/minimum-common-value/) | [Link](./lib/easy/2540_minimum_common_value.rb) |
| 2544. Alternating Digit Sum | [Link](https://leetcode.com/problems/alternating-digit-sum/) | [Link](./lib/easy/2544_alternating_digit_sum.rb) |
| 2549. Count Distinct Numbers on Board | [Link](https://leetcode.com/problems/count-distinct-numbers-on-board/) | [Link](./lib/easy/2549_count_distinct_numbers_on_board.rb) |

### Medium

| Name | Link to LeetCode | Link to solution |
|--------------------|--------------------------------------------------------|-------------------------------------------|
| 2. Add Two Numbers | [Link](https://leetcode.com/problems/add-two-numbers/) | [Link](./lib/medium/2_add_two_numbers.rb) |
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '5.9.3'
s.version = '5.9.4'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
s.executable = 'leetcode-ruby'
Expand Down
66 changes: 66 additions & 0 deletions lib/medium/2_add_two_numbers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

require_relative '../common/linked_list'

# https://leetcode.com/problems/add-two-numbers/
# @param {ListNode} l1
# @param {ListNode} l2
# @return {ListNode}
def add_two_numbers(l1, l2)
sum = ::ListNode.new
p = sum
add = 0
while !l1.nil? && !l2.nil?
a = l1.val
b = l2.val
s = a + b + add

if s > 9
s -= 10
add = 1
else
add = 0
end

p.next = ::ListNode.new(s)
p = p.next
l1 = l1.next
l2 = l2.next
end

if !l1.nil?
until l1.nil?
s = l1.val + add

if s > 9
s -= 10
add = 1
else
add = 0
end

p&.next = ::ListNode.new(s)
p = p&.next
l1 = l1.next
end
elsif !l2.nil?
until l2.nil?
s = l2.val + add

if s > 9
s -= 10
add = 1
else
add = 0
end

p&.next = ::ListNode.new(s)
p = p&.next
l2 = l2.next
end
end

p&.next = ::ListNode.new(1) if add == 1

sum.next
end
51 changes: 51 additions & 0 deletions test/medium/test_2_add_two_numbers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/2_add_two_numbers'
require_relative '../../lib/common/linked_list'
require 'minitest/autorun'

class AddTwoNumbersTest < ::Minitest::Test
def test_default
assert(
::ListNode.are_equals(
::ListNode.from_array([7, 0, 8]),
add_two_numbers(
::ListNode.from_array([2, 4, 3]),
::ListNode.from_array([5, 6, 4])
)
)
)
assert(
::ListNode.from_array([0]),
add_two_numbers(
::ListNode.from_array([0]),
::ListNode.from_array([0])
)
)
assert(
::ListNode.from_array([8, 9, 9, 9, 0, 0, 0, 1]),
add_two_numbers(
::ListNode.from_array([9, 9, 9, 9, 9, 9, 9]),
::ListNode.from_array([9, 9, 9, 9])
)
)
end

def test_additional
assert(
::ListNode.from_array([8, 9, 9, 9, 0, 0, 0]),
add_two_numbers(
::ListNode.from_array([9, 9, 9, 9, 9, 9, 2]),
::ListNode.from_array([9, 9, 9, 9])
)
)
assert(
::ListNode.from_array([8, 9, 9, 9, 0, 0, 0]),
add_two_numbers(
::ListNode.from_array([9, 9, 9, 9]),
::ListNode.from_array([9, 9, 9, 9, 9, 9, 2])
)
)
end
end
Loading