【每日一题0930】最近一个大数

:woman_mage:给定一个正整数,请编写一个函数,将它的所有数字重新组合,找出刚好比它大一点儿的那个数字。

实例:
输入:12,返回:21
输入:513,返回:531
输入:144,返回:414

题目难度:中等
题目来源:codewars

def next_bigger(n: int) -> int:
    pass

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
1 个赞
def next_bigger(n: int) -> int:
    n_list = list(str(n))
    from itertools import permutations
    n_lists = []
    for i in permutations(n_list,len(n_list)):
        n_lists.append(int(''.join(list(i))))
    result = list(set(n_lists))
    result.sort()
    return result[result.index(n)+1]
public Integer nextBig(Integer integer){
        String[] split = integer.toString().split("");
        // 判断低位数是否大于高位数,是则交换并结束循环
        for (int i = split.length-1; i >= 1; i--) {
            if (Integer.parseInt(split[i])>Integer.parseInt(split[i-1])){
                String temp = split[i];
                split[i] = split[i-1];
                split[i-1] = temp;
                break;
            }
        }
        // 重新生成大数
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < split.length; i++) {
            stringBuilder.append(split[i]);
        }
        return Integer.valueOf(stringBuilder.toString());
    }
    @Test
    public void test1(){
        assert nextBig(12).equals(21);
        assert nextBig(513).equals(531);
        assert nextBig(2017).equals(2071);
        assert nextBig(414).equals(441);
        assert nextBig(144).equals(414);
        assert nextBig(11).equals(11);
    }
def next_bigger(n: int) -> int:
    num_list = [int(i) for i in str(n)]
    recombination_list = []

    def perm(n, begin, end):

        if begin >= end:
            num_str = ''
            for one_num in n:
                num_str += str(one_num)
            num = int(num_str)
            recombination_list.append(num)
        else:
            i = begin
            for num in range(begin, end):
                n[num], n[i] = n[i], n[num]
                perm(n, begin + 1, end)
                n[num], n[i] = n[i], n[num]

    perm(num_list, 0, len(num_list))
    recombination_list.sort()
    next_bigger_num = 0
    for i in recombination_list:
        if i > n:
            next_bigger_num = i
            break
    return next_bigger_num


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
def next_bigger(n: int) -> int:
    nums = list(str(n))
    nums.reverse()
    for i in range(len(nums)):
        if nums[i] > nums[i+1]:
            temp = nums[i+1]
            nums[i + 1] = nums[i]
            nums[i] = temp
            break
    nums.reverse()
    return int(''.join(nums))

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
from itertools import permutations


def next_bigger(n: int) -> int:
    n_list = [i for i in str(n)]
    data = []
    # permutations返回的是列表中元素的全排列
    # 例如513 全排列:[513, 531, 153, 135, 351, 315]
    for i in permutations(n_list, len(n_list)):
        data.append(int(''.join(list(i))))
    result = list(set(data))
    result.sort()
    if result.index(n) == len(result)-1:
        return None
    else:
        return result[result.index(n) + 1]


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
def next_bigger(n: int) -> int:
    list_n = list(str(n))
    loop = len(list_n)-1
    while loop>0:
        j = loop-1
        while j>-1:
            if int(list_n[loop]) > int(list_n[j]):
                temp = list_n[loop]
                list_n[loop] = list_n[j]
                list_n[j] = temp
                start = list_n[:j+1]
                end = list_n[j+1:]
                end.sort()
                return int("".join(start+end))
            j -=1
        loop -= 1
    return n
        
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(11) == 11
assert next_bigger(112) == 121
assert next_bigger(121) == 211
assert next_bigger(1221) == 2112
assert next_bigger(1122) == 1212
assert next_bigger(3655) == 5356
assert next_bigger(326543) == 332456

import itertools
def aa(num: int):
 x=eval(‘9’*len(str(num)))
 for i in itertools.permutations(str(num), len(str(num))):
  j=eval(’’.join(i))
  x=j if num<j<x else x
 return x if x != eval(‘9’*len(str(num))) else None

def bb(num: int):
 s=
 for i in itertools.permutations(str(num), len(str(num))):
  s.append(eval(’’.join(i)))
  s=sorted(list(set(s)))
 return s[s.index(num)+1] if s.index(num) < len(s)-1 else None

def next_bigger(n) -> int:
    n = int(n)
    new_num = []
    for i in range(len(str(n))):
        a = n % 10
        n = n // 10
        if(len(new_num)>=1):
            c = new_num[-1]
            if a < c:
                d = min(filter(lambda x:x>a and x <=c,new_num))
                new_num[new_num.index(d)] = a
                a = d
                new_num.sort()
                return int(str(n) + str(a) + "".join([str(x) for x in new_num]))

        new_num.append(a)
    else:
        return -1


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
from itertools import permutations
def next_bigger(n):
    length = len(str(n))
    if length > 1:
        s = [i for i in str(n)]
        datas = list(permutations(s,length))
        results = list(set([int(''.join(x)) for x in datas]))
        results.sort()
        if results[-1] == n:
            return -1
        return results[results.index(n)+1]
    else:
        return -1
def next_bigger(n: int) -> int:
    list_int = list(str(n))
    for i in range(-1, -len(list_int), -1):
        if int(list_int[i]) > int(list_int[i-1]):
            temp = list_int[i-1]
            list_int[i-1] = list_int[i]
            list_int[i] = temp
            break
    return int("".join(list_int))

def next_bigger(n: int)->int :
    new_list=[]
    for i in itertools.permutations(str(n),len(str(n))):
        if int(''.join(i)) not in new_list:new_list.append(int(''.join(i)))
    new_list.sort()
    return new_list[new_list.index(n)+1]
from itertools import permutations
def next_bigger(n: int) -> int:
    premutation_values=[]
    for i in permutations(str(n)):
        if int(''.join(i)) not in premutation_values:
            premutation_values.append(int(''.join(i)))
    premutation_values.sort()
    return premutation_values[premutation_values.index(n)+1]

assert next_bigger(3542)==4235
from itertools import permutations

def next_bigger(n: int) -> int:
    s_num = sorted(set(int("".join(num)) for num in permutations(str(n))))
    print(s_num)
    if n == s_num[-1]:   #有可能没有比n更大的数
        return -1

    return s_num[s_num.index(n) + 1]


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
def next_bigger(n: int) -> int:
    s=[]
    from itertools import permutations
    n_list=[k for k in str(n)]
    for i in permutations(n_list,len(n_list)):
        s.append(int(''.join(i)))
    s.sort(reverse=True)
    for j in range(len(s)):
        if s[j]==n:
            return s[j-1]

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