Member-only story

Leetcode 66: Plus One

Pierre-Marie Poitevin
3 min readDec 29, 2021

--

In this problem, we are adding 1 to an arbitrary large integer, the integer is represented in a array of integers representing each digit.

I published the solution in the video format there if you like this format: https://www.youtube.com/watch?v=J6tN_oBhpz8

This is the problem statement:

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.

And there are a couple of examples given:

Examples for testing

Analysis

Examples

We first look at 3 examples to get an idea of how it works.

[1,2,3] -> [1,2,4]

This is obvious, add to the last digit.

[9,9,9] -> [1,0,0,0]

This is different because the result will have one more digit than the input. This happens only when all the digits are 9s.

[1,1,9,9] -> [1,2,0,0]

This is a hybrid of the 2 previous cases. The trailing 9s become 0s and we add 1 to the first non-9…

--

--

Pierre-Marie Poitevin
Pierre-Marie Poitevin

No responses yet