describe

Given a positive integer num consisting only of digits 6 and 9.

Return the maximum number you can get by changing at most one digit (6 becomes 9, and 9 becomes 6).

Example 1:

Input: num = 9669 Output: 9969 Explanation: Changing the first digit results in 6669. Changing the second digit results in 9969. Changing the third digit results in  9699. Changing the fourth digit results in 9666. The maximum number is 9969.Copy the code

Example 2:

Input: num = 9996
Output: 9999
Explanation: Changing the last digit 6 to 9 results in the maximum number.
Copy the code

Example 3:

Input: num = 9999
Output: 9999
Explanation: It is better not to apply any change.
Copy the code

Note:

1 <= num <= 10^4
num's digits are 6 or 9.
Copy the code

parsing

What is the largest number that can be obtained by changing 6 into 9 or 9 into 6? Perform the “69” operation once for each character, and then compare the size with num. The larger number is assigned to num. At the end of the traversal, the largest number is obtained.

answer

class Solution(object):
    def maximum69Number (self, num):
        """
        :type num: int
        :rtype: int
        """
        s = str(num)
        for i in range(len(s)):
            if s[i]=='9':
                num = max(int(s[:i]+'6'+s[i+1:]), num)
            else:
                num = max(int(s[:i]+'9'+s[i+1:]), num)
        return num



        	      
		
Copy the code

The results

Given in the Python online submissions. Memory Usage: 10000 ms. Submissions in the Python online list for Maximum 69 numbers.Copy the code

parsing

If you know what they’re saying, the biggest number is, in fact, the biggest number you can get by changing the first 6 from front to back to 9.

answer

class Solution(object):
    def maximum69Number (self, num):
        """
        :type num: int
        :rtype: int
        """
        return str(num).replace('6','9',1)
Copy the code

The results

Given the given list in the Python online submission. Memory Usage: 10 ms Given in the Python online submissions for Maximum 69 numbers.Copy the code

Original link: leetcode.com/problems/ma…

Your support is my biggest motivation