LeetCode 2566. Maximum Difference by Remapping a Digit
考点 | 难度 |
---|---|
Greedy | Easy |
题目
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"))