Skip to content

Commit

Permalink
Merge pull request #212 from Dhavisco/66-Plus-One
Browse files Browse the repository at this point in the history
added PlusOne python code
  • Loading branch information
iamdestinychild authored Oct 8, 2023
2 parents bd46351 + ead0e5f commit 90dd624
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 25 deletions.
24 changes: 24 additions & 0 deletions LeetCode/66-Plus-One/Python/PlusOne.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution(object):
def plusOne(self, digits):
# Convert the list of digits to an integer
num = int(''.join(map(str, digits)))

# Increment the integer by one
num += 1

# Convert the incremented integer back to a list of digits
result = list(map(int, str(num)))

return result

def main():
# Create an instance of the Solution class
solution = Solution()

# Example usage
input_digits = [1, 2, 3]
result = solution.plusOne(input_digits)
print("Input:", input_digits)
print("Result:", result)

main()
48 changes: 48 additions & 0 deletions LeetCode/66-Plus-One/Python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Plus One - Increment Large Integer Represented as Array of Digits

## Problem Description

You are given a large integer represented as an array of digits, where each digit in the array corresponds to a digit of the integer. The digits are ordered from most significant (leftmost) to least significant (rightmost), and there are no leading zeros. Your task is to increment this large integer by one and return the updated array of digits while maintaining the order.

**Example 1:**
Input: `digits = [1, 2, 3]`
Output: `[1, 2, 4]`
Explanation: The input array represents the integer 123. Incrementing by one results in 124.

**Example 2:**
Input: `digits = [4, 3, 2, 1]`
Output: `[4, 3, 2, 2]`
Explanation: The input array represents the integer 4321. Incrementing by one results in 4322.

**Example 3:**
Input: `digits = [9]`
Output: `[1, 0]`
Explanation: The input array represents the integer 9. Incrementing by one results in 10.

## Approach
The approach to solving this problem efficiently involves the following steps:

Initialize variables, including n to store the number of digits in the input array and a loop to traverse the digits from right to left.

Start a loop from the least significant digit (rightmost) to the most significant digit (leftmost).

Inside the loop, increment the current digit by one and check if it becomes 10.

If the current digit is 10 after incrementing, set it to 0 and propagate a carry-over to the next digit.

If the current digit is not 10, there is no carry-over, and we can stop the loop.

After the loop, check if there is still a carry-over.

If there is a carry-over, insert an additional digit with a value of 1 at the beginning of the array.

Return the updated array, which now represents the incremented integer.

## Complexity Analysis
- Time complexity: O(n)
- The algorithm iterates through the digits once, where n is the number of digits in the input array. Each digit is examined once in a linear fashion.
- Space complexity: O(1)
- The algorithm uses a constant amount of extra space for variables, regardless of the size of the input array. There is no significant space dependency on the input size.

This solution efficiently increments a large integer represented as an array of digits and provides the updated array as output, all while maintaining a low time and space complexity.

Binary file added LeetCode/66-Plus-One/Python/Screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 0 additions & 11 deletions LeetCode/Concatenation-of-Array/Python/app.py

This file was deleted.

14 changes: 0 additions & 14 deletions LeetCode/Concatenation-of-Array/python/app.py

This file was deleted.

0 comments on commit 90dd624

Please sign in to comment.