File tree 4 files changed +120
-2
lines changed
solution/1600-1699/1603.Design Parking System
4 files changed +120
-2
lines changed Original file line number Diff line number Diff line change @@ -47,22 +47,62 @@ parkingSystem.addCar(1); // 返回 false ,因为没有空的大车位,唯一
47
47
48
48
<!-- 这里可写通用的实现逻辑 -->
49
49
50
+ 为每种车维护一个计数器,初始值为车位的数目。此后,每来一辆车,就将对应类型的计数器减 1。当计数器为 0 时,说明车位已满。
51
+
50
52
<!-- tabs:start -->
51
53
52
54
### ** Python3**
53
55
54
56
<!-- 这里可写当前语言的特殊实现逻辑 -->
55
57
56
58
``` python
59
+ class ParkingSystem :
60
+
61
+ def __init__ (self , big : int , medium : int , small : int ):
62
+ self .spaces = [big, medium, small]
63
+
57
64
65
+ def addCar (self , carType : int ) -> bool :
66
+ if self .spaces[carType - 1 ] <= 0 :
67
+ return False
68
+ self .spaces[carType - 1 ] -= 1
69
+ return True
70
+
71
+
72
+ # Your ParkingSystem object will be instantiated and called as such:
73
+ # obj = ParkingSystem(big, medium, small)
74
+ # param_1 = obj.addCar(carType)
58
75
```
59
76
60
77
### ** Java**
61
78
62
79
<!-- 这里可写当前语言的特殊实现逻辑 -->
63
80
64
81
``` java
65
-
82
+ class ParkingSystem {
83
+
84
+ private int [] spaces = new int [3 ];
85
+
86
+ public ParkingSystem (int big , int medium , int small ) {
87
+ spaces[0 ] = big;
88
+ spaces[1 ] = medium;
89
+ spaces[2 ] = small;
90
+ }
91
+
92
+ public boolean addCar (int carType ) {
93
+ if (spaces[carType - 1 ] <= 0 ) {
94
+ return false ;
95
+ }
96
+ -- spaces[carType - 1 ];
97
+ return true ;
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Your ParkingSystem object will be instantiated and called as such:
103
+ * ParkingSystem obj = new ParkingSystem(big, medium, small);
104
+ * boolean param_1 = obj.addCar(carType);
105
+ */
66
106
```
67
107
68
108
### ** ...**
Original file line number Diff line number Diff line change @@ -47,13 +47,51 @@ parkingSystem.addCar(1); // return false because there is no available slot for
47
47
### ** Python3**
48
48
49
49
``` python
50
+ class ParkingSystem :
50
51
52
+ def __init__ (self , big : int , medium : int , small : int ):
53
+ self .spaces = [big, medium, small]
54
+
55
+
56
+ def addCar (self , carType : int ) -> bool :
57
+ if self .spaces[carType - 1 ] <= 0 :
58
+ return False
59
+ self .spaces[carType - 1 ] -= 1
60
+ return True
61
+
62
+
63
+ # Your ParkingSystem object will be instantiated and called as such:
64
+ # obj = ParkingSystem(big, medium, small)
65
+ # param_1 = obj.addCar(carType)
51
66
```
52
67
53
68
### ** Java**
54
69
55
70
``` java
56
-
71
+ class ParkingSystem {
72
+
73
+ private int [] spaces = new int [3 ];
74
+
75
+ public ParkingSystem (int big , int medium , int small ) {
76
+ spaces[0 ] = big;
77
+ spaces[1 ] = medium;
78
+ spaces[2 ] = small;
79
+ }
80
+
81
+ public boolean addCar (int carType ) {
82
+ if (spaces[carType - 1 ] <= 0 ) {
83
+ return false ;
84
+ }
85
+ -- spaces[carType - 1 ];
86
+ return true ;
87
+ }
88
+ }
89
+
90
+ /**
91
+ * Your ParkingSystem object will be instantiated and called as such:
92
+ * ParkingSystem obj = new ParkingSystem(big, medium, small);
93
+ * boolean param_1 = obj.addCar(carType);
94
+ */
57
95
```
58
96
59
97
### ** ...**
Original file line number Diff line number Diff line change
1
+ class ParkingSystem {
2
+
3
+ private int [] spaces = new int [3 ];
4
+
5
+ public ParkingSystem (int big , int medium , int small ) {
6
+ spaces [0 ] = big ;
7
+ spaces [1 ] = medium ;
8
+ spaces [2 ] = small ;
9
+ }
10
+
11
+ public boolean addCar (int carType ) {
12
+ if (spaces [carType - 1 ] <= 0 ) {
13
+ return false ;
14
+ }
15
+ --spaces [carType - 1 ];
16
+ return true ;
17
+ }
18
+ }
19
+
20
+ /**
21
+ * Your ParkingSystem object will be instantiated and called as such:
22
+ * ParkingSystem obj = new ParkingSystem(big, medium, small);
23
+ * boolean param_1 = obj.addCar(carType);
24
+ */
Original file line number Diff line number Diff line change
1
+ class ParkingSystem :
2
+
3
+ def __init__ (self , big : int , medium : int , small : int ):
4
+ self .spaces = [big , medium , small ]
5
+
6
+
7
+ def addCar (self , carType : int ) -> bool :
8
+ if self .spaces [carType - 1 ] <= 0 :
9
+ return False
10
+ self .spaces [carType - 1 ] -= 1
11
+ return True
12
+
13
+
14
+ # Your ParkingSystem object will be instantiated and called as such:
15
+ # obj = ParkingSystem(big, medium, small)
16
+ # param_1 = obj.addCar(carType)
You can’t perform that action at this time.
0 commit comments