Skip to content

Commit 9eb8e4d

Browse files
authored
Create 1323_Maximum_69_Number.cpp
1 parent bbb12c7 commit 9eb8e4d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

1323_Maximum_69_Number.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int maximum69Number (int num) {
4+
auto str = std::to_string(num);
5+
auto switchChar = [](char c) -> char {
6+
if (c == '9') {
7+
c = '6';
8+
} else if (c == '6') {
9+
c = '9';
10+
}
11+
return c;
12+
};
13+
for (size_t i = 0; i < str.size(); ++i) {
14+
str[i] = switchChar(str[i]);
15+
num = std::max(num, std::stoi(str));
16+
str[i] = switchChar(str[i]);
17+
}
18+
return num;
19+
}
20+
};

0 commit comments

Comments
 (0)