LeetCode知识点总结 - 2566

文章讲述了在给定一个整数的情况下,分析一个可能的数字重映射策略,找出仅改变一个数字时,最大值与最小值之间的差。解决方案通过替换第一个非9的数字为9,将第一个数字改为0来计算。
摘要由CSDN通过智能技术生成

LeetCode 2566. Maximum Difference by Remapping a Digit

考点难度
GreedyEasy
题目

You are given an integer num. You know that Bob will sneakily remap one of the 10 possible digits (0 to 9) to another digit.

Return the difference between the maximum and minimum values Bob can make by remapping exactly one digit in num.

思路

最小:第一位改成0
最大:第一个非9位改成9

答案
class Solution(object):
    def minMaxDifference(self, num):
        num = str(num)
        i = 0
        while num[i] == "9" and i < len(num)-1:
            i += 1

        return int(num.replace(num[i], "9")) - int(num.replace(num[0], "0")) 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值