Skip to main content

Plus One

Given an array representing a large integer Increment it by one and return the result Example: [1,2,3] => [1,2,4] Example: [9] => [1,0]

A non-negative integer is given as an array digits, most significant digit first, where each element is between 0 and 9 and there are no leading zeros unless the number is 0. Add one to the integer and return its digits in the same format. The result can be one digit longer than the input, as when [9,9,9] becomes [1,0,0,0].

Arrays & HashingMath & Geometry

Examples

You write

plus_one(digits: List[int])

  1. Example 1

    Input
    [1,2,3]
    returns
    [1,2,4]
  2. Example 2

    Input
    [4,3,2,1]
    returns
    [4,3,2,2]

The pattern behind it

Arrays & Hashing

I reach for it when

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

All 127 Arrays & Hashing problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Plus Oneeasy

Prep guides that list this problem

Our company tags come from public interview write-ups. None of these is a confirmed question.