20240926 finished.

This commit is contained in:
jackfiled 2024-09-26 13:26:35 +08:00
parent 1f20dc3d7f
commit 8830b0dbcd
2 changed files with 44 additions and 1 deletions

View File

@ -245,4 +245,5 @@ mod p2376_count_special_integers;
mod p2374_node_with_highest_edge_score;
mod p997_find_the_town_judge;
mod p2207_maximize_number_of_subsequences_in_a_string;
mod p2306_naming_a_company;
mod p2306_naming_a_company;
mod p2535_difference_between_element_sum_and_digit_sum_of_an_array;

View File

@ -0,0 +1,42 @@
/**
* [2535] Difference Between Element Sum and Digit Sum of an Array
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn difference_of_sum(nums: Vec<i32>) -> i32 {
let mut result = 0;
for num in nums {
let mut num = num;
let mut base = 1;
while num > 0 {
let digit = num % 10;
result += digit * base - digit;
base *= 10;
num = num / 10;
}
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2535() {
assert_eq!(9, Solution::difference_of_sum(vec![1, 15, 6, 3]));
assert_eq!(0, Solution::difference_of_sum(vec![1, 2, 3, 4]));
}
}