20240926 finished.
This commit is contained in:
parent
1f20dc3d7f
commit
8830b0dbcd
|
@ -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;
|
|
@ -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]));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user