20240730 Finished
This commit is contained in:
parent
17ee25130e
commit
bd6382ab87
|
@ -189,3 +189,4 @@ mod p191_number_of_1_bits;
|
|||
mod p136_single_number;
|
||||
mod p137_single_number_ii;
|
||||
mod p201_bitwise_and_of_numbers_range;
|
||||
mod p66_plus_one;
|
38
src/problem/p66_plus_one.rs
Normal file
38
src/problem/p66_plus_one.rs
Normal file
|
@ -0,0 +1,38 @@
|
|||
/**
|
||||
* [66] Plus One
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn plus_one(digits: Vec<i32>) -> Vec<i32> {
|
||||
let mut digits = digits;
|
||||
let mut overflow = 1;
|
||||
|
||||
for i in (0..digits.len()).rev() {
|
||||
let bit = digits[i] + overflow;
|
||||
digits[i] = bit % 10;
|
||||
overflow = bit / 10;
|
||||
}
|
||||
|
||||
if overflow > 0 {
|
||||
digits.insert(0, overflow);
|
||||
}
|
||||
|
||||
digits
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_66() {
|
||||
assert_eq!(vec![1, 0], Solution::plus_one(vec![9]));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user