Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1ff89f3

Browse files
author
guangsheng.li01
committed
Sloved 0066
1 parent f244be0 commit 1ff89f3

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/a0066_plus_one.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* [0066] plus-one
3+
*/
4+
5+
pub struct Solution {}
6+
7+
// solution impl starts here
8+
9+
impl Solution {
10+
pub fn plus_one(digits: Vec<i32>) -> Vec<i32> {
11+
let mut digits = digits;
12+
for i in (0..digits.len()).rev() {
13+
digits[i] += 1;
14+
digits[i] %= 10;
15+
if digits[i] != 0 {
16+
return digits;
17+
}
18+
}
19+
let mut res = vec![1];
20+
res.append(&mut digits);
21+
res
22+
}
23+
}
24+
25+
// solution impl ends here
26+
27+
// solution tests starts here
28+
29+
#[cfg(test)]
30+
mod tests {
31+
use super::*;
32+
33+
#[test]
34+
fn test_case0() {
35+
assert_eq!(Solution::plus_one(vec![1, 2, 3]), vec![1, 2, 4]);
36+
}
37+
38+
#[test]
39+
fn test_case1() {
40+
assert_eq!(Solution::plus_one(vec![9, 9, 9]), vec![1, 0, 0, 0]);
41+
}
42+
}
43+
44+
// solution tests ends here

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod a0026_remove_duplicates_from_sorted_array;
1111
mod a0027_remove_element;
1212
mod a0035_search_insert_position;
1313
mod a0053_maximum_subarray;
14+
mod a0066_plus_one;
1415
mod a0145_binary_tree_postorder_traversal;
1516
mod a0172_factorial_trailing_zeroes;
1617
mod a0400_nth_digit;

0 commit comments

Comments
 (0)