Skip to content

1323. Maximum 69 Number 👍

  • Time: $O(\log\texttt{num})$
  • Space: $O(\log\texttt{num})$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
 public:
  int maximum69Number(int num) {
    string ans = to_string(num);

    for (char& c : ans)
      if (c == '6') {
        c = '9';
        break;
      }

    return stoi(ans);
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
  public int maximum69Number(int num) {
    char[] ans = String.valueOf(num).toCharArray();

    for (int i = 0; i < ans.length; ++i)
      if (ans[i] == '6') {
        ans[i] = '9';
        break;
      }

    return Integer.valueOf(String.valueOf(ans));
  }
}
1
2
3
class Solution:
  def maximum69Number(self, num: int) -> int:
    return int(str(num).replace('6', '9', 1))