- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Plus One in Python
Suppose we have an array of integers, say A. A will hold n elements, and they are non-negative. The whole array A is representing one large number. So if A = [5, 3, 2, 4] is given, it indicates the number 5324. We have to take that array A, then increase the number by 1, and again return the number like an array as given. So after increasing A will be [5, 3, 2, 5]
To solve this, we will follow these steps.
- Take the array and append each character into a string to make it string
- then convert the string to an integer, then increase the number by 1
- then split each digit and make another array
Let us see the following implementation to get a better understanding −
class Solution(object): def plusOne(self, digits): """ :type digits: List[int] :rtype: List[int] """ num = "" for i in digits: num +=str(i) num = int(num) num+=1 num = str(num) ans =  for i in num: ans.append(int(i)) return ans digits = [5,3,2,4] ob1 = Solution() print(ob1.plusOne(digits))
digits = [5,3,2,4]
- Plus One Linked List in C++
- What are the specifications of one plus 5?
- What are the specifications of one plus 3T phone?
- Cplus plus vs Java vs Python?
- Return the natural logarithm of one plus the input array element-wise in Numpy
- Instant plus() method in Java
- LocalDate plus() method in Java
- LocalDateTime plus() method in Java
- Duration plus() method in Java
- LocalTime plus() method in Java
- Add One in Python
- C Program to check Plus Perfect Number
- Now call facebook stream to google plus
- What is the Cost plus pricing method?
- MySQL query to insert current date plus specific time?