2043.简易银行系统
链接:2043.简易银行系统
难度:Medium
标签:设计、数组、哈希表、模拟
简介:实现 Bank 类。
题解 1 - cpp
- 编辑时间:2022-03-18
- 执行用时:200ms
- 内存消耗:113.9MB
- 编程语言:cpp
- 解法介绍:存储数据后校验。
class Bank {
public:
int n;
vector<long long> accounts;
Bank(vector<long long>& balance) {
n = balance.size();
this->accounts = balance;
}
bool transfer(int account1, int account2, long long money) {
if (!check_account(account1) || !check_account(account2) ||
!check_money(account1, money))
return false;
accounts[account1 - 1] -= money;
accounts[account2 - 1] += money;
return true;
}
bool deposit(int account, long long money) {
if (!check_account(account)) return false;
accounts[account - 1] += money;
return true;
}
bool withdraw(int account, long long money) {
if (!check_account(account) || !check_money(account, money))
return false;
accounts[account - 1] -= money;
return true;
}
bool check_account(int account) { return account >= 1 && account <= n; }
bool check_money(int account, long long money) {
return accounts[account - 1] >= money;
}
};