/
iterative.rs
37 lines (29 loc) · 888 Bytes
/
iterative.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
pub struct Solution;
// ------------------------------------------------------ snip ------------------------------------------------------ //
impl Solution {
pub fn max_value(n: String, x: i32) -> String {
let mut n = n;
let x = char::from(b'0' + (x as u8));
let index = if n.starts_with('-') {
n.find(|c| c > x) // We know that '0' > '-'.
} else {
n.find(|c| c < x)
}
.unwrap_or(n.len());
n.insert(index, x);
n
}
}
// ------------------------------------------------------ snip ------------------------------------------------------ //
impl super::Solution for Solution {
fn max_value(n: String, x: i32) -> String {
Self::max_value(n, x)
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}