requirements
You are given a positive integer num consisting only of the numbers 6 and 9.
You can only flip one digit at most to change a 6 to a 9, or a 9 to a 6.
Please return the largest number you can get.
Example 1:
Input: num = 9669 Output: 9969 Description: Change the first digit to get 6669. Change the second digit to get 9969. Change the third digit to get 9699. Change the fourth digit to get 9666. The largest number is 9969.Copy the code
Example 2:
Input: num = 9996 Output: 9999 Explanation: Change the last digit from 6 to 9, resulting in 9999 being the largest number.Copy the code
Example 3:
Input: num = 9999 Output: 9999Copy the code
The core code
class Solution:
def maximum69Number (self, num: int) - >int:
return int(str(num).replace("6"."9".1))
Copy the code
We just need to find the leftmost number 6 to replace it.