Given a non-empty array of digits representing a non-negative integer, plus 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 contain a single digit.

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

Example 1:


Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.


Example 2:


Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.

Source: leetcode | Difficulty: EASY
Tags: Data structures

Mathematics

Starter code: class Solution:
    def plusOne(self, digits: List[int]) -> List[int]:
        

Solutions:
class Solution:

	def plusOne(self, digits):
		carry = 1
		for i in range(len(digits) - 1, -1, -1):
			digits[i] += carry
			if digits[i] > 9:
				digits[i] -= 10
				carry = 1
			else:
				carry = 0
			if carry == 0:
				break
		if carry == 1:
			digits.insert(0, 1)
		return digits


class Solution:

	def plusOne(self, digits):
		l = len(digits)
		i = l - 1
		while i >= 0:
			if digits[i] != 9:
				digits[i] += 1
				break
			else:
				digits[i] = 0
				i -= 1
		if i == -1:
			digits = [1] + digits
		return digits


class Solution:

	def plusOne(self, digits):
		i = len(digits) - 1
		carry = 1
		while carry != 0 or i >= 0:
			temp = digits[i] + carry
			digits[i] = temp % 10
			carry = temp // 10
			i -= 1
			if i < 0 and carry > 0:
				digits.insert(0, carry)
				carry = 0
		return digits


class Solution:

	def plusOne(self, digits):
		l = [str(i) for i in digits]
		l = int(''.join(l))
		l += 1
		l = list(str(l))
		l = [int(i) for i in l]
		return l


class Solution:

	def plusOne(self, digits):
		flag = 1
		i = len(digits) - 1
		while i >= 0:
			if digits[i] + flag < 10:
				digits[i] = digits[i] + flag
				flag = 0
			else:
				digits[i] = (digits[i] + flag) % 10
			i -= 1
		if flag == 1:
			digits.insert(0, 1)
		return digits


class Solution:

	def plusOne(self, digits):
		if not digits:
			return []
		i = len(digits) - 1
		while i >= 0:
			if digits[i] < 9:
				digits[i] += 1
				return digits
			else:
				digits[i] = 0
				i -= 1
		if digits[0] == 0:
			return [1] + digits


class Solution:

	def plusOne(self, digits):
		d = [str(x) for x in digits]
		num = int(''.join(d)) + 1
		return list(map(int, str(num)))


class Solution:

	def plusOne(self, digits):
		sum = 0
		for i in range(0, len(digits)):
			sum = sum * 10 + digits[i]
		return [int(i) for i in str(sum + 1)]


class Solution:

	def plusOne(self, digits):
		if not digits:
			return [1]
		carry = 1
		for i in reversed(range(len(digits))):
			if carry == 0:
				return digits
			(carry, digits[i]) = ((digits[i] + carry) // 10, (digits[i] + carry) % 10)
		if carry:
			return [1] + digits
		return digits


class Solution:

	def plusOne(self, digits):
		plus = 1
		for i in range(len(digits) - 1, -1, -1):
			if digits[i] + plus > 9:
				digits[i] = 0
				plus = 1
			else:
				digits[i] = digits[i] + plus
				plus = 0
		if plus == 1:
			digits.insert(0, 1)
		return digits


class Solution:

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