# 输出下一个更大的数字（一道Python题）求指点

• 11

## 注意，请审清题

n = 263457394695时，你的结果可能是997665544332但应该等于263457394956。

## 题目如下

12 ==> 21
513 ==> 531
2017 ==> 2071
9 ==> -1
111 ==> -1
531 ==> -1

``````def next_bigger(n):
res = int(''.join(sorted(list(str(n)),reverse=True)))
return res if res != n else -1``````

##### 5个回答

``````# from collections import namedtuple
# from typing import List
def find_next_greater(num: int) -> int:
ns: List[str] = [e for e in str(num)]
l: int = len(ns)
Needle = namedtuple('Needle', ['LAST', 'PENULTIMATE'])
needle: Needle = Needle(LAST=l-1, PENULTIMATE=l-2)
for i in range(needle.PENULTIMATE, -1, -1):
for j in range(needle.LAST, i, -1):
if ns[j] > ns[i]:
ns[i], ns[j] = ns[j], ns[i]
ns[i+1:] = sorted(ns[i+1:])
return int(''.join(ns))
return -1

find_next_greater(263457394695) == 263457394956  # True
find_next_greater(5446) == 5464  # True
find_next_greater(10006) == 10060  # True
find_next_greater(1000786) == 1000867  # True
find_next_greater(10086) == 10608  # True
find_next_greater(26) == 62  # True
find_next_greater(321) == -1  # True
find_next_greater(3212) == 3221  # True
find_next_greater(8) == -1  # True
find_next_greater(133398999998) == 133399889999  # True``````