-
Notifications
You must be signed in to change notification settings - Fork 0
/
design-parking-system.rs
63 lines (57 loc) · 1.55 KB
/
design-parking-system.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#![allow(unused)]
/// https://leetcode-cn.com/problems/design-parking-system/
use std::sync::atomic::AtomicI32;
struct ParkingSystem {
big: AtomicI32,
medium: AtomicI32,
small: AtomicI32,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl ParkingSystem {
fn new(big: i32, medium: i32, small: i32) -> Self {
Self {
big: AtomicI32::new(big),
medium: AtomicI32::new(medium),
small: AtomicI32::new(small),
}
}
fn add_car(&mut self, car_type: i32) -> bool {
let get_car_parking = |x: &mut i32| {
if *x - 1 < 0 {
false
} else {
*x -= 1;
true
}
};
match car_type {
1 => get_car_parking(&mut (*self.big.get_mut())),
2 => get_car_parking(&mut (*self.medium.get_mut())),
3 => get_car_parking(&mut (*self.small.get_mut())),
_ => false,
}
}
}
/**
* Your ParkingSystem object will be instantiated and called as such:
* let obj = ParkingSystem::new(big, medium, small);
* let ret_1: bool = obj.add_car(carType);
*/
fn main() {
let mut obj = ParkingSystem::new(1, 1, 0);
println!("{}", obj.add_car(1));
}
#[cfg(test)]
mod tests {
use crate::ParkingSystem;
#[test]
fn test() {
let mut obj = ParkingSystem::new(1, 1, 0);
assert!(obj.add_car(1));
assert!(obj.add_car(2));
assert!(!obj.add_car(3));
}
}