-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path415_add_strings.rb
More file actions
41 lines (35 loc) · 778 Bytes
/
Copy path415_add_strings.rb
File metadata and controls
41 lines (35 loc) · 778 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# frozen_string_literal: true
# https://leetcode.com/problems/add-strings/
# @param {String} num1
# @param {String} num2
# @return {String}
def add_strings(num1, num2)
result = ''
num1_p = num1.length - 1
num2_p = num2.length - 1
addition = 0
while num1_p >= 0 || num2_p >= 0
first = next_num(num1, num1_p)
second = next_num(num2, num2_p)
sum = first + second
if addition == 1
sum += addition
addition = 0
end
if sum >= 10
sum -= 10
addition = 1
end
result += sum.to_s
num1_p -= 1
num2_p -= 1
end
result += addition.to_s if addition == 1
result.reverse
end
# @param {String} num
# @param {Integer} index
# @return {Integer}
def next_num(num, index)
index >= 0 ? num[index].to_i(10) : 0
end