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 ATM {
public:
ATM() {
}
void deposit(vector<int> banknotesCount) {
}
vector<int> withdraw(int amount) {
}
};
/**
* Your ATM object will be instantiated and called as such:
* ATM* obj = new ATM();
* obj->deposit(banknotesCount);
* vector<int> param_2 = obj->withdraw(amount);
*/
运行代码
提交
java 解法, 执行用时: 139 ms, 内存消耗: 53.8 MB, 提交时间: 2023-09-11 09:56:53
class ATM {
private long[] cnt;
private int[] nums;
public ATM() {
this.cnt = new long[5];
this.nums = new int[]{20, 50, 100, 200, 500};
}
public void deposit(int[] banknotesCount) {
for (int i = 0; i < 5; i++) {
cnt[i] += banknotesCount[i];
}
}
public int[] withdraw(int amount) {
int[] res = new int[5];
for (int i = 4; i >= 0; i--) {
if (amount >= nums[i] && cnt[i] > 0) {
int num = (amount / nums[i]) > cnt[i] ? (int) cnt[i] : (amount / nums[i]);
res[i] = num;
amount -= nums[i] * num;
}
}
// 如果能取出来,再对cnt真正操作
if (amount == 0) {
for (int i = 0; i < 5; i++) {
cnt[i] -= res[i];
}
return res;
}
return new int[]{-1};
}
}
/**
* Your ATM object will be instantiated and called as such:
* ATM obj = new ATM();
* obj.deposit(banknotesCount);
* int[] param_2 = obj.withdraw(amount);
*/
python3 解法, 执行用时: 232 ms, 内存消耗: 19.8 MB, 提交时间: 2023-09-11 09:45:42
class ATM:
def __init__(self):
self.atm = [0] * 5 # 0: 20, 1: 50, 2: 100, 3: 200, 4: 500
def deposit(self, banknotesCount: List[int]) -> None:
for i, cnt in enumerate(banknotesCount):
self.atm[i] += cnt
def withdraw(self, amount: int) -> List[int]:
res = [0] * 5
money = [20, 50, 100, 200, 500]
for i in range(4, -1, -1):
res[i] = min(amount // money[i], self.atm[i])
amount -= res[i] * money[i]
if amount > 0:
return [-1]
for i, cnt in enumerate(res):
self.atm[i] -= cnt
return res
# Your ATM object will be instantiated and called as such:
# obj = ATM()
# obj.deposit(banknotesCount)
# param_2 = obj.withdraw(amount)
php 解法, 执行用时: 380 ms, 内存消耗: 25.9 MB, 提交时间: 2023-09-11 09:45:06
class ATM {
public $money = [20, 50, 100, 200, 500];
/**
*/
function __construct() {
$this->atm = array_fill(0, 5, 0);
}
/**
* @param Integer[] $banknotesCount
* @return NULL
*/
function deposit($banknotesCount) {
foreach ( $banknotesCount as $i => $cnt ) {
$this->atm[$i] += $cnt;
}
}
/**
* @param Integer $amount
* @return Integer[]
*/
function withdraw($amount) {
$ans = array_fill(0, 5, 0);
for ( $i = 4; $i >= 0; $i-- ) {
$ans[$i] = min(intval($amount/$this->money[$i]), $this->atm[$i]);
$amount -= $ans[$i] * $this->money[$i];
}
if ( $amount > 0 ) { // 没法取恰好 amount
return [-1];
}
foreach ( $ans as $i => $c ) {
$this->atm[$i] -= $c;
}
return $ans;
}
}
/**
* Your ATM object will be instantiated and called as such:
* $obj = ATM();
* $obj->deposit($banknotesCount);
* $ret_2 = $obj->withdraw($amount);
*/
golang 解法, 执行用时: 196 ms, 内存消耗: 8.1 MB, 提交时间: 2023-09-11 09:35:57
var a = [5]int{20, 50, 100, 200, 500}
type ATM [5]int
func Constructor() ATM { return ATM{} }
// 存钱
func (left *ATM) Deposit(banknotesCount []int) {
for i, count := range banknotesCount {
left[i] += count
}
}
// 取钱
func (left *ATM) Withdraw(amount int) []int {
ans := make([]int, 5)
for i := 4; i >= 0; i-- {
ans[i] = min(amount/a[i], left[i])
amount -= ans[i] * a[i]
}
if amount > 0 { // 没法取恰好 amount
return []int{-1}
}
for i, count := range ans {
left[i] -= count
}
return ans
}
func min(a, b int) int { if a > b { return b }; return a }
/**
* Your ATM object will be instantiated and called as such:
* obj := Constructor();
* obj.Deposit(banknotesCount);
* param_2 := obj.Withdraw(amount);
*/