Skip to content

Commit a15ec89

Browse files
committed
feat: add more
1 parent 36ec9fe commit a15ec89

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ members = [
99
"greedy",
1010
"trie",
1111
"stack",
12+
"Math",
1213
"priority_queue"
1314
]

Math/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "leetcode_math"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
path = "lib.rs"
8+
doctest = false
9+
10+
[dependencies]

Math/_0066_plus_one.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// https://leetcode.com/problems/plus-one
2+
//
3+
// You are given a **large integer** represented as an integer array `digits`, where each `digits[i]` is the `i<sup>th</sup>` digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading `0`'s.
4+
//
5+
// Increment the large integer by one and return _the resulting array of digits_.
6+
//
7+
// **Example 1:**
8+
//
9+
// ```
10+
// **Input:** digits = [1,2,3]
11+
// **Output:** [1,2,4]
12+
// **Explanation:** The array represents the integer 123.
13+
// Incrementing by one gives 123 + 1 = 124.
14+
// Thus, the result should be [1,2,4].
15+
// ```
16+
//
17+
// **Example 2:**
18+
//
19+
// ```
20+
// **Input:** digits = [4,3,2,1]
21+
// **Output:** [4,3,2,2]
22+
// **Explanation:** The array represents the integer 4321.
23+
// Incrementing by one gives 4321 + 1 = 4322.
24+
// Thus, the result should be [4,3,2,2].
25+
// ```
26+
//
27+
// **Example 3:**
28+
//
29+
// ```
30+
// **Input:** digits = [9]
31+
// **Output:** [1,0]
32+
// **Explanation:** The array represents the integer 9.
33+
// Incrementing by one gives 9 + 1 = 10.
34+
// Thus, the result should be [1,0].
35+
// ```
36+
//
37+
// **Constraints:**
38+
//
39+
// * `1 <= digits.length <= 100`
40+
// * `0 <= digits[i] <= 9`
41+
// * `digits` does not contain any leading `0`'s.
42+
43+
pub fn plus_one(digits: Vec<i32>) -> Vec<i32> {
44+
let mut digits_clone = digits.clone();
45+
for x in digits_clone.iter_mut().rev() {
46+
if *x < 9 {
47+
*x += 1;
48+
return digits_clone;
49+
} else {
50+
*x = 0;
51+
}
52+
}
53+
digits_clone.insert(0, 1);
54+
return digits_clone;
55+
}
56+
57+
#[test]
58+
pub fn t1() {
59+
assert_eq!(plus_one(vec![1, 2, 3]), vec![1, 2, 4]);
60+
assert_eq!(plus_one(vec![9, 9]), vec![1, 0, 0]);
61+
}

Math/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#![allow(dead_code)]
2+
3+
mod _0066_plus_one;

0 commit comments

Comments
 (0)