[LeetCode] Maximum 69 Number

1323. Maximum 69 Number

You are 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).

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int maximum69Number (int num) {
string s = to_string(num);
for(int i = 0; i < s.length(); i++) {
if(s[i] == '6') {
s[i] = '9';
return stoi(s);
}
}
return num;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/11/07/PS/LeetCode/maximum-69-number/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.