Skip to content

Commit 2da7e15

Browse files
author
eamondang
committed
leetcode practice easy exercise
0 parents  commit 2da7e15

8 files changed

+163
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
/Cargo.lock

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "leetcode-solving"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

src/easy/e1342_number_of_steps.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#![allow(dead_code)]
2+
struct Solution;
3+
4+
impl Solution {
5+
pub fn number_of_steps(mut n: i32) -> i32 {
6+
let mut steps = 0;
7+
while n > 0 {
8+
if n % 2 == 0 {
9+
steps += 1;
10+
n /= 2;
11+
} else {
12+
steps += 1;
13+
n -= 1;
14+
}
15+
}
16+
steps
17+
}
18+
}
19+
20+
#[cfg(test)]
21+
mod tests {
22+
use super::Solution;
23+
24+
#[test]
25+
fn steps_of_14() {
26+
assert_eq!(Solution::number_of_steps(14), 6);
27+
}
28+
29+
#[test]
30+
fn steps_of_8() {
31+
assert_eq!(Solution::number_of_steps(8), 4);
32+
}
33+
34+
#[test]
35+
fn steps_of_100() {
36+
assert_eq!(Solution::number_of_steps(100), 9);
37+
}
38+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#![allow(dead_code)]
2+
struct Solution;
3+
4+
impl Solution {
5+
pub fn running_sum(nums: Vec<i32>) -> Vec<i32> {
6+
nums.iter()
7+
.scan(0, |prev, curr| {
8+
*prev += curr;
9+
Some(*prev)
10+
})
11+
.collect()
12+
}
13+
}
14+
15+
#[cfg(test)]
16+
mod tests {
17+
use super::*;
18+
19+
#[test]
20+
fn case_1() {
21+
let nums = vec![1, 2, 3, 4];
22+
assert_eq!(Solution::running_sum(nums), vec![1, 3, 6, 10]);
23+
}
24+
25+
#[test]
26+
fn case_2() {
27+
let nums = vec![1, 1, 1, 1, 1];
28+
assert_eq!(Solution::running_sum(nums), vec![1, 2, 3, 4, 5]);
29+
}
30+
31+
#[test]
32+
fn case_3() {
33+
let nums = vec![3, 1, 2, 10, 1];
34+
assert_eq!(Solution::running_sum(nums), vec![3, 4, 6, 16, 17]);
35+
}
36+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#![allow(dead_code)]
2+
struct Solution;
3+
4+
impl Solution {
5+
pub fn maximun_wealth(accounts: Vec<Vec<i32>>) -> i32 {
6+
accounts.iter().map(|i| i.iter().sum()).max().unwrap()
7+
}
8+
}
9+
10+
#[cfg(test)]
11+
mod tests {
12+
use super::Solution;
13+
14+
#[test]
15+
fn array_2d() {
16+
let accounts = vec![vec![1, 2, 3], vec![3, 2, 1]];
17+
assert_eq!(Solution::maximun_wealth(accounts), 6);
18+
}
19+
20+
#[test]
21+
fn array_3x2() {
22+
let accounts = vec![vec![1, 5], vec![7, 3], vec![3, 5]];
23+
assert_eq!(Solution::maximun_wealth(accounts), 10);
24+
}
25+
26+
#[test]
27+
fn array_3x3() {
28+
let accounts = vec![vec![2, 8, 7], vec![7, 1, 3], vec![1, 9, 5]];
29+
assert_eq!(Solution::maximun_wealth(accounts), 17);
30+
}
31+
}

src/easy/e412_fizz_buzz.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#![allow(dead_code)]
2+
struct Solution;
3+
4+
impl Solution {
5+
pub fn fizz_buzz(n: i32) -> Vec<String> {
6+
let mut result: Vec<String> = vec![];
7+
for i in 1..=n {
8+
match (i % 3, i % 5) {
9+
(0, 0) => result.push("FizzBuzz".into()),
10+
(0, _) => result.push("Fizz".into()),
11+
(_, 0) => result.push("Buzz".into()),
12+
_ => result.push(i.to_string()),
13+
};
14+
}
15+
result
16+
}
17+
}
18+
19+
#[cfg(test)]
20+
mod tests {
21+
use super::Solution;
22+
23+
#[test]
24+
fn elements_3() {
25+
assert_eq!(Solution::fizz_buzz(3), vec!["1", "2", "Fizz"]);
26+
}
27+
28+
#[test]
29+
fn element_5() {
30+
assert_eq!(Solution::fizz_buzz(5), vec!["1", "2", "Fizz", "4", "Buzz"]);
31+
}
32+
33+
#[test]
34+
fn element_15() {
35+
assert_eq!(
36+
Solution::fizz_buzz(15),
37+
vec![
38+
"1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz",
39+
"13", "14", "FizzBuzz"
40+
]
41+
);
42+
}
43+
}

src/easy/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mod e1342_number_of_steps;
2+
mod e1480_running_sum_of_1d_array;
3+
mod e1672_richest_customer_wealth;
4+
mod e412_fizz_buzz;

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod easy;

0 commit comments

Comments
 (0)