【每日一题20220512】最小代价

:mage:‍ 给定一个整数n,通过移除其中某一位数字,找出操作之后遗留的最大数字。

【示例】
输入:152
输出:52
解释:如果移除1,得到52;如果移除5,得到12;如果移除2,得到15。因此最大方案是移除1得到52。

题目难度:中等
题目来源:Simple Fun #79: Delete a Digit

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

assert solution(152) == 52
assert solution(1001) == 101
assert solution(10) == 1
def solution(n: int)-> int:
    nums = str(n)
    return max([int(nums[:i] + nums[i+1:len(nums)+1]) for i in range(len(nums))])


assert solution(152) == 52
assert solution(1001) == 101
assert solution(10) == 1
def solution(n: int)-> int:
    # your code here
    return max([int(str(n).replace(item, "",1)) for item in str(n)])

assert solution(152) == 52
assert solution(1001) == 101
assert solution(10) == 1
1 个赞

思路跟楼上一样。

def solution(n: int)-> int:
    # your code here
    return max([int((str(n)).replace(i, '', 1)) for i in str(n)])


assert solution(152) == 52
assert solution(1001) == 101
assert solution(10) == 1
def solution(n: int) -> int:
    return max([int(str(n).replace(i, '', 1)) for i in str(n)])


assert solution(152) == 52
assert solution(1001) == 101
assert solution(10) == 1

[quote=“hogwarts, post:1, topic:20694, full:true”]
:mage:‍ 给定一个整数n,通过移除其中某一位数字,找出操作之后遗留的最大数字。

【示例】
输入:152
输出:52
解释:如果移除1,得到52;如果移除5,得到12;如果移除2,得到15。因此最大方案是移除1得到52。

题目难度:中等
题目来源:[Simple Fun #79: Delete a Digit](https://www.用replace明显不可以的

用str(n).replace(i,"",1)的方式应该不行哦,比如assert soluion(1001)去除末尾的1的时候,会去除成开头的1了。

def solution(n: int)-> int:
    str1 = str(n)
    a=[]
    for i in range(0,len(str1)):
        str2=str1[:i] + str1[i+1:len(str1)]
        a.append(int(str2))
    return max(a)

assert solution(152) == 52
assert solution(1001) == 101
assert solution(10) == 1
def solution(n: int)-> int:
    return max([int(''.join(i)) for i in itertools.combinations(str(n),len(str(n))-1)])
def solution(n: int)-> int:
    return max([int(str(n).replace(i,'',1)) for i in str(n)])
def solution(n: int)-> int:
    str1 = str(n)
    for i in range(len(str1)-1):
        if int(str1[i]) < int(str1[i+1]):
            return int(str1[:i]+str1[i+1:])
    return int(str1[:-1])
    #str1 = str(n)
    #return max(int(str1[:i]+str1[i+1:]) for i in range(len(str1)))
def solution(n: int)-> int:
    n_new=[int(str(n)[0:i]+str(n)[i+1:len(str(n))]) for i in range(len(str(n)))]
    n_new.sort(reverse=True)
    return n_new[0]

assert solution(152) == 52
assert solution(1001) == 101
assert solution(10) == 1
from functools import reduce
def solution(n: int)-> int:
	list_n=list(map(str,str(n)))##将一个整数转为字符串列表  ['1', '2', '3', '4']
	result=[]
	for i in range(len(list_n)):
		tmp2=list_n[0:i]+list_n[i+1:]
		a=int(reduce(lambda x,y:x+y,tmp2))
		result.append(a)

	return max(result)

assert solution(152) == 52
assert solution(1001) == 101
assert solution(10) == 1
def solution(n: int)-> int:
    lresult=[]
    for i in range(len(str(n))):
        new_str = str(n).replace(str(n)[i], "", 1)
        lresult.append(int(new_str))
    return max(lresult)
def solution(n: int) -> int:
    str_n = str(n)
    res = []
    for i in range(0, len(str_n)):
        res.append(str_n.replace(str_n[i], "", 1))
    return int(max(res))


assert solution(152) == 52
assert solution(1001) == 101
assert solution(10) == 1

def solution(n: int)-> int:
    return max([int(str(n)[:i]+str(n)[i+1:]) for i in range(len(str(n)))])

assert solution(152) == 52
assert solution(1001) == 101
assert solution(10) == 1