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 Bank {
public:
Bank(vector<long long>& balance) {
}
bool transfer(int account1, int account2, long long money) {
}
bool deposit(int account, long long money) {
}
bool withdraw(int account, long long money) {
}
};
/**
* Your Bank object will be instantiated and called as such:
* Bank* obj = new Bank(balance);
* bool param_1 = obj->transfer(account1,account2,money);
* bool param_2 = obj->deposit(account,money);
* bool param_3 = obj->withdraw(account,money);
*/
运行代码
提交
javascript 解法, 执行用时: 292 ms, 内存消耗: 68 MB, 提交时间: 2022-11-26 17:39:58
/**
* @param {number[]} balance
*/
var Bank = function(balance) {
this.balance = balance
};
/**
* @param {number} account1
* @param {number} account2
* @param {number} money
* @return {boolean}
*/
Bank.prototype.transfer = function(account1, account2, money) {
if (account1 > this.balance.length || account2 > this.balance.length || this.balance[account1 - 1] < money) {
return false;
}
this.balance[account1 - 1] -= money;
this.balance[account2 - 1] += money;
return true;
};
/**
* @param {number} account
* @param {number} money
* @return {boolean}
*/
Bank.prototype.deposit = function(account, money) {
if (account > this.balance.length) {
return false;
}
this.balance[account - 1] += money;
return true;
};
/**
* @param {number} account
* @param {number} money
* @return {boolean}
*/
Bank.prototype.withdraw = function(account, money) {
if (account > this.balance.length || this.balance[account - 1] < money) {
return false;
}
this.balance[account - 1] -= money;
return true;
};
/**
* Your Bank object will be instantiated and called as such:
* var obj = new Bank(balance)
* var param_1 = obj.transfer(account1,account2,money)
* var param_2 = obj.deposit(account,money)
* var param_3 = obj.withdraw(account,money)
*/
golang 解法, 执行用时: 316 ms, 内存消耗: 36.9 MB, 提交时间: 2022-11-26 17:37:18
type Bank []int64
func Constructor(balance []int64) Bank {
return balance
}
func (b Bank) Transfer(account1, account2 int, money int64) bool {
if account1 > len(b) || account2 > len(b) || b[account1-1] < money {
return false
}
b[account1-1] -= money
b[account2-1] += money
return true
}
func (b Bank) Deposit(account int, money int64) bool {
if account > len(b) {
return false
}
b[account-1] += money
return true
}
func (b Bank) Withdraw(account int, money int64) bool {
if account > len(b) || b[account-1] < money {
return false
}
b[account-1] -= money
return true
}
/**
* Your Bank object will be instantiated and called as such:
* obj := Constructor(balance);
* param_1 := obj.Transfer(account1,account2,money);
* param_2 := obj.Deposit(account,money);
* param_3 := obj.Withdraw(account,money);
*/
python3 解法, 执行用时: 248 ms, 内存消耗: 40.5 MB, 提交时间: 2022-11-26 17:37:05
class Bank:
def __init__(self, balance: List[int]):
self.balance = balance
def transfer(self, account1: int, account2: int, money: int) -> bool:
if account1 > len(self.balance) or account2 > len(self.balance) or self.balance[account1 - 1] < money:
return False
self.balance[account1 - 1] -= money
self.balance[account2 - 1] += money
return True
def deposit(self, account: int, money: int) -> bool:
if account > len(self.balance):
return False
self.balance[account - 1] += money
return True
def withdraw(self, account: int, money: int) -> bool:
if account > len(self.balance) or self.balance[account - 1] < money:
return False
self.balance[account - 1] -= money
return True
# Your Bank object will be instantiated and called as such:
# obj = Bank(balance)
# param_1 = obj.transfer(account1,account2,money)
# param_2 = obj.deposit(account,money)
# param_3 = obj.withdraw(account,money)