summaryrefslogtreecommitdiff
path: root/plus-one/src/main.rs
blob: 7fdef54980fa2b11471f8bf21286aa90e208906a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
fn main() {
    println!("Hello, world!");
}

struct Solution {}
impl Solution {
    pub fn plus_one(digits: Vec<i32>) -> Vec<i32> {
        let mut result = Vec::with_capacity(digits.len()+1);
        let mut carry = 1;
        for i in digits.iter().rev() {
            let sum = *i + carry;
            result.push(sum % 10);
            carry = sum / 10;
        }
        if carry > 0 {
            result.push(carry);
        }
        result.reverse();
        result
    }
}