开发者

given number A, find a number B which is bigger than that by using same digits available in the number A [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