# Plus One – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.

You are given a **large integer** represented as an integer array `digits`

, where each `digits[i]`

is the `i`

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 ^{th}`0`

‘s.

Increment the large integer by one and return *the resulting array of digits*.

**Example 1:**

Input:digits = [1,2,3]Output:[1,2,4]Explanation:The array represents the integer 123. Incrementing by one gives 123 + 1 = 124. Thus, the result should be [1,2,4].

**Example 2:**

Input:digits = [4,3,2,1]Output:[4,3,2,2]Explanation:The array represents the integer 4321. Incrementing by one gives 4321 + 1 = 4322. Thus, the result should be [4,3,2,2].

**Example 3:**

Input:digits = [9]Output:[1,0]Explanation:The array represents the integer 9. Incrementing by one gives 9 + 1 = 10. Thus, the result should be [1,0].

**Constraints:**

`1 <= digits.length <= 100`

`0 <= digits[i] <= 9`

`digits`

does not contain any leading`0`

‘s.

# C++ Plus One LeetCode Solution

```
``````
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int n = digits.size() - 1;
for (int i = n; i >= 0; --i) { // traverse digits from the last element (least significant)
// since we begin with the last digit, increasing that digit by one
// results in overflow. Therefore, all elements PRIOR to digits[0]
// need to be considered since there may be additional nines between
// digits[0], ... , digits[n].
if (digits[i] == 9) {
digits[i] = 0;
} else { // current digit is not 9 so we can safely increment by one
digits[i] += 1;
return digits;
}
}
// if the program runs to this point, each 9 is now a 0.
// to get a correct solution, we need to add one more element with
// a value of zero AND set digits[0] to 1 (in the most significant position)
// to account for the carry digit.
digits.push_back(0);
digits[0] = 1;
return digits;
}
};
```

# Java Plus One LeetCode Solution

```
``````
public int[] plusOne(int[] digits) {
int n = digits.length;
for(int i=n-1; i>=0; i--) {
if(digits[i] < 9) {
digits[i]++;
return digits;
}
digits[i] = 0;
}
int[] newNumber = new int [n+1];
newNumber[0] = 1;
return newNumber;
}
```

# Python 3 Plus One LeetCode Solution

```
``````
def plusOne(digits):
num = 0
for i in range(len(digits)):
num += digits[i] * pow(10, (len(digits)-1-i))
return [int(i) for i in str(num+1)]
```

Array-1180

String-562

Hash Table-412

Dynamic Programming-390

Math-368

Sorting-264

Greedy-257

Depth-First Search-256

Database-215

Breadth-First Search-200

Tree-195

Binary Search-191

Matrix-176

Binary Tree-160

Two Pointers-151

Bit Manipulation-140

Stack-133

Heap (Priority Queue)-117

Design-116

Graph-108

Simulation-103

Prefix Sum-96

Backtracking-92

Counting-86

Sliding Window-73

Linked List-69

Union Find-66

Ordered Set-48

Monotonic Stack-47

Recursion-43

Trie-41

Binary Search Tree-40

Divide and Conquer-40

Enumeration-39

Bitmask-37

Queue-33

Memoization-32

Topological Sort-31

Geometry-30

Segment Tree-27

Game Theory-24

Hash Function-24

Binary Indexed Tree-21

Interactive-18

Data Stream-17

String Matching-17

Rolling Hash-17

Shortest Path-16

Number Theory-16

Combinatorics-15

Randomized-12

Monotonic Queue-9

Iterator-9

Merge Sort-9

Concurrency-9

Doubly-Linked List-8

Brainteaser-8

Probability and Statistics-7

Quickselect-7

Bucket Sort-6

Suffix Array-6

Minimum Spanning Tree-5

Counting Sort-5

Shell-4

Line Sweep-4

Reservoir Sampling-4

Eulerian Circuit-3

Radix Sort-3

Strongly Connected Componen-t2

Rejection Sampling-2

Biconnected Component-1

## Leave a comment below