-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0066-plus-one.rb
46 lines (35 loc) · 1.15 KB
/
0066-plus-one.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
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true
# 66. Plus One
# https://leetcode.com/problems/plus-one
=begin
You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.
Increment the large integer by one and return the resulting array of digits.
# Constraints:
* 1 <= digits.length <= 100
* 0 <= digits[i] <= 9
* digits does not contain any leading 0's.
=end
# @param {Integer[]} digits
# @return {Integer[]}
def plus_one(digits)
carryover, pointer = 1, -1
while carryover.positive?
break digits.unshift(carryover) if digits[pointer].nil?
sum = digits[pointer] + carryover
carryover = sum / 10
digits[pointer] = sum % 10
pointer -= 1
end
digits
end
# ********************#
# TEST #
# ********************#
require "test/unit"
class Test_plus_one < Test::Unit::TestCase
def test_
assert_equal [1, 2, 4], plus_one([1, 2, 3])
assert_equal [4, 3, 2, 2], plus_one([4, 3, 2, 1])
assert_equal [1, 0], plus_one([9])
end
end