Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitee99f06

Browse files
authored
Added task 2
1 parent105c20e commitee99f06

File tree

6 files changed

+139
-3
lines changed

6 files changed

+139
-3
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Definition for singly-linked list.
2+
classListNode
3+
attr_accessor:val,:next
4+
5+
definitialize(x)
6+
@val=x
7+
@next=nil
8+
end
9+
10+
defto_a
11+
result=[]
12+
current=self
13+
whilecurrent
14+
result <<current.val
15+
current=current.next
16+
end
17+
result
18+
end
19+
end

‎src/main/ruby/g0001_0100/s0001_two_sum/solution.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//#Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table
2-
//#Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays #Big_O_Time_O(n)_Space_O(n)
3-
//#2023_11_08_Time_57_ms_(89.38%)_Space_212.2_MB_(11.34%)
1+
# #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table
2+
# #Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays #Big_O_Time_O(n)_Space_O(n)
3+
# #2023_11_08_Time_57_ms_(89.38%)_Space_212.2_MB_(11.34%)
44

55
# @param {Integer[]} nums
66
# @param {Integer} target
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2\. Add Two Numbers
2+
3+
Medium
4+
5+
You are given two**non-empty** linked lists representing two non-negative integers. The digits are stored in**reverse order**, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
6+
7+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg)
12+
13+
**Input:** l1 =[2,4,3], l2 =[5,6,4]
14+
15+
**Output:**[7,0,8]
16+
17+
**Explanation:** 342 + 465 = 807.
18+
19+
**Example 2:**
20+
21+
**Input:** l1 =[0], l2 =[0]
22+
23+
**Output:**[0]
24+
25+
**Example 3:**
26+
27+
**Input:** l1 =[9,9,9,9,9,9,9], l2 =[9,9,9,9]
28+
29+
**Output:**[8,9,9,9,0,0,0,1]
30+
31+
**Constraints:**
32+
33+
* The number of nodes in each linked list is in the range`[1, 100]`.
34+
*`0 <= Node.val <= 9`
35+
* It is guaranteed that the list represents a number that does not have leading zeros.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Math #Linked_List #Recursion
2+
# #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15
3+
# #Big_O_Time_O(max(N,M))_Space_O(max(N,M))
4+
5+
require_relative'../../com_github_leetcode/list_node'
6+
7+
# Definition for singly-linked list.
8+
# class ListNode
9+
# attr_accessor :val, :next
10+
# def initialize(val = 0, _next = nil)
11+
# @val = val
12+
# @next = _next
13+
# end
14+
# end
15+
# @param {ListNode} l1
16+
# @param {ListNode} l2
17+
# @return {ListNode}
18+
defadd_two_numbers(l1,l2)
19+
dummy_head=ListNode.new(0)
20+
p=l1
21+
q=l2
22+
curr=dummy_head
23+
carry=0
24+
25+
whilep ||q
26+
x=p ?p.val :0
27+
y=q ?q.val :0
28+
sum=carry +x +y
29+
carry=sum /10
30+
curr.next=ListNode.new(sum %10)
31+
curr=curr.next
32+
33+
p=p.nextifp
34+
q=q.nextifq
35+
end
36+
37+
curr.next=ListNode.new(carry)ifcarry >0
38+
39+
dummy_head.next
40+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require_relative"../../../main/ruby/com_github_leetcode/list_node"
2+
3+
classLinkedListUtils
4+
defself.construct_linked_list(arr)
5+
returnnilifarr.empty?
6+
7+
head=ListNode.new(arr.shift)
8+
current=head
9+
10+
arr.eachdo |val|
11+
current.next=ListNode.new(val)
12+
current=current.next
13+
end
14+
15+
head
16+
end
17+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require_relative"../../../../main/ruby/com_github_leetcode/list_node"
2+
require_relative"../../com_github_leetcode/linked_list_utils"
3+
require_relative'../../../../main/ruby/g0001_0100/s0002_add_two_numbers/solution'
4+
require'minitest/autorun'
5+
6+
classSolutionTest <Minitest::Test
7+
deftest_add_two_numbers
8+
list_node1=LinkedListUtils.construct_linked_list([2,4,3])
9+
list_node2=LinkedListUtils.construct_linked_list([5,6,4])
10+
result=add_two_numbers(list_node1,list_node2)
11+
assert_equal([7,0,8],result.to_a)
12+
end
13+
14+
deftest_add_two_numbers2
15+
result=add_two_numbers(ListNode.new(0),ListNode.new(0))
16+
assert_equal([0],result.to_a)
17+
end
18+
19+
deftest_add_two_numbers3
20+
list_node1=LinkedListUtils.construct_linked_list([9,9,9,9,9,9,9])
21+
list_node2=LinkedListUtils.construct_linked_list([9,9,9,9])
22+
result=add_two_numbers(list_node1,list_node2)
23+
assert_equal([8,9,9,9,0,0,0,1],result.to_a)
24+
end
25+
end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp