Skip to content

Commit fbecc2d

Browse files
committed
chore: update typescript solution for 2043. Simple Bank System
1 parent 07c4985 commit fbecc2d

2 files changed

Lines changed: 71 additions & 3 deletions

File tree

docs/LeetCode/2043. Simple Bank System/README.mdx

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import Tabs from "@theme/Tabs";
2626
import TabItem from "@theme/TabItem";
2727
import CodeBlock from "@theme/CodeBlock";
2828
import DifficultyBadge from "@site/src/components/Badges/DifficultyBadge";
29-
import Solution from "!!raw-loader!./solution.js";
29+
import SolutionJs from "!!raw-loader!./solution.js";
30+
import SolutionTs from "!!raw-loader!./solution.ts";
3031
import Description from "./_Description.md";
3132
import Examples from "./_Examples.md";
3233

@@ -43,7 +44,15 @@ import Examples from "./_Examples.md";
4344

4445
<TabItem value="solution" label="解答">
4546
## Solution
46-
<CodeBlock language="js">{Solution}</CodeBlock>
47+
<Tabs>
48+
<TabItem value="js" label="JavaScript" default>
49+
<CodeBlock language="js">{SolutionJs}</CodeBlock>
50+
</TabItem>
51+
<TabItem value="ts" label="TypeScript">
52+
<CodeBlock language="ts">{SolutionTs}</CodeBlock>
53+
</TabItem>
54+
</Tabs>
55+
4756
</TabItem>
4857
</Tabs>
4958

@@ -101,4 +110,19 @@ Bank.prototype.withdraw = function (account, money) {
101110

102111
## 心得
103112

104-
酷酷物件導向
113+
酷酷物件導向
114+
因為物件導向很適合用來練習 TypeScript
115+
所以也另外寫了 TS 版本的解答。
116+
跟 JS 的邏輯是一樣的,只是變為使用 class 來實作,以及在初始化時候包含 `balance``n` 的型別來避免 IDE 報錯
117+
118+
```ts
119+
class Bank {
120+
public balance: number[];
121+
public n: number;
122+
123+
constructor(balance: number[]) {
124+
this.balance = balance;
125+
this.n = balance.length;
126+
}
127+
}
128+
```
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Bank {
2+
public balance: number[];
3+
public n: number;
4+
5+
constructor(balance: number[]) {
6+
this.balance = balance;
7+
this.n = balance.length;
8+
}
9+
10+
transfer(account1: number, account2: number, money: number): boolean {
11+
if (account1 > this.n || account2 > this.n) return false;
12+
if (money > this.balance[account1 - 1]) return false;
13+
14+
this.balance[account1 - 1] -= money;
15+
this.balance[account2 - 1] += money;
16+
17+
return true;
18+
}
19+
20+
deposit(account: number, money: number): boolean {
21+
if (account > this.n) return false;
22+
23+
this.balance[account - 1] += money;
24+
25+
return true;
26+
}
27+
28+
withdraw(account: number, money: number): boolean {
29+
if (account > this.n) return false;
30+
if (money > this.balance[account - 1]) return false;
31+
32+
this.balance[account - 1] -= money;
33+
34+
return true;
35+
}
36+
}
37+
38+
/**
39+
* Your Bank object will be instantiated and called as such:
40+
* var obj = new Bank(balance)
41+
* var param_1 = obj.transfer(account1,account2,money)
42+
* var param_2 = obj.deposit(account,money)
43+
* var param_3 = obj.withdraw(account,money)
44+
*/

0 commit comments

Comments
 (0)