列表

详情


2043. 简易银行系统

你的任务是为一个很受欢迎的银行设计一款程序,以自动化执行所有传入的交易(转账,存款和取款)。银行共有 n 个账户,编号从 1n 。每个账号的初始余额存储在一个下标从 0 开始的整数数组 balance 中,其中第 (i + 1) 个账户的初始余额是 balance[i]

请你执行所有 有效的 交易。如果满足下面全部条件,则交易 有效

实现 Bank 类:

 

示例:

输入:
["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 并不存在。

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
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)

上一题