Skip to content

Commit 1a689c3

Browse files
authored
Merge pull request #651 from fartem/2_Add_Two_Numbers
2024-06-21 v. 5.9.4: added "2. Add Two Numbers"
2 parents 482ecdd + f31c69e commit 1a689c3

File tree

4 files changed

+124
-1
lines changed

4 files changed

+124
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,9 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
454454
| 2540. Minimum Common Value | [Link](https://leetcode.com/problems/minimum-common-value/) | [Link](./lib/easy/2540_minimum_common_value.rb) |
455455
| 2544. Alternating Digit Sum | [Link](https://leetcode.com/problems/alternating-digit-sum/) | [Link](./lib/easy/2544_alternating_digit_sum.rb) |
456456
| 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) |
457+
458+
### Medium
459+
460+
| Name | Link to LeetCode | Link to solution |
461+
|--------------------|--------------------------------------------------------|-------------------------------------------|
462+
| 2. Add Two Numbers | [Link](https://leetcode.com/problems/add-two-numbers/) | [Link](./lib/medium/2_add_two_numbers.rb) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '5.9.3'
8+
s.version = '5.9.4'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
1111
s.executable = 'leetcode-ruby'

lib/medium/2_add_two_numbers.rb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../common/linked_list'
4+
5+
# https://leetcode.com/problems/add-two-numbers/
6+
# @param {ListNode} l1
7+
# @param {ListNode} l2
8+
# @return {ListNode}
9+
def add_two_numbers(l1, l2)
10+
sum = ::ListNode.new
11+
p = sum
12+
add = 0
13+
while !l1.nil? && !l2.nil?
14+
a = l1.val
15+
b = l2.val
16+
s = a + b + add
17+
18+
if s > 9
19+
s -= 10
20+
add = 1
21+
else
22+
add = 0
23+
end
24+
25+
p.next = ::ListNode.new(s)
26+
p = p.next
27+
l1 = l1.next
28+
l2 = l2.next
29+
end
30+
31+
if !l1.nil?
32+
until l1.nil?
33+
s = l1.val + add
34+
35+
if s > 9
36+
s -= 10
37+
add = 1
38+
else
39+
add = 0
40+
end
41+
42+
p&.next = ::ListNode.new(s)
43+
p = p&.next
44+
l1 = l1.next
45+
end
46+
elsif !l2.nil?
47+
until l2.nil?
48+
s = l2.val + add
49+
50+
if s > 9
51+
s -= 10
52+
add = 1
53+
else
54+
add = 0
55+
end
56+
57+
p&.next = ::ListNode.new(s)
58+
p = p&.next
59+
l2 = l2.next
60+
end
61+
end
62+
63+
p&.next = ::ListNode.new(1) if add == 1
64+
65+
sum.next
66+
end

test/medium/test_2_add_two_numbers.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/2_add_two_numbers'
5+
require_relative '../../lib/common/linked_list'
6+
require 'minitest/autorun'
7+
8+
class AddTwoNumbersTest < ::Minitest::Test
9+
def test_default
10+
assert(
11+
::ListNode.are_equals(
12+
::ListNode.from_array([7, 0, 8]),
13+
add_two_numbers(
14+
::ListNode.from_array([2, 4, 3]),
15+
::ListNode.from_array([5, 6, 4])
16+
)
17+
)
18+
)
19+
assert(
20+
::ListNode.from_array([0]),
21+
add_two_numbers(
22+
::ListNode.from_array([0]),
23+
::ListNode.from_array([0])
24+
)
25+
)
26+
assert(
27+
::ListNode.from_array([8, 9, 9, 9, 0, 0, 0, 1]),
28+
add_two_numbers(
29+
::ListNode.from_array([9, 9, 9, 9, 9, 9, 9]),
30+
::ListNode.from_array([9, 9, 9, 9])
31+
)
32+
)
33+
end
34+
35+
def test_additional
36+
assert(
37+
::ListNode.from_array([8, 9, 9, 9, 0, 0, 0]),
38+
add_two_numbers(
39+
::ListNode.from_array([9, 9, 9, 9, 9, 9, 2]),
40+
::ListNode.from_array([9, 9, 9, 9])
41+
)
42+
)
43+
assert(
44+
::ListNode.from_array([8, 9, 9, 9, 0, 0, 0]),
45+
add_two_numbers(
46+
::ListNode.from_array([9, 9, 9, 9]),
47+
::ListNode.from_array([9, 9, 9, 9, 9, 9, 2])
48+
)
49+
)
50+
end
51+
end

0 commit comments

Comments
 (0)