-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1945: Sum of Digits of String After Convert
- Loading branch information
Showing
3 changed files
with
68 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
49 changes: 49 additions & 0 deletions
49
src/problem_1945_sum_of_digits_of_string_after_convert/iterative.rs
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,49 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn get_lucky(s: String, k: i32) -> i32 { | ||
let k = k as u32; | ||
let mut value = 0; | ||
|
||
// Convert and transform. | ||
|
||
for c in s.into_bytes() { | ||
let c = c - (b'a' - 1); | ||
|
||
value += u16::from(c / 10 + c % 10); | ||
} | ||
|
||
// Transform. | ||
|
||
for _ in 1..k { | ||
let mut temp = 0; | ||
|
||
while value != 0 { | ||
temp += value % 10; | ||
value /= 10; | ||
} | ||
|
||
value = temp; | ||
} | ||
|
||
i32::from(value) | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn get_lucky(s: String, k: i32) -> i32 { | ||
Self::get_lucky(s, k) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/problem_1945_sum_of_digits_of_string_after_convert/mod.rs
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 get_lucky(s: String, k: i32) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [(("iiii", 1), 36), (("leetcode", 2), 6), (("zbax", 2), 8)]; | ||
|
||
for ((s, k), expected) in test_cases { | ||
assert_eq!(S::get_lucky(s.to_string(), k), expected); | ||
} | ||
} | ||
} |