作者Rushia (みけねこ的鼻屎)
看板Marginalman
標題Re: [閒聊] 每日LeetCode
時間Mon May 29 20:46:05 2023
https://leetcode.com/problems/design-parking-system/description/
1603. Design Parking System
設計一個停車場類別,他有大、中、小三種大小的車位,分別對應1、2、3的類型
1.ParkingSystem(int big, int medium, int small):初始化車位有幾個
2.bool addCar(int carType):carType類型的車可以停進去的話返回true。
Example 1:
Input
["ParkingSystem", "addCar", "addCar", "addCar", "addCar"]
[[1, 1, 0], [1], [2], [3], [1]]
Output
[null, true, true, false, false]
Explanation
ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0);
parkingSystem.addCar(1); // return true because there is 1 available slot for
a big car
parkingSystem.addCar(2); // return true because there is 1 available slot for
a medium car
parkingSystem.addCar(3); // return false because there is no available slot
for a small car
parkingSystem.addCar(1); // return false because there is no available slot
for a big car. It is already occupied.
思路:
1.用一個陣列保存當前車位剩幾個
Java Code
--------------------------------------------
class ParkingSystem {
private int[] slot;
public ParkingSystem(int big, int medium, int small) {
slot = new int[]{big, medium, small};
}
public boolean addCar(int carType) {
if (slot[carType - 1] <= 0) {
return false;
}
slot[carType - 1]--;
return true;
}
}
--------------------------------------------
又水了一題= =
--
https://i.imgur.com/CBMFmWk.jpg
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.100.75.86 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1685364368.A.058.html
推 a9486l: 大師 05/29 20:46
※ 編輯: Rushia (122.100.75.86 臺灣), 05/29/2023 20:47:47
推 NTHUlagka: 大師 05/30 00:49