C++
Java
Python
Python3
C
C#
JavaScript
Ruby
Swift
Go
Scala
Kotlin
Rust
PHP
TypeScript
Racket
Erlang
Elixir
Dart
monokai
ambiance
chaos
chrome
cloud9_day
cloud9_night
cloud9_night_low_color
clouds
clouds_midnight
cobalt
crimson_editor
dawn
dracula
dreamweaver
eclipse
github
github_dark
gob
gruvbox
gruvbox_dark_hard
gruvbox_light_hard
idle_fingers
iplastic
katzenmilch
kr_theme
kuroir
merbivore
merbivore_soft
mono_industrial
nord_dark
one_dark
pastel_on_dark
solarized_dark
solarized_light
sqlserver
terminal
textmate
tomorrow
tomorrow_night
tomorrow_night_blue
tomorrow_night_bright
tomorrow_night_eighties
twilight
vibrant_ink
xcode
上次编辑到这里,代码来自缓存 点击恢复默认模板
class ParkingSystem {
public:
ParkingSystem(int big, int medium, int small) {
}
bool addCar(int carType) {
}
};
/**
* Your ParkingSystem object will be instantiated and called as such:
* ParkingSystem* obj = new ParkingSystem(big, medium, small);
* bool param_1 = obj->addCar(carType);
*/
运行代码
提交
kotlin 解法, 执行用时: 336 ms, 内存消耗: 46.3 MB, 提交时间: 2023-10-11 11:03:04
class ParkingSystem(big: Int, medium: Int, small: Int) {
private val park = intArrayOf(0, big, medium, small)
fun addCar(carType: Int): Boolean = park[carType]-- > 0
}
/**
* Your ParkingSystem object will be instantiated and called as such:
* var obj = ParkingSystem(big, medium, small)
* var param_1 = obj.addCar(carType)
*/
rust 解法, 执行用时: 20 ms, 内存消耗: 2.3 MB, 提交时间: 2023-10-11 11:02:37
struct ParkingSystem {
park: Vec<i32>,
}
/**
* `&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 {
ParkingSystem { park: vec![0, big, medium, small] }
}
fn add_car(&mut self, car_type: i32) -> bool {
self.park[car_type as usize] -= 1;
self.park[car_type as usize] >= 0
}
}
/**
* 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);
*/
javascript 解法, 执行用时: 144 ms, 内存消耗: 48.7 MB, 提交时间: 2023-10-11 11:01:54
/**
* @param {number} big
* @param {number} medium
* @param {number} small
*/
var ParkingSystem = function(big, medium, small) {
this.big = big;
this.medium = medium;
this.small = small;
};
/**
* @param {number} carType
* @return {boolean}
*/
ParkingSystem.prototype.addCar = function(carType) {
if (carType === 1) {
if (this.big > 0) {
this.big--;
return true;
}
} else if (carType === 2) {
if (this.medium > 0) {
this.medium--;
return true;
}
} else if (carType === 3) {
if (this.small > 0) {
this.small--;
return true;
}
}
return false;
};
/**
* Your ParkingSystem object will be instantiated and called as such:
* var obj = new ParkingSystem(big, medium, small)
* var param_1 = obj.addCar(carType)
*/
java 解法, 执行用时: 7 ms, 内存消耗: 43.4 MB, 提交时间: 2023-10-11 11:01:13
class ParkingSystem {
int big, medium, small;
public ParkingSystem(int big, int medium, int small) {
this.big = big;
this.medium = medium;
this.small = small;
}
public boolean addCar(int carType) {
if (carType == 1) {
if (big > 0) {
big--;
return true;
}
} else if (carType == 2) {
if (medium > 0) {
medium--;
return true;
}
} else if (carType == 3) {
if (small > 0) {
small--;
return true;
}
}
return false;
}
}
/**
* Your ParkingSystem object will be instantiated and called as such:
* ParkingSystem obj = new ParkingSystem(big, medium, small);
* boolean param_1 = obj.addCar(carType);
*/
cpp 解法, 执行用时: 60 ms, 内存消耗: 32.8 MB, 提交时间: 2023-10-11 11:00:55
class ParkingSystem {
public:
int b, m, s;
ParkingSystem(int big, int medium, int small): b(big), m(medium), s(small) {}
bool addCar(int carType) {
if (carType == 1) {
if (b > 0) {
b--;
return true;
}
} else if (carType == 2) {
if (m > 0) {
m--;
return true;
}
} else if (carType == 3) {
if (s > 0) {
s--;
return true;
}
}
return false;
}
};
/**
* Your ParkingSystem object will be instantiated and called as such:
* ParkingSystem* obj = new ParkingSystem(big, medium, small);
* bool param_1 = obj->addCar(carType);
*/
python3 解法, 执行用时: 148 ms, 内存消耗: 15.1 MB, 提交时间: 2021-03-19 23:50:56
class ParkingSystem:
def __init__(self, big: int, medium: int, small: int):
self.cards = [big, medium, small]
def addCar(self, carType: int) -> bool:
if self.cards[carType-1] > 0:
self.cards[carType-1] -= 1
return True
return False
# Your ParkingSystem object will be instantiated and called as such:
# obj = ParkingSystem(big, medium, small)
# param_1 = obj.addCar(carType)
golang 解法, 执行用时: 52 ms, 内存消耗: 7.2 MB, 提交时间: 2021-03-19 23:49:21
type ParkingSystem struct {
cards []int
}
func Constructor(big int, medium int, small int) ParkingSystem {
return ParkingSystem{cards: []int{big, medium, small}}
}
func (this *ParkingSystem) AddCar(carType int) bool {
if this.cards[carType-1] > 0 {
this.cards[carType-1]--
return true
}
return false
}
/**
* Your ParkingSystem object will be instantiated and called as such:
* obj := Constructor(big, medium, small);
* param_1 := obj.AddCar(carType);
*/