Skip to content

Commit 01f3f4f

Browse files
committedMar 18, 2022
feat: add rust solution to lc problem: No.2043
No.2043.Simple Bank System
1 parent 341d7ef commit 01f3f4f

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed
 

‎solution/2000-2099/2043.Simple Bank System/README.md

+60
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,66 @@ func (this *Bank) Withdraw(account int, money int64) bool {
287287
*/
288288
```
289289

290+
### **Rust**
291+
292+
```rust
293+
struct Bank {
294+
balance: Vec<i64>,
295+
}
296+
297+
/**
298+
* `&self` means the method takes an immutable reference.
299+
* If you need a mutable reference, change it to `&mut self` instead.
300+
*/
301+
impl Bank {
302+
fn new(balance: Vec<i64>) -> Self {
303+
Bank { balance }
304+
}
305+
306+
fn transfer(&mut self, account1: i32, account2: i32, money: i64) -> bool {
307+
let (account1, account2, n) = (account1 as usize, account2 as usize, self.balance.len());
308+
if n < account1 || n < account2 {
309+
return false;
310+
}
311+
if self.balance[account1 - 1] < money {
312+
return false;
313+
}
314+
self.balance[account1 - 1] -= money;
315+
self.balance[account2 - 1] += money;
316+
true
317+
}
318+
319+
fn deposit(&mut self, account: i32, money: i64) -> bool {
320+
let (account, n) = (account as usize, self.balance.len());
321+
if n < account {
322+
return false;
323+
}
324+
self.balance[account - 1] += money;
325+
true
326+
}
327+
328+
fn withdraw(&mut self, account: i32, money: i64) -> bool {
329+
let (account, n) = (account as usize, self.balance.len());
330+
if n < account {
331+
return false;
332+
}
333+
if self.balance[account - 1] < money {
334+
return false;
335+
}
336+
self.balance[account - 1] -= money;
337+
true
338+
}
339+
}
340+
341+
/**
342+
* Your Bank object will be instantiated and called as such:
343+
* let obj = Bank::new(balance);
344+
* let ret_1: bool = obj.transfer(account1, account2, money);
345+
* let ret_2: bool = obj.deposit(account, money);
346+
* let ret_3: bool = obj.withdraw(account, money);
347+
*/
348+
```
349+
290350
### **...**
291351

292352
```

‎solution/2000-2099/2043.Simple Bank System/README_EN.md

+60
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,66 @@ func (this *Bank) Withdraw(account int, money int64) bool {
277277
*/
278278
```
279279

280+
### **Rust**
281+
282+
```rust
283+
struct Bank {
284+
balance: Vec<i64>,
285+
}
286+
287+
/**
288+
* `&self` means the method takes an immutable reference.
289+
* If you need a mutable reference, change it to `&mut self` instead.
290+
*/
291+
impl Bank {
292+
fn new(balance: Vec<i64>) -> Self {
293+
Bank { balance }
294+
}
295+
296+
fn transfer(&mut self, account1: i32, account2: i32, money: i64) -> bool {
297+
let (account1, account2, n) = (account1 as usize, account2 as usize, self.balance.len());
298+
if n < account1 || n < account2 {
299+
return false;
300+
}
301+
if self.balance[account1 - 1] < money {
302+
return false;
303+
}
304+
self.balance[account1 - 1] -= money;
305+
self.balance[account2 - 1] += money;
306+
true
307+
}
308+
309+
fn deposit(&mut self, account: i32, money: i64) -> bool {
310+
let (account, n) = (account as usize, self.balance.len());
311+
if n < account {
312+
return false;
313+
}
314+
self.balance[account - 1] += money;
315+
true
316+
}
317+
318+
fn withdraw(&mut self, account: i32, money: i64) -> bool {
319+
let (account, n) = (account as usize, self.balance.len());
320+
if n < account {
321+
return false;
322+
}
323+
if self.balance[account - 1] < money {
324+
return false;
325+
}
326+
self.balance[account - 1] -= money;
327+
true
328+
}
329+
}
330+
331+
/**
332+
* Your Bank object will be instantiated and called as such:
333+
* let obj = Bank::new(balance);
334+
* let ret_1: bool = obj.transfer(account1, account2, money);
335+
* let ret_2: bool = obj.deposit(account, money);
336+
* let ret_3: bool = obj.withdraw(account, money);
337+
*/
338+
```
339+
280340
### **...**
281341

282342
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
struct Bank {
2+
balance: Vec<i64>,
3+
}
4+
5+
/**
6+
* `&self` means the method takes an immutable reference.
7+
* If you need a mutable reference, change it to `&mut self` instead.
8+
*/
9+
impl Bank {
10+
fn new(balance: Vec<i64>) -> Self {
11+
Bank { balance }
12+
}
13+
14+
fn transfer(&mut self, account1: i32, account2: i32, money: i64) -> bool {
15+
let (account1, account2, n) = (account1 as usize, account2 as usize, self.balance.len());
16+
if n < account1 || n < account2 {
17+
return false;
18+
}
19+
if self.balance[account1 - 1] < money {
20+
return false;
21+
}
22+
self.balance[account1 - 1] -= money;
23+
self.balance[account2 - 1] += money;
24+
true
25+
}
26+
27+
fn deposit(&mut self, account: i32, money: i64) -> bool {
28+
let (account, n) = (account as usize, self.balance.len());
29+
if n < account {
30+
return false;
31+
}
32+
self.balance[account - 1] += money;
33+
true
34+
}
35+
36+
fn withdraw(&mut self, account: i32, money: i64) -> bool {
37+
let (account, n) = (account as usize, self.balance.len());
38+
if n < account {
39+
return false;
40+
}
41+
if self.balance[account - 1] < money {
42+
return false;
43+
}
44+
self.balance[account - 1] -= money;
45+
true
46+
}
47+
}
48+
49+
/**
50+
* Your Bank object will be instantiated and called as such:
51+
* let obj = Bank::new(balance);
52+
* let ret_1: bool = obj.transfer(account1, account2, money);
53+
* let ret_2: bool = obj.deposit(account, money);
54+
* let ret_3: bool = obj.withdraw(account, money);
55+
*/

0 commit comments

Comments
 (0)
Please sign in to comment.