- Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0989-add-to-array-form-of-integer.rb
37 lines (29 loc) · 986 Bytes
/
0989-add-to-array-form-of-integer.rb
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
# frozen_string_literal: true
# 989. Add to Array-Form of Integer
# https://leetcode.com/problems/add-to-array-form-of-integer
# TODO: IMPROVE
# @param {Integer[]} num
# @param {Integer} k
# @return {Integer[]}
defadd_to_array_form1(num,k)
(num.join.to_i + k).digits.reverse
end
# ********************#
# TEST #
# ********************#
require"test/unit"
classTest_add_to_array_form < Test::Unit::TestCase
deftest_
assert_equal[1,2,3,4],add_to_array_form1([1,2,0,0],34)
assert_equal[4,5,5],add_to_array_form1([2,7,4],181)
assert_equal[1,0,2,1],add_to_array_form1([2,1,5],806)
end
end
require"benchmark"
num=[1,0,2,9,5,8,7,3,6,7,9,1,3,4,6,7,8,9,5,4,3,4,6,7,7,6,5,4,3,0,1]
k=458971347
Benchmark.bmdo |x|
x.report("add_to_array_form1: "){add_to_array_form1(num,k)}
end
# user system total real
# add_to_array_form1: 0.000033 0.000025 0.000058 ( 0.000054)