leetcode 2043.简易银行系统
1.题目要求:
示例:
输入:
["Bank", "withdraw", "transfer", "deposit", "transfer", "withdraw"]
[[[10, 100, 20, 50, 30]], [3, 10], [5, 1, 20], [5, 20], [3, 4, 15], [10, 50]]
输出:
[null, true, true, true, false, false]
解释:
Bank bank = new Bank([10, 100, 20, 50, 30]);
bank.withdraw(3, 10); // 返回 true ,账户 3 的余额是 $20 ,所以可以取款 $10 。
// 账户 3 余额为 $20 - $10 = $10 。
bank.transfer(5, 1, 20); // 返回 true ,账户 5 的余额是 $30 ,所以可以转账 $20 。
// 账户 5 的余额为 $30 - $20 = $10 ,账户 1 的余额为 $10 + $20 = $30 。
bank.deposit(5, 20); // 返回 true ,可以向账户 5 存款 $20 。
// 账户 5 的余额为 $10 + $20 = $30 。
bank.transfer(3, 4, 15); // 返回 false ,账户 3 的当前余额是 $10 。
// 所以无法转账 $15 。
bank.withdraw(10, 50); // 返回 false ,交易无效,因为账户 10 并不存在。
提示:
n == balance.length
1 <= n, account, account1, account2 <= 105
0 <= balance[i], money <= 1012
transfer, deposit, withdraw 三个函数,每个 最多调用 104 次
2.题目代码:
class Bank {
public:
vector<long long> bank_account;
Bank(vector<long long>& balance) {
bank_account = balance;
}
//转帐
bool transfer(int account1, int account2, long long money) {
if(account1 > bank_account.size() || account2 > bank_account.size()){
return false;
}
if(bank_account[account1 - 1] >= money){
bank_account[account1 - 1] -= money;
bank_account[account2 - 1] += money;
return true;
}else{
return false;
}
}
//存款
bool deposit(int account, long long money) {
if(account > bank_account.size()){
return false;
}else{
bank_account[account - 1] += money;
return true;
}
}
//取款
bool withdraw(int account, long long money) {
if(account > bank_account.size()){
return false;
}
if(bank_account[account - 1] >= money){
bank_account[account - 1] -= money;
return true;
}else{
return false;
}
}
};
/**
* 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);
*/
原文地址:https://blog.csdn.net/m0_54244065/article/details/143589855
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!