mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
ci(rs): add pre-commit hook for ‹rustfmt›
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
56b6eee734
commit
94c49118db
6 changed files with 42 additions and 21 deletions
|
@ -8,3 +8,8 @@ repos:
|
|||
- id: end-of-file-fixer
|
||||
- id: check-yaml
|
||||
- id: check-added-large-files
|
||||
- repo: https://github.com/mfocko/pre-commit-hooks
|
||||
rev: main
|
||||
hooks:
|
||||
- id: rust-fmt
|
||||
files: \.rs$
|
||||
|
|
|
@ -16,12 +16,12 @@
|
|||
// }
|
||||
// }
|
||||
// }
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
impl Solution {
|
||||
fn inorder(values: &mut Vec<i32>, node: Option<&Rc<RefCell<TreeNode>>>) {
|
||||
match node {
|
||||
None => {},
|
||||
None => {}
|
||||
Some(n) => {
|
||||
Solution::inorder(values, (*n).borrow().left.as_ref());
|
||||
values.push((*n).borrow().val);
|
||||
|
|
|
@ -17,9 +17,9 @@
|
|||
// }
|
||||
// }
|
||||
|
||||
use std::rc::Rc;
|
||||
use std::cmp;
|
||||
use std::cell::RefCell;
|
||||
use std::cmp;
|
||||
use std::rc::Rc;
|
||||
|
||||
fn _max_depth(root: Option<Rc<RefCell<TreeNode>>>, depth: i32) -> i32 {
|
||||
match root {
|
||||
|
|
|
@ -7,7 +7,10 @@ struct Accumulator {
|
|||
|
||||
impl Accumulator {
|
||||
fn new() -> Accumulator {
|
||||
Accumulator { removed: 0, removed_distinct: 0 }
|
||||
Accumulator {
|
||||
removed: 0,
|
||||
removed_distinct: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn update(self, length: usize, count: i32) -> Accumulator {
|
||||
|
@ -26,24 +29,27 @@ impl Solution {
|
|||
pub fn min_set_size(arr: Vec<i32>) -> i32 {
|
||||
let mut counts = arr
|
||||
.iter()
|
||||
.fold(BTreeMap::<i32, i32>::new(), |mut acc: BTreeMap<i32, i32>, x: &i32| {
|
||||
let current = *acc.get(x).unwrap_or(&0);
|
||||
acc.insert(*x, current + 1);
|
||||
.fold(
|
||||
BTreeMap::<i32, i32>::new(),
|
||||
|mut acc: BTreeMap<i32, i32>, x: &i32| {
|
||||
let current = *acc.get(x).unwrap_or(&0);
|
||||
acc.insert(*x, current + 1);
|
||||
|
||||
acc
|
||||
})
|
||||
acc
|
||||
},
|
||||
)
|
||||
.iter()
|
||||
.map(|(&x, &y)| { (x, y) })
|
||||
.map(|(&x, &y)| (x, y))
|
||||
.collect::<Vec<(i32, i32)>>();
|
||||
|
||||
counts
|
||||
.sort_by(|&(_, y0), &(_, y1)| y1.cmp(&y0));
|
||||
counts.sort_by(|&(_, y0), &(_, y1)| y1.cmp(&y0));
|
||||
|
||||
counts
|
||||
.iter()
|
||||
.fold(Accumulator::new(), |acc, &(_, count)| {
|
||||
acc.update(arr.len(), count)
|
||||
}).removed_distinct
|
||||
})
|
||||
.removed_distinct
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -59,12 +65,18 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_example() {
|
||||
assert_eq!(Solution::min_set_size(vec![3, 3, 3, 3, 5, 5, 5, 2, 2, 7]), 2);
|
||||
assert_eq!(
|
||||
Solution::min_set_size(vec![3, 3, 3, 3, 5, 5, 5, 2, 2, 7]),
|
||||
2
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_all_same() {
|
||||
assert_eq!(Solution::min_set_size(vec![7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]), 1);
|
||||
assert_eq!(
|
||||
Solution::min_set_size(vec![7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]),
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
impl Solution {
|
||||
pub fn running_sum(nums: Vec<i32>) -> Vec<i32> {
|
||||
nums.iter().scan(0, |s, &x| {
|
||||
*s = *s + x;
|
||||
Some(*s)
|
||||
}).collect()
|
||||
nums.iter()
|
||||
.scan(0, |s, &x| {
|
||||
*s = *s + x;
|
||||
Some(*s)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,6 +13,8 @@ impl Solution {
|
|||
let mut frequencies: Vec<_> = freqs.iter().collect();
|
||||
frequencies.sort_by_key(|&(_, count)| Reverse(count));
|
||||
|
||||
frequencies.iter().fold(String::new(), |s, (c, count)| s + &c.to_string().repeat(**count))
|
||||
frequencies.iter().fold(String::new(), |s, (c, count)| {
|
||||
s + &c.to_string().repeat(**count)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue