LeetCode Entry
1323. Maximum 69 Number
Bigger number by replacing 6 to 9
1323. Maximum 69 Number easy
blog post
substack
youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1082
Problem TLDR
Bigger number by replacing 6 to 9 #easy
Intuition
The simpliest way is to convert to char array, replace first, then stop.
Approach
- let’s explore other unusual ways
Complexity
-
Time complexity: \(O(1)\)
-
Space complexity: \(O(1)\)
Code
// 7ms
fun maximum69Number (n: Int) =
"$n".replaceFirst('6', '9').toInt()
// 12ms
fun maximum69Number (n: Int) =
n + 3 * (setOf(1000, 100, 10, 1).firstOrNull { n/it%9>0 }?:0)
// 0ms
pub fn maximum69_number(n: i32) -> i32 {
n + 3 * [1000, 100, 10, 1].iter().find(|&&p| n/p%10==6).unwrap_or(&0)
}
// 0ms
int maximum69Number (int n) {
for (int p = 1000; p; p /= 10)
if (n/p%10 == 6) return n + 3*p;
return n;
}
// 0ms
maximum69Number = lambda _, n: int(str(n).replace('6','9',1))