[leetcode] 1323. Maximum 69 Number

网友投稿 274 2022-08-27

[leetcode] 1323. Maximum 69 Number

Description

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 = 9669Output: 9969Explanation: 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 = 9996Output: 9999Explanation: Changing the last digit 6 to 9 results in the maximum number.

Example 3:

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

Constraints:

1 <= num <= 10^4num’s digits are 6 or 9.

分析

题目的意思是:给定各个位只包含6或者9的数,至多改变一个位置的数字,使得数最大。思路也很直接了,改的数肯定是6变成9,然后越靠左数越大,所以从左往右便利找到第一个6,然后变成9就行了。

代码

class Solution: def maximum69Number (self, num: int) -> int: num_str=str(num) res='' n=len(num_str) i=0 while(i

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:[leetcode] 1492. The kth Factor of n
下一篇:谷爱凌夺冠熊猫帽子抢断货 品牌营销搭上冬奥“流量快车”!
相关文章

 发表评论

暂时没有评论,来抢沙发吧~