【每日一题20220810】寻找下一个更大的数字

:magic_wand: 创建一个函数,它接受一个正整数并返回下一个更大的数字,该数字可以通过重新排列其数字来形成。例如:

12 ==> 21
513 ==> 531
2017 ==> 2071
nextBigger(num: 12)   // returns 21
nextBigger(num: 513)  // returns 531
nextBigger(num: 2017) // returns 2071

如果无法重新排列数字以形成更大的数字,请返回-1

9 ==> -1
111 ==> -1
531 ==> -1

题目难度:一般
题目来源:https://www.codewars.com/kata/55983863da40caa2c900004e

def next_bigger(n:int) -> int:
    # your code here

assert next_bigger(12) == 21
assert next_bigger(513) == 531
assert next_bigger(2017) == 2071
assert next_bigger(414) == 441
assert next_bigger(144) == 414
assert next_bigger(123456789) == 123456798
assert next_bigger(1234567890) == 1234567908
assert next_bigger(9876543210) == -1
assert next_bigger(9999999999) == -1
assert next_bigger(59884848459853) == 59884848483559
def next_bigger(n:int) -> int:
    n_list = list(str(n))
    for i in range(len(n_list)-2,-1,-1):
        diff = float('inf')
        big_flag = False
        tmp_index = None
        for j in range(i+1,len(n_list)):
            tmp = int(n_list[j]) - int(n_list[i])
            if 0<tmp<diff:
                big_flag = True
                diff = tmp
                tmp_index = j
        if big_flag:
            n_list[i],n_list[tmp_index]=n_list[tmp_index],n_list[i]
            return int(''.join(n_list[:i+1]+sorted(n_list[i+1:])))
    else:
        return -1

手机…

解题思路:
1、拆解数字为数字集合,
然后排列组合组成所有可能性的数字,
最后与原数字进行比较,
比原数字大的值的中,
差值最小的那个数字就是下一个更大的数字,
如果差值为0则本身就是最大值!

def nextBigger(num: int):
    num1 = list(str(num))
    num2 = num1.copy()
    num2.sort(reverse=True)
    result = 0
    if num1 == num2:
        return -1
    else:
        num2.sort(reverse=False)
        for i in range(len(num2)):
            result1 = int(num2[i]) * pow(10, i)
            result += result1
        return result


print(nextBigger(513))
print(nextBigger(111))
print(nextBigger(5))
print(nextBigger(23456))
def next_bigger(n:int) -> int:
    if ''.join(sorted(str(n),reverse=True))==str(n):
        return -1
    else:
        for i in range(len(str(n))-1,-1,-1):
            if int(str(n)[i])>int(str(n)[i-1]):
                n1=str(n)[:i-1]
                n2=str(n)[i-1:]
                break
        min_n2=min([n2[j] for j in range(1,len(n2)) if n2[j]>n2[0]])
        n2_list=[n2[k] for k in range(len(n2))]
        index_min=n2_list.index(min_n2)
        n2_list.remove(n2_list[index_min])
        n2_list.sort(reverse=False)
        n2_list.insert(0,min_n2)
        return int(n1+''.join(n2_list))

assert next_bigger(12) == 21
assert next_bigger(513) == 531
assert next_bigger(2017) == 2071
assert next_bigger(414) == 441
assert next_bigger(144) == 414
assert next_bigger(123456789) == 123456798
assert next_bigger(1234567890) == 1234567908
assert next_bigger(9876543210) == -1
assert next_bigger(9999999999) == -1
assert next_bigger(59884848459853) == 59884848483559
import itertools
def next_bigger(n:int) -> int:
    str_int = str(n)
    max_int = int(''.join(map(str,sorted(map(int,str_int),reverse=True))))
    if max_int <= n:
        return -1
    if len(str_int)>=2:
        i = 2
        new_num = -1
        while i <= len(str_int) and new_num <= n :
            num_list = sorted(set(map(int,[''.join(p) for p in itertools.permutations(str_int[-i:])])))
            if len(num_list) >1:
                second_index =  num_list.index(int(str_int[-i:]))
                if second_index + 1<= len(num_list)-1:
                    new_num = int(str_int[:-i]+str(num_list[second_index+1]))
            i += 1
        return new_num