given number A, find a number B which is bigger than that by using same digits available in the number A [closed]
You have given a positive n开发者_Python百科umber. You have to find a number which is immediate bigger than that by using same digits available in the number.
use same digits with same number of time, coming in positive integer and if a small number is not possible then we have to return -1.
For example: (1) You have given a number 7585 , your output should be 7855 . (2) 7111, return -1.
Thanks,
Zhong
Easy:
def findnext(i):
array = digitsOf(i)
n = max_int
for perm in permutations(array):
if(number(perm) > i):
n = min(number(perm), n)
if n=max_int:
return -1
else:
return n
Find a bigger digit after smaller (start from the least significant digit), then swap them.
convert to string break into individual numbers and push into an array sort the array descending array to string string to number if new number is higher than old, return it, if not return -1
精彩评论