app开发者平台在数字化时代的重要性与发展趋势解析
816
2022-08-25
LeetCode-1323. 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 = 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.
题解:
class Solution {public: int maximum69Number (int num) { string s = to_string(num); int i = 0; while (i < s.length()) { if (s[i] == '6') { s[i] = '9'; return stoi(s); } i++; } return num; }};
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~