-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
pub struct Solution; | ||
|
||
impl Solution { | ||
pub fn maximum69_number(num: i32) -> i32 { | ||
let mut result = 0; | ||
let mut div = if num < 100 { | ||
10 | ||
} else if num < 1000 { | ||
100 | ||
} else { | ||
1000 | ||
}; | ||
let mut num = num; | ||
let mut ok = false; | ||
while num != 0 { | ||
let mut digit = num / div; | ||
num %= div; | ||
div /= 10; | ||
if digit == 6 && !ok { | ||
ok = true; | ||
digit = 9; | ||
} | ||
result = result * 10 + digit; | ||
} | ||
result | ||
} | ||
} | ||
|
||
impl super::Solution for Solution { | ||
fn maximum69_number(num: i32) -> i32 { | ||
Self::maximum69_number(num) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn maximum69_number(num: i32) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [(9669, 9969), (9996, 9999), (9999, 9999)]; | ||
|
||
for (num, expected) in test_cases { | ||
assert_eq!(S::maximum69_number(num), expected); | ||
} | ||
} | ||
} |