LeetCode 1323. 6 和 9 组成的最大数字

简介: LeetCode 1323. 6 和 9 组成的最大数字

LeetCode 1323. 6 和 9 组成的最大数字


Table of Contents

中文版:

英文版:

My answer:

解题报告:

中文版:

给你一个仅由数字 6 和 9 组成的正整数 num。

你最多只能翻转一位数字,将 6 变成 9,或者把 9 变成 6 。

请返回你可以得到的最大数字。

 

示例 1:

输入:num = 9669

输出:9969

解释:

改变第一位数字可以得到 6669 。

改变第二位数字可以得到 9969 。

改变第三位数字可以得到 9699 。

改变第四位数字可以得到 9666 。

其中最大的数字是 9969 。

示例 2:

输入:num = 9996

输出:9999

解释:将最后一位从 6 变到 9,其结果 9999 是最大的数。

示例 3:

输入:num = 9999

输出:9999

解释:无需改变就已经是最大的数字了。

 

提示:

1 <= num <= 10^4

num 每一位上的数字都是 6 或者 9 。

 

英文版:

5315. Maximum 69 Number

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.

Example 2:

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

Example 3:

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

Constraints:

1 <= num <= 10^4

num's digits are 6 or 9.

My answer:

class Solution:
         #替换字符串string中指定位置p的字符为c
    def subs(self,string,p,c):
        new = []
        for s in string:
            new.append(s)
        new[p] = c
        return ''.join(new)
    def maximum69Number (self, num: int) -> int:        
        nums = str(num)
        ans = nums
        for i in range(len(nums)):
            if nums[i] == '9':
                continue
            else:
                newvalue = self.subs(nums,i,'9')
                print(newvalue)
                if newvalue > ans:
                    ans = newvalue
        return int(ans)

解题报告:

1、其实我自己的算法是比较复杂的,因为要把所有的 6 替换成 9,再逐一与原来的数比较,如果比原来的数大,则复制给 ans,主要是打擂台思想。

2、以下是大神算法:https://leetcode-cn.com/circle/article/6Su4LC/

其中关于本题的代码:

其实只要把第一个 6 改成 9 就一定是最大的了。

class Solution:
    def maximum69Number (self, num: int) -> int:
        x = list(str(num))
        for i in range(len(x)):
            if x[i] == '6':
                x[i] = '9'
                break
        return int(''.join(x))
作者:Amir
链接:https://leetcode-cn.com/circle/article/6Su4LC/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

文中又提到另一个大神的算法,在此膜拜:https://leetcode-cn.com/u/thebestone/

用 Python 自带的 replace() 方法,参数1 表示只要改第 1 个 6 为 9 即可。

class Solution:
    def maximum69Number (self, num: int) -> int:
        return int(str(num).replace('6','9',1))
作者:Amir
链接:https://leetcode-cn.com/circle/article/6Su4LC/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关文章
|
存储 前端开发 算法
LeetCode只出现一次的数字使用JavaScript解题|前端学算法
LeetCode只出现一次的数字使用JavaScript解题|前端学算法
110 0
|
算法 PHP
力扣(LeetCode)算法题解:1365. 有多少小于当前数字的数字
力扣(LeetCode)算法题解:1365. 有多少小于当前数字的数字
109 0
|
算法 PHP
力扣(LeetCode)算法题解:1323. 6 和 9 组成的最大数字
力扣(LeetCode)算法题解:1323. 6 和 9 组成的最大数字
106 0
|
算法 PHP
力扣(LeetCode)算法题解:1295. 统计位数为偶数的数字
力扣(LeetCode)算法题解:1295. 统计位数为偶数的数字
87 0
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode 136. 只出现一次的数字 Single Number
LeetCode 136. 只出现一次的数字 Single Number
LeetCode 1342. 将数字变成 0 的操作次数 Number of Steps to Reduce a Number to Zero
LeetCode 1342. 将数字变成 0 的操作次数 Number of Steps to Reduce a Number to Zero
LeetCode 136. 只出现一次的数字
LeetCode 136. 只出现一次的数字
LeetCode 1296. 划分数组为连续数字的集合
LeetCode 1296. 划分数组为连续数字的集合
LeetCode 1295. 统计位数为偶数的数字
LeetCode 1295. 统计位数为偶数的数字

热门文章

最新文章