Created
May 29, 2023 05:22
-
-
Save mdrokz/42f8741b7f369adf6ff4a487caeb97fd to your computer and use it in GitHub Desktop.
Leetcode problem 1603 design a parking system implemented in rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum CarType { | |
Big(i32), | |
Med(i32), | |
Small(i32) | |
} | |
struct ParkingSystem { | |
big: CarType, | |
medium: CarType, | |
small: CarType | |
} | |
impl CarType { | |
fn get_count(&self) -> i32 { | |
match self { | |
CarType::Big(count) => *count, | |
CarType::Med(count) => *count, | |
CarType::Small(count) => *count | |
} | |
} | |
fn remove_car(&mut self) { | |
match self { | |
CarType::Big(count) => *count -= 1, | |
CarType::Med(count) => *count -= 1, | |
CarType::Small(count) => *count -= 1 | |
} | |
} | |
} | |
/** | |
* `&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: CarType::Big(big), | |
medium: CarType::Med(medium), | |
small: CarType::Small(small) | |
} | |
} | |
fn add_car(&mut self, car_type: i32) -> bool { | |
match car_type { | |
1 => { | |
if self.big.get_count() > 0 { | |
self.big.remove_car(); | |
return true; | |
} | |
return false; | |
}, | |
2 => { | |
if self.medium.get_count() > 0 { | |
self.medium.remove_car(); | |
return true; | |
} | |
return false; | |
}, | |
3 => { | |
if self.small.get_count() > 0 { | |
self.small.remove_car(); | |
return true; | |
} | |
return false; | |
}, | |
_ => { | |
return 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); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment