Skip to content

Commit 7e8e4f5

Browse files
committed
Ran Rust Format
1 parent 8f4294f commit 7e8e4f5

38 files changed

+299
-310
lines changed

src/average_salary_excluding_the_minimum_and_maximum_salary.rs

-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::cmp::min;
99
struct Solution;
1010

1111
impl Solution {
12-
1312
pub fn average(salary: Vec<i32>) -> f64 {
1413
let n = salary.len();
1514
let mut min_salary = i32::max_value();
@@ -26,7 +25,6 @@ impl Solution {
2625
sum -= max_salary;
2726
sum as f64 / (n - 2) as f64
2827
}
29-
3028
}
3129

3230
#[cfg(test)]
@@ -46,5 +44,4 @@ mod tests {
4644
let result = Solution::average(salary);
4745
assert_eq!(result, 2000.00000);
4846
}
49-
5047
}

src/best_time_to_buy_and_sell_stock.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use std::cmp::min;
1212
struct Solution;
1313

1414
impl Solution {
15-
1615
pub fn max_profit(prices: Vec<i32>) -> i32 {
1716
let n = prices.len();
1817
let mut profit = 0;
@@ -25,7 +24,6 @@ impl Solution {
2524

2625
profit
2726
}
28-
2927
}
3028

3129
#[cfg(test)]
@@ -34,16 +32,15 @@ mod tests {
3432

3533
#[test]
3634
fn example_1() {
37-
let prices = vec![7,1,5,3,6,4];
35+
let prices = vec![7, 1, 5, 3, 6, 4];
3836
let result = Solution::max_profit(prices);
3937
assert_eq!(result, 5);
4038
}
4139

4240
#[test]
4341
fn example_2() {
44-
let prices = vec![7,6,4,3,1];
42+
let prices = vec![7, 6, 4, 3, 1];
4543
let result = Solution::max_profit(prices);
4644
assert_eq!(result, 0);
4745
}
48-
4946
}

src/climbing_stairs.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::collections::HashMap;
77
struct Solution;
88

99
impl Solution {
10-
1110
pub fn climb_stairs(n: i32) -> i32 {
1211
let mut ways = HashMap::new();
1312
Self::worker(n, &mut ways)
@@ -22,15 +21,13 @@ impl Solution {
2221
if ways.contains_key(&n) {
2322
ways[&n]
2423
} else {
25-
let result = Self::worker(n-1, ways) + Self::worker(n-2, ways);
24+
let result = Self::worker(n - 1, ways) + Self::worker(n - 2, ways);
2625
ways.insert(n, result);
2726
result
2827
}
2928
}
30-
3129
}
3230
}
33-
3431
}
3532

3633
#[cfg(test)]
@@ -50,5 +47,4 @@ mod tests {
5047
let result = Solution::climb_stairs(n);
5148
assert_eq!(result, 3);
5249
}
53-
5450
}

src/coin_change.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,36 @@ use std::collections::HashMap;
1212
struct Solution;
1313

