LC_2043

先看题目

 

 

 

 本以为这是一道难题,没想到就是一道简单的模拟题,题解写得比我还简单

有一个坑需要注意:

编号从1~n ,数组的下标是从0~n-1

然后按照题目将这些操作模拟一遍即可

这题可以说非常社畜

 1 class Bank {
 2 
 3     private long[] balance;
 4     public Bank(long[] balance) {
 5         this.balance = balance;
 6     }
 7     
 8     public boolean transfer(int account1, int account2, long money) {
 9         if(account1 < 1 || account1 > balance.length){
10             return false;
11         }
12         if(account2 < 1 || account2 > balance.length){
13             return false;
14         }
15         if(balance[account1-1] >= money){
16             balance[account1-1] -= money;
17             balance[account2-1] += money;
18             return true;
19         }else{
20             return false;
21         }
22     }
23     
24     public boolean deposit(int account, long money) {
25         if(account < 1 || account > balance.length){
26             return false;
27         }
28         long preMoney = balance[account-1];
29         balance[account-1] += money;
30         if(balance[account-1] == (preMoney + money)){
31             return true;
32         }else{
33             return false;
34         }
35     }
36     
37     public boolean withdraw(int account, long money) {
38         if(account < 1 || account > balance.length){
39             return false;
40         }
41         if(balance[account-1] >= money){
42             balance[account-1] -= money;
43             return true;
44         }else{
45             return false;
46         }
47     }
48 }
49 
50 /**
51  * Your Bank object will be instantiated and called as such:
52  * Bank obj = new Bank(balance);
53  * boolean param_1 = obj.transfer(account1,account2,money);
54  * boolean param_2 = obj.deposit(account,money);
55  * boolean param_3 = obj.withdraw(account,money);
56  */

 

即便现在很迷茫,也不要放弃

posted @ 2022-03-18 16:38  雨下_整夜  阅读(25)  评论(0)    收藏  举报