coding-interview|September 03, 2020|1 min read

Plus One - Leet Code Solution

TL;DR

Walk backwards — if digit < 9, increment and return. If digit is 9, set to 0 and carry. If you exit the loop, prepend a 1 (all 9s case like 999 → 1000).

Plus One - Leet Code Solution

Problem Statement

Given a non-empty array of digits representing a non-negative integer, increment one to the integer.

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.

Example

Input: digits = [1,2,3]
Output: [1,2,4]

Solution

It has very simple solution, just by iterating array from the end. And, keeping track of carry.
A simple complexity here is that result can be bigger than original array if we got a carry for the last sum.

Code

public int[] plusOne(int[] digits) {
   int l = digits.length;
   
   //initializing carry with the number we want to add for first time.
   int carry = 1;
   
   for (int i=l-1; i>=0; i--) {
      digits[i] = digits[i] + carry;
      
      carry = digits[i]/10;
      digits[i] = digits[i]%10;
   }

   // copy result to another array
   int targetSize = carry == 1 ? l+1 : l;
   int[] res = new int[targetSize];
   
   int i=0;
   if (carry == 1) {
      res[0] = carry;
      i = 1;
   }
   for (; i<targetSize; i++) {
      res[i] = digits[i-carry];
   }
   return res;
}

Complexity

Its O(n)

Related Posts

Replace all spaces in a string with %20

Replace all spaces in a string with %20

Problem Statement Replace all spaces in a string with ‘%20’ (three characters…

Valid Palindrome - Leet Code Solution

Valid Palindrome - Leet Code Solution

Problem Statement Given a string, determine if it is a palindrome, considering…

Valid Anagrams - Leet Code Solution

Valid Anagrams - Leet Code Solution

Problem Statement Given two strings s and t , write a function to determine if t…

Rotate Image - Leet Code Solution

Rotate Image - Leet Code Solution

Problem Statement You are given an n x n 2D matrix representing an image, rotate…

Validate Sudoku - Leet Code Solution

Validate Sudoku - Leet Code Solution

Problem Statement Determine if a 9x9 Sudoku board is valid. Only the filled…

Move Zeroes - Leet Code Solution

Move Zeroes - Leet Code Solution

Problem Statement Given an array nums, write a function to move all 0’s to the…

Latest Posts

Software Security in the AI Era: How to Write Secure Code When AI Writes Code Too

Software Security in the AI Era: How to Write Secure Code When AI Writes Code Too

In 2025, 72% of professional developers used AI-assisted coding tools daily. By…

SQL Injection: The Complete Guide to Understanding, Preventing, and Detecting SQLi Attacks

SQL Injection: The Complete Guide to Understanding, Preventing, and Detecting SQLi Attacks

SQL injection has been on the OWASP Top 10 since the list was created in 200…

Building a Vulnerability Detection System That Developers Actually Use

Building a Vulnerability Detection System That Developers Actually Use

Here’s a stat that should concern every security team: 73% of developers say…

How to Be a Full-Time Freelancer: Resources, Finding Clients, and Building a Sustainable Business

How to Be a Full-Time Freelancer: Resources, Finding Clients, and Building a Sustainable Business

Making the leap from full-time employment to freelancing is one of the most…

Deep Dive on Elasticsearch: A System Design Interview Perspective

Deep Dive on Elasticsearch: A System Design Interview Perspective

“If you’re searching, filtering, or aggregating over large volumes of semi…

Deep Dive on Apache Kafka: A System Design Interview Perspective

Deep Dive on Apache Kafka: A System Design Interview Perspective

“Kafka is not a message queue. It’s a distributed commit log that happens to be…