【每日一题1228】寻找极值

:woman_mage: 给定一个3位数的数字,请编写一个函数,将它重新排列,并找出其中值最大的形式。

示例:
输入:123,输出:321。

题目难度:简单
题目来源:Rearrange Number to Get its Maximum

def solution(num: int) -> int:
    # your code

assert solution(123) == 321
assert solution(666) == 666
assert solution(109) == 910
def solution(num: int) -> int:
    return int(''.join([i for i in sorted(list(str(num)), reverse=True)]))


assert solution(123) == 321
assert solution(666) == 666
assert solution(109) == 910
1 个赞
def solution(num: int) -> int:
    return int(''.join(sorted([i for i in str(num)], reverse=True)))
assert solution(123) == 321
assert solution(666) == 666
assert solution(109) == 910
return int(''.join([i for i in sorted(list(str(num)), reverse=True)]))
def solution(num: int) -> int:
    return int(''.join([i for i in sorted(str(num),reverse=True)]))
    
def solution(num: int) -> int:
    L = [item for item in list(str(num))]
    L.sort(reverse=True)
    return int(''.join(L))

assert solution(123) == 321
assert solution(666) == 666
assert solution(109) == 910
def solution(num: int) -> int:
    return int("".join(sorted(list(str(num)),reverse=True)))
def solution(num: int) -> int:
    return int(''.join([i for i in sorted(str(num),reverse=True)]))

assert solution(123) == 321
assert solution(666) == 666
assert solution(109) == 910
def solution(num: int) -> int:
   l = "".join(sorted([ i for i in str(num)],reverse=True))
   return int(l)
def solution10(num:int):
    ll=list(str(num))
    ll.sort(reverse=True)
    ss_num=''
    for i in ll:
        ss_num+=i
    return int(ss_num)

assert solution10(123) == 321
assert solution10(666) == 666
assert solution10(109) == 910
def solution(num: int) -> int:
    return int(''.join(sorted(str(num), reverse=True)))


assert solution(123) == 321
assert solution(666) == 666
assert solution(109) == 910

看来大家基本都是一个思路,我来拆分下细节

def solution(num: int) -> int:
    # your code
    # 转换成字符串
    str_num = str(num)
    # 拿到每一个值,变成列表
    num_lis = [int(i) for i in str_num]
    # 将列表进行排序
    num_lis.sort(reverse=True)
    # 把列表转换成字符串
    new_str = [str(j) for j in num_lis]
    # 字符串转换为整数
    final_res = int(''.join(new_str))
    return final_res

assert solution(123) == 321
assert solution(666) == 666
assert solution(109) == 910
def solution(num: int) -> int:
    return int(''.join(sorted([i for i in str(num)],reverse=True)))

assert solution(123) == 321
assert solution(666) == 666
assert solution(109) == 910