Skip to content

Commit

Permalink
Added solution for Smallest Value of the Rearranged Number
Browse files Browse the repository at this point in the history
  • Loading branch information
Blacknahil committed Jan 3, 2024
1 parent 4293812 commit 135d906
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions week4/smallest-value-of-the-rearranged-number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution:
def smallestNumber(self, num: int) -> int:
n_s=str(num)
if num<0:
n_list=list(n_s[1:])
n_list.sort(reverse=True)
return int("-" + "".join(n_list))
else:
n_list=list(n_s)
n_list.sort()
for i in range(len(n_list)):
if n_list[i] !="0":
break
pos=i
while pos<len(n_list) and n_list[pos]=="0":
pos+=1
if pos<len(n_list):
n_list[i],n_list[pos]=n_list[pos],n_list[i]
break
return int("".join(n_list))


0 comments on commit 135d906

Please sign in to comment.