Plus One

Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.

Assumptions: 1. do not contain any leading zero 2. the most significant digit is at the head of the list

Example

I: [1, 2, 3]
O: [1, 2, 4]
I: [9, 9, 9]
O: [1, 0, 0, 0]

Analysis

Easy array/math are popular warm up questions. Practice speed and bug free.

Brute Force Approach:

linear scan the array from right to left
keep track of a carry
check if (carry == 1) in the end
then create a new array with size + 1

Code w/ side effects

Code w/o side effects

Last updated