-
-
Notifications
You must be signed in to change notification settings - Fork 734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[LeetCode] 1323. Maximum 69 Number #1323
Comments
grandyang
changed the title
[LeetCode] 1323. Missing Problem
[LeetCode] 1323. Maximum 69 Number
Sep 14, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You are given a positive integer
num
consisting only of digits6
and9
.Return the maximum number you can get by changing at most one digit (
6
* becomes9
, and9
becomes6
)*.Example 1:
Example 2:
Example 3:
Constraints:
1 <= num <= 104
num
consists of only6
and9
digits.这道题说是给了一个只含有6和9的正整数,现在说是可以将6变成9,或者将9变成6,最多可以变换一次,让返回可以得到的最大的数字。既然要得到最大的数字,那么肯定是把6变成9得到的数字最大,而且尽量去变高位上的数字。所以方法就是从高位开始遍历,若遇到6,则变为9即可,变换了之后直接 break 掉就行了,因为最多只能变换一次。为了能方便从高位开始遍历,可以将给定的数字转为字符串开始遍历,之后变换完了之后再转为整型数就可以了,参见代码如下:
Github 同步地址:
#1231
参考资料:
https://leetcode.com/problems/maximum-69-number/
https://leetcode.com/problems/maximum-69-number/solutions/2640037/maximum-69-number/
https://leetcode.com/problems/maximum-69-number/solutions/484292/java-python-replace-once/
LeetCode All in One 题目讲解汇总(持续更新中...)
(欢迎加入博主的知识星球,博主将及时答疑解惑,并分享刷题经验与总结,快快加入吧~)
喜欢请点赞,疼爱请打赏❤️~.~
微信打赏
|
Venmo 打赏
---|---
The text was updated successfully, but these errors were encountered: