Skip to content

Commit f38541c

Browse files
committed
Solution Dijkstra-Edu#66 - SJ-Cipher/Edited - 02/02/2025
1 parent 067167a commit f38541c

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Explanation:
2+
-> Check each element of the array/list from the end to the beginning
3+
-> If the value is less than 9, add one and return the array/list
4+
-> Else set it to zero and continue the loop(since the adjacent number might be 9 as well)
5+
-> If all the numbers are set to zero(meaning all were 9's in input array), insert 1 in the beginning of the array/list and return it
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List
2+
3+
#SOLUTION
4+
class Solution:
5+
def plusOne(self, digits: List[int]) -> List[int]:
6+
for i in range(len(digits)-1,-1,-1): #iterating the list from the end to beginning
7+
if digits[i] < 9:
8+
digits[i]+=1
9+
return digits # if digit is less than 9 we add one and return
10+
else:
11+
digits[i]=0 #else make it zero and continue iterating
12+
return [1] + digits # if all are 0 (i.e all 9's), we add one at the start and return
13+
14+
if __name__ == "__main__":
15+
print(Solution().plusOne([9,9,9,9,9])) # Example input
16+
17+
#Time Complexity : O(n)

0 commit comments

Comments
 (0)