Skip to content

Commit 10c2bcc

Browse files
committed
2024-11-22 v. 7.0.9: added "445. Add Two Numbers II"
1 parent 5cc6769 commit 10c2bcc

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,3 +574,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
574574
| 438. Find All Anagrams in a String | [Link](https://leetcode.com/problems/find-all-anagrams-in-a-string/) | [Link](./lib/medium/438_find_all_anagrams_in_a_string.rb) | [Link](./test/medium/test_438_find_all_anagrams_in_a_string.rb) |
575575
| 442. Find All Duplicates in an Array | [Link](https://leetcode.com/problems/find-all-duplicates-in-an-array/) | [Link](./lib/medium/442_find_all_duplicates_in_an_array.rb) | [Link](./test/medium/test_442_find_all_duplicates_in_an_array.rb) |
576576
| 443. String Compression | [Link](https://leetcode.com/problems/string-compression/) | [Link](./lib/medium/443_string_compression.rb) | [Link](./test/medium/test_443_string_compression.rb) |
577+
| 445. Add Two Numbers II | [Link](https://leetcode.com/problems/add-two-numbers-ii/) | [Link](./lib/medium/445_add_two_numbers_ii.rb) | [Link](./test/medium/test_445_add_two_numbers_ii.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 = '7.0.8'
8+
s.version = '7.0.9'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'

lib/medium/445_add_two_numbers_ii.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/add-two-numbers-ii/
4+
# @param {ListNode} l1
5+
# @param {ListNode} l2
6+
# @return {ListNode}
7+
def add_two_numbers445(l1, l2)
8+
first_num = []
9+
while l1
10+
first_num << l1.val
11+
l1 = l1.next
12+
end
13+
14+
second_num = []
15+
while l2
16+
second_num << l2.val
17+
l2 = l2.next
18+
end
19+
20+
sum = (first_num.join.to_i + second_num.join.to_i).to_s
21+
22+
result = ::ListNode.new(0)
23+
curr = result
24+
sum.each_char do |c|
25+
curr.next = ::ListNode.new(c.to_i)
26+
curr = curr.next
27+
end
28+
29+
result.next
30+
end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/common/linked_list'
5+
require_relative '../../lib/medium/445_add_two_numbers_ii'
6+
require 'minitest/autorun'
7+
8+
class AddTwoNumbersIITest < ::Minitest::Test
9+
def test_default_one
10+
assert(
11+
::ListNode.are_equals(
12+
::ListNode.from_array(
13+
[7, 8, 0, 7]
14+
),
15+
add_two_numbers445(
16+
::ListNode.from_array(
17+
[7, 2, 4, 3]
18+
),
19+
::ListNode.from_array(
20+
[5, 6, 4]
21+
)
22+
)
23+
)
24+
)
25+
end
26+
27+
def test_default_two
28+
assert(
29+
::ListNode.are_equals(
30+
::ListNode.from_array(
31+
[8, 0, 7]
32+
),
33+
add_two_numbers445(
34+
::ListNode.from_array(
35+
[2, 4, 3]
36+
),
37+
::ListNode.from_array(
38+
[5, 6, 4]
39+
)
40+
)
41+
)
42+
)
43+
end
44+
45+
def test_default_three
46+
assert(
47+
::ListNode.are_equals(
48+
::ListNode.from_array([0]),
49+
add_two_numbers445(
50+
::ListNode.from_array([0]),
51+
::ListNode.from_array([0])
52+
)
53+
)
54+
)
55+
end
56+
end

0 commit comments

Comments
 (0)