1414
impl Solution {
15-
1615
pub fn coin_change(coins: Vec<i32>, amount: i32) -> i32 {
1716
let mut mins = HashMap::new();
1817
Self::worker(&coins, amount, &mut mins)
1918
}
2019

2120
fn worker(coins: &Vec<i32>, amount: i32, mins: &mut HashMap<i32, i32>) -> i32 {
22-
if amount < 0 { -1 }
23-
else if amount == 0 { 0 }
24-
else if mins.contains_key(&amount) { mins[&amount] }
25-
else {
21+
if amount < 0 {
22+
-1
23+
} else if amount == 0 {
24+
0
25+
} else if mins.contains_key(&amount) {
26+
mins[&amount]
27+
} else {
2628
let mut result = -1;
2729
for coin in coins {
2830
let sub = Self::worker(coins, amount - coin, mins);
2931
if sub != -1 {
3032
let current = sub + 1;
31-
if result == -1 { result = current; }
32-
else { result = min(result, current); }
33+
if result == -1 {
34+
result = current;
35+
} else {
36+
result = min(result, current);
37+
}
3338
}
3439
}
3540

3641
mins.insert(amount, result);
3742
result
3843
}
3944
}
40-
4145
}
4246

4347
#[cfg(test)]
@@ -46,7 +50,7 @@ mod tests {
4650

4751
#[test]
4852
fn example_1() {
49-
let coins = vec![1,2,5];
53+
let coins = vec![1, 2, 5];
5054
let amount = 11;
5155
let result = Solution::coin_change(coins, amount);
5256
assert_eq!(result, 3);
@@ -67,5 +71,4 @@ mod tests {
6771
let result = Solution::coin_change(coins, amount);
6872
assert_eq!(result, 0);
6973
}
70-
7174
}

src/container_with_most_water.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use std::cmp::max;
1313
struct Solution;
1414

1515
impl Solution {
16-
1716
pub fn max_area(height: Vec<i32>) -> i32 {
1817
let n = height.len();
1918
let mut max_area = 0;
@@ -37,7 +36,6 @@ impl Solution {
3736

3837
max_area
3938
}
40-
4139
}
4240

4341
#[cfg(test)]
@@ -46,16 +44,15 @@ mod tests {
4644

4745
#[test]
4846
fn example_1() {
49-
let height = vec![1,8,6,2,5,4,8,3,7];
47+
let height = vec![1, 8, 6, 2, 5, 4, 8, 3, 7];
5048
let result = Solution::max_area(height);
5149
assert_eq!(result, 49);
5250
}
5351

5452
#[test]
5553
fn example_2() {
56-
let height = vec![1,1];
54+
let height = vec![1, 1];
5755
let result = Solution::max_area(height);
5856
assert_eq!(result, 1);
5957
}
60-
6158
}

src/contains_duplicate.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::collections::HashSet;
66
struct Solution;
77

88
impl Solution {
9-
109
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
1110
let mut seen = HashSet::new();
1211
let mut result = false;
@@ -20,7 +19,6 @@ impl Solution {
2019

2120
result
2221
}
23-
2422
}
2523

2624
#[cfg(test)]
@@ -29,23 +27,22 @@ mod tests {
2927

3028
#[test]
3129
fn example_1() {
32-
let nums = vec![1,2,3,1];
30+
let nums = vec![1, 2, 3, 1];
3331
let result = Solution::contains_duplicate(nums);
3432
assert!(result);
3533
}
3634

3735
#[test]
3836
fn example_2() {
39-
let nums = vec![1,2,3,4];
37+
let nums = vec![1, 2, 3, 4];
4038
let result = Solution::contains_duplicate(nums);
4139
assert!(!result);
4240
}
4341

4442
#[test]
4543
fn example_3() {
46-
let nums = vec![1,1,1,3,3,4,3,2,4,2];
44+
let nums = vec![1, 1, 1, 3, 3, 4, 3, 2, 4, 2];
4745
let result = Solution::contains_duplicate(nums);
4846
assert!(result);
4947
}
50-
5148
}

src/destroying_asteroids.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
struct Solution;
1212

1313
impl Solution {
14-
1514
pub fn asteroids_destroyed(mass: i32, asteroids: Vec<i32>) -> bool {
1615
let mut mass = mass as i64;
1716
let mut by_smallest = asteroids;
@@ -30,7 +29,6 @@ impl Solution {
3029

3130
result
3231
}
33-
3432
}
3533

3634
#[cfg(test)]
@@ -40,17 +38,16 @@ mod tests {
4038
#[test]
4139
fn example_1() {
4240
let mass = 10;
43-
let asteroids = vec![3,9,19,5,21];
41+
let asteroids = vec![3, 9, 19, 5, 21];
4442
let result = Solution::asteroids_destroyed(mass, asteroids);
4543
assert!(result);
4644
}
4745

4846
#[test]
4947
fn example_2() {
5048
let mass = 5;
51-
let asteroids = vec![4,9,23,4];
49+
let asteroids = vec![4, 9, 23, 4];
5250
let result = Solution::asteroids_destroyed(mass, asteroids);
5351
assert!(!result);
5452
}
55-
5653
}

src/find_k_closest_elements.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@ use std::collections::BinaryHeap;
44
#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
55
struct State {
66
distance: usize,
7-
num: i32
7+
num: i32,
88
}
99

1010
impl State {
11-
1211
fn new(num: i32, x: i32) -> Self {
1312
let distance = i32::abs(num - x) as usize;
1413
State { distance, num }
1514
}
16-
1715
}
1816

1917
/// Given a sorted integer array `arr`, two integers `k` and `x`, return the
@@ -26,7 +24,6 @@ impl State {
2624
struct Solution;
2725

2826
impl Solution {
29-
3027
pub fn find_closest_elements(arr: Vec<i32>, k: i32, x: i32) -> Vec<i32> {
3128
let mut min_heap = BinaryHeap::new();
3229
for num in arr {
@@ -41,7 +38,6 @@ impl Solution {
4138
result.sort();
4239
result
4340
}
44-
4541
}
4642

4743
#[cfg(test)]
@@ -50,20 +46,19 @@ mod tests {
5046

5147
#[test]
5248
fn example_1() {
53-
let arr = vec![1,2,3,4,5];
49+
let arr = vec![1, 2, 3, 4, 5];
5450
let k = 4;
5551
let x = 3;
5652
let result = Solution::find_closest_elements(arr, k, x);
57-
assert_eq!(result, vec![1,2,3,4]);
53+
assert_eq!(result, vec![1, 2, 3, 4]);
5854
}
5955

6056
#[test]
6157
fn example_2() {
62-
let arr = vec![1,2,3,4,5];
58+
let arr = vec![1, 2, 3, 4, 5];
6359
let k = 4;
6460
let x = -1;
6561
let result = Solution::find_closest_elements(arr, k, x);
66-
assert_eq!(result, vec![1,2,3,4]);
62+
assert_eq!(result, vec![1, 2, 3, 4]);
6763
}
68-
6964
}

src/find_median_from_data_stream.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::BinaryHeap, cmp::Reverse};
1+
use std::{cmp::Reverse, collections::BinaryHeap};
22

33
/// The median is the middle value in an ordered integer list. If the size of
44
/// the list is even, there is no middle value, and the median is the mean of
@@ -19,15 +19,14 @@ use std::{collections::BinaryHeap, cmp::Reverse};
1919
/// within `10^-5` of the actual answer will be accepted.
2020
struct MedianFinder {
2121
min_heap: BinaryHeap<Reverse<i32>>,
22-
max_heap: BinaryHeap<i32>
22+
max_heap: BinaryHeap<i32>,
2323
}
2424

2525
impl MedianFinder {
26-
2726
fn new() -> Self {
2827
Self {
2928
min_heap: BinaryHeap::new(),
30-
max_heap: BinaryHeap::new()
29+
max_heap: BinaryHeap::new(),
3130
}
3231
}
3332

@@ -49,7 +48,9 @@ impl MedianFinder {
4948
let value = self.min_heap.peek();
5049
if value.is_some() {
5150
value.unwrap().0
52-
} else { 0 }
51+
} else {
52+
0
53+
}
5354
}
5455

5556
fn find_median(&self) -> f64 {
@@ -61,8 +62,6 @@ impl MedianFinder {
6162
}
6263
result
6364
}
64-
65-
6665
}
6766

6867
#[cfg(test)]
@@ -80,5 +79,4 @@ mod tests {
8079
let result2 = median_finder.find_median();
8180
assert_eq!(result2, 2.0);
8281
}
83-
8482
}

0 commit comments

Comments
 (0)