mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-04 16:12:47 +08:00
Update README
This commit is contained in:
@ -81,5 +81,5 @@ func nextGreaterElements1(nums []int) []int {
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0500.Keyboard-Row/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0506.Relative-Ranks/">下一页➡️</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0504.Base-7/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
67
website/content/ChapterFour/0500~0599/0504.Base-7.md
Normal file
67
website/content/ChapterFour/0500~0599/0504.Base-7.md
Normal file
@ -0,0 +1,67 @@
|
||||
# [504. Base 7](https://leetcode.com/problems/base-7/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given an integer num, return a string of its base 7 representation.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
Input: num = 100
|
||||
Output: "202"
|
||||
|
||||
**Example 2:**
|
||||
|
||||
Input: num = -7
|
||||
Output: "-10"
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- -10000000 <= num <= 10000000
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个整数 num,将其转化为 7 进制,并以字符串形式输出。
|
||||
|
||||
## 解题思路
|
||||
|
||||
num反复除以7,然后倒排余数
|
||||
|
||||
# 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
import "strconv"
|
||||
|
||||
func convertToBase7(num int) string {
|
||||
if num == 0 {
|
||||
return "0"
|
||||
}
|
||||
negative := false
|
||||
if num < 0 {
|
||||
negative = true
|
||||
num = -num
|
||||
}
|
||||
var ans string
|
||||
var nums []int
|
||||
for num != 0 {
|
||||
remainder := num % 7
|
||||
nums = append(nums, remainder)
|
||||
num = num / 7
|
||||
}
|
||||
if negative {
|
||||
ans += "-"
|
||||
}
|
||||
for i := len(nums) - 1; i >= 0; i-- {
|
||||
ans += strconv.Itoa(nums[i])
|
||||
}
|
||||
return ans
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0503.Next-Greater-Element-II/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0506.Relative-Ranks/">下一页➡️</a></p>
|
||||
</div>
|
@ -86,6 +86,6 @@ func findRelativeRanks(score []int) []string {
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0503.Next-Greater-Element-II/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0504.Base-7/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0507.Perfect-Number/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
@ -113,6 +113,7 @@ func dfs(board [][]byte, x, y int) {
|
||||
nx, ny := x+dir8[i][0], y+dir8[i][1]
|
||||
if isInBoard(board, nx, ny) && board[nx][ny] == 'M' {
|
||||
cnt++
|
||||
|
||||
}
|
||||
}
|
||||
if cnt > 0 {
|
||||
@ -133,6 +134,7 @@ func isInBoard(board [][]byte, x, y int) bool {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0528.Random-Pick-with-Weight/">⬅️上一页</a></p>
|
||||
|
@ -189,5 +189,5 @@ func isLowerLetter(v byte) bool {
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0725.Split-Linked-List-in-Parts/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0729.My-Calendar-I/">下一页➡️</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0728.Self-Dividing-Numbers/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
@ -0,0 +1,75 @@
|
||||
# [728. Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/)
|
||||
|
||||
## 题目
|
||||
|
||||
A self-dividing number is a number that is divisible by every digit it contains.
|
||||
|
||||
- For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.
|
||||
|
||||
A self-dividing number is not allowed to contain the digit zero.
|
||||
|
||||
Given two integers left and right, return a list of all the self-dividing numbers in the range [left, right].
|
||||
|
||||
**Example 1:**
|
||||
|
||||
Input: left = 1, right = 22
|
||||
Output: [1,2,3,4,5,6,7,8,9,11,12,15,22]
|
||||
|
||||
**Example 2:**
|
||||
|
||||
Input: left = 47, right = 85
|
||||
Output: [48,55,66,77]
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- 1 <= left <= right <= 10000
|
||||
|
||||
## 题目大意
|
||||
|
||||
自除数是指可以被它包含的每一位数整除的数。
|
||||
|
||||
- 例如,128 是一个 自除数 ,因为 128 % 1 == 0,128 % 2 == 0,128 % 8 == 0。
|
||||
|
||||
自除数 不允许包含 0 。
|
||||
|
||||
给定两个整数 left 和 right ,返回一个列表,列表的元素是范围 [left, right] 内所有的 自除数 。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 模拟计算
|
||||
|
||||
# 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func selfDividingNumbers(left int, right int) []int {
|
||||
var ans []int
|
||||
for num := left; num <= right; num++ {
|
||||
if selfDividingNum(num) {
|
||||
ans = append(ans, num)
|
||||
}
|
||||
}
|
||||
return ans
|
||||
}
|
||||
|
||||
func selfDividingNum(num int) bool {
|
||||
for d := num; d > 0; d = d / 10 {
|
||||
reminder := d % 10
|
||||
if reminder == 0 {
|
||||
return false
|
||||
}
|
||||
if num%reminder != 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0726.Number-of-Atoms/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0729.My-Calendar-I/">下一页➡️</a></p>
|
||||
</div>
|
@ -182,6 +182,6 @@ func (this *MyCalendar) Book(start int, end int) bool {
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0726.Number-of-Atoms/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0728.Self-Dividing-Numbers/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0732.My-Calendar-III/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
@ -80,5 +80,5 @@ func construct2DArray(original []int, m int, n int) [][]int {
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1900~1999/1984.Minimum-Difference-Between-Highest-and-Lowest-of-K-Scores/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/2100~2199/2164.Sort-Even-and-Odd-Indices-Independently/">下一页➡️</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/2000~2099/2037.Minimum-Number-of-Moves-to-Seat-Everyone/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
@ -0,0 +1,101 @@
|
||||
# [2037. Minimum Number of Moves to Seat Everyone](https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/)
|
||||
|
||||
## 题目
|
||||
|
||||
There are n seats and n students in a room. You are given an array seats of length n, where seats[i] is the position of the ith seat. You are also given the array students of length n, where students[j] is the position of the jth student.
|
||||
|
||||
You may perform the following move any number of times:
|
||||
|
||||
- Increase or decrease the position of the ith student by 1 (i.e., moving the ith student from position x to x + 1 or x - 1)
|
||||
|
||||
Return the minimum number of moves required to move each student to a seat such that no two students are in the same seat.
|
||||
|
||||
Note that there may be multiple seats or students in the same position at the beginning.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
Input: seats = [3,1,5], students = [2,7,4]
|
||||
Output: 4
|
||||
Explanation: The students are moved as follows:
|
||||
- The first student is moved from from position 2 to position 1 using 1 move.
|
||||
- The second student is moved from from position 7 to position 5 using 2 moves.
|
||||
- The third student is moved from from position 4 to position 3 using 1 move.
|
||||
In total, 1 + 2 + 1 = 4 moves were used.
|
||||
|
||||
**Example 2:**
|
||||
|
||||
Input: seats = [4,1,5,9], students = [1,3,2,6]
|
||||
Output: 7
|
||||
Explanation: The students are moved as follows:
|
||||
- The first student is not moved.
|
||||
- The second student is moved from from position 3 to position 4 using 1 move.
|
||||
- The third student is moved from from position 2 to position 5 using 3 moves.
|
||||
- The fourth student is moved from from position 6 to position 9 using 3 moves.
|
||||
In total, 0 + 1 + 3 + 3 = 7 moves were used.
|
||||
|
||||
**Example 3:**
|
||||
|
||||
Input: seats = [2,2,6,6], students = [1,3,2,6]
|
||||
Output: 4
|
||||
Explanation: Note that there are two seats at position 2 and two seats at position 6.
|
||||
The students are moved as follows:
|
||||
- The first student is moved from from position 1 to position 2 using 1 move.
|
||||
- The second student is moved from from position 3 to position 6 using 3 moves.
|
||||
- The third student is not moved.
|
||||
- The fourth student is not moved.
|
||||
In total, 1 + 3 + 0 + 0 = 4 moves were used.
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- n == seats.length == students.length
|
||||
- 1 <= n <= 100
|
||||
- 1 <= seats[i], students[j] <= 100
|
||||
|
||||
## 题目大意
|
||||
|
||||
一个房间里有 n 个座位和 n 名学生,房间用一个数轴表示。给你一个长度为 n 的数组 seats,其中 seats[i] 是第 i 个座位的位置。同时给你一个长度为 n 的数组 students ,其中 students[j] 是第 j 位学生的位置。
|
||||
|
||||
你可以执行以下操作任意次:
|
||||
|
||||
增加或者减少第 i 位学生的位置,每次变化量为 1(也就是将第 i 位学生从位置 x 移动到 x + 1或者 x - 1)
|
||||
|
||||
请你返回使所有学生都有座位坐的最少移动次数,并确保没有两位学生的座位相同。
|
||||
|
||||
请注意,初始时有可能有多个座位或者多位学生在 同一位置。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 排序+模拟计算
|
||||
|
||||
# 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
import "sort"
|
||||
|
||||
func minMovesToSeat(seats []int, students []int) int {
|
||||
sort.Ints(seats)
|
||||
sort.Ints(students)
|
||||
n := len(students)
|
||||
moves := 0
|
||||
for i := 0; i < n; i++ {
|
||||
moves += abs(seats[i], students[i])
|
||||
}
|
||||
return moves
|
||||
}
|
||||
|
||||
func abs(a, b int) int {
|
||||
if a > b {
|
||||
return a - b
|
||||
}
|
||||
return b - a
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/2000~2099/2022.Convert-1D-Array-Into-2D-Array/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/2000~2099/2038.Remove-Colored-Pieces-if-Both-Neighbors-are-the-Same-Color/">下一页➡️</a></p>
|
||||
</div>
|
@ -0,0 +1,112 @@
|
||||
# [2038. Remove Colored Pieces if Both Neighbors are the Same Color](https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/)
|
||||
|
||||
## 题目
|
||||
|
||||
There are n pieces arranged in a line, and each piece is colored either by 'A' or by 'B'. You are given a string colors of length n where colors[i] is the color of the ith piece.
|
||||
|
||||
Alice and Bob are playing a game where they take alternating turns removing pieces from the line. In this game, Alice moves first.
|
||||
|
||||
- Alice is only allowed to remove a piece colored 'A' if both its neighbors are also colored 'A'. She is not allowed to remove pieces that are colored 'B'.
|
||||
- Bob is only allowed to remove a piece colored 'B' if both its neighbors are also colored 'B'. He is not allowed to remove pieces that are colored 'A'.
|
||||
- Alice and Bob cannot remove pieces from the edge of the line.
|
||||
- If a player cannot make a move on their turn, that player loses and the other player wins.
|
||||
|
||||
Assuming Alice and Bob play optimally, return true if Alice wins, or return false if Bob wins.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
Input: colors = "AAABABB"
|
||||
Output: true
|
||||
Explanation:
|
||||
AAABABB -> AABABB
|
||||
Alice moves first.
|
||||
She removes the second 'A' from the left since that is the only 'A' whose neighbors are both 'A'.
|
||||
|
||||
Now it's Bob's turn.
|
||||
Bob cannot make a move on his turn since there are no 'B's whose neighbors are both 'B'.
|
||||
Thus, Alice wins, so return true.
|
||||
|
||||
**Example 2:**
|
||||
|
||||
Input: colors = "AA"
|
||||
Output: false
|
||||
Explanation:
|
||||
Alice has her turn first.
|
||||
There are only two 'A's and both are on the edge of the line, so she cannot move on her turn.
|
||||
Thus, Bob wins, so return false.
|
||||
|
||||
**Example 3:**
|
||||
|
||||
Input: colors = "ABBBBBBBAAA"
|
||||
Output: false
|
||||
Explanation:
|
||||
ABBBBBBBAAA -> ABBBBBBBAA
|
||||
Alice moves first.
|
||||
Her only option is to remove the second to last 'A' from the right.
|
||||
|
||||
ABBBBBBBAA -> ABBBBBBAA
|
||||
Next is Bob's turn.
|
||||
He has many options for which 'B' piece to remove. He can pick any.
|
||||
|
||||
On Alice's second turn, she has no more pieces that she can remove.
|
||||
Thus, Bob wins, so return false.
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- 1 <= colors.length <= 100000
|
||||
- colors consists of only the letters 'A' and 'B'
|
||||
|
||||
## 题目大意
|
||||
|
||||
总共有 n 个颜色片段排成一列,每个颜色片段要么是 'A' 要么是 'B' 。给你一个长度为 n 的字符串 colors ,其中 colors[i] 表示第 i 个颜色片段的颜色。
|
||||
|
||||
Alice 和 Bob 在玩一个游戏,他们轮流从这个字符串中删除颜色。Alice 先手。
|
||||
|
||||
- 如果一个颜色片段为 'A' 且相邻两个颜色都是颜色 'A',那么 Alice 可以删除该颜色片段。Alice不可以删除任何颜色 'B' 片段。
|
||||
- 如果一个颜色片段为 'B'且相邻两个颜色都是颜色 'B' ,那么 Bob 可以删除该颜色片段。Bob 不可以删除任何颜色 'A' 片段。
|
||||
- Alice 和 Bob 不能从字符串两端删除颜色片段。
|
||||
- 如果其中一人无法继续操作,则该玩家 输掉游戏且另一玩家 获胜。
|
||||
|
||||
假设 Alice 和 Bob 都采用最优策略,如果 Alice 获胜,请返回true,否则 Bob 获胜,返回false。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 统计 Alice 和 Bob 分别可以操作的次数记为 As,Bs
|
||||
- 因为 Alice 先手,所以只要 As 大于 Bs,Alice 获胜返回 true,否则 Bob 获胜返回 false
|
||||
|
||||
# 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func winnerOfGame(colors string) bool {
|
||||
As, Bs := 0, 0
|
||||
Acont, Bcont := 0, 0
|
||||
for _, color := range colors {
|
||||
if color == 'A' {
|
||||
Acont += 1
|
||||
Bcont = 0
|
||||
} else {
|
||||
Bcont += 1
|
||||
Acont = 0
|
||||
}
|
||||
if Acont >= 3 {
|
||||
As += Acont - 2
|
||||
}
|
||||
if Bcont >= 3 {
|
||||
Bs += Bcont - 2
|
||||
}
|
||||
}
|
||||
if As > Bs {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/2000~2099/2037.Minimum-Number-of-Moves-to-Seat-Everyone/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/2000~2099/2043.Simple-Bank-System/">下一页➡️</a></p>
|
||||
</div>
|
120
website/content/ChapterFour/2000~2099/2043.Simple-Bank-System.md
Normal file
120
website/content/ChapterFour/2000~2099/2043.Simple-Bank-System.md
Normal file
@ -0,0 +1,120 @@
|
||||
# [2043. Simple Bank System](https://leetcode.com/problems/simple-bank-system/)
|
||||
|
||||
## 题目
|
||||
|
||||
You have been tasked with writing a program for a popular bank that will automate all its incoming transactions (transfer, deposit, and withdraw). The bank has n accounts numbered from 1 to n. The initial balance of each account is stored in a 0-indexed integer array balance, with the (i + 1)th account having an initial balance of balance[i].
|
||||
|
||||
Execute all the valid transactions. A transaction is valid if:
|
||||
|
||||
- The given account number(s) are between 1 and n, and
|
||||
- The amount of money withdrawn or transferred from is less than or equal to the balance of the account.
|
||||
|
||||
Implement the Bank class:
|
||||
|
||||
- Bank(long[] balance) Initializes the object with the 0-indexed integer array balance.
|
||||
- boolean transfer(int account1, int account2, long money) Transfers money dollars from the account numbered account1 to the account numbered account2. Return true if the transaction was successful, false otherwise.
|
||||
- boolean deposit(int account, long money) Deposit money dollars into the account numbered account. Return true if the transaction was successful, false otherwise.
|
||||
- boolean withdraw(int account, long money) Withdraw money dollars from the account numbered account. Return true if the transaction was successful, false otherwise.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
Input
|
||||
["Bank", "withdraw", "transfer", "deposit", "transfer", "withdraw"]
|
||||
[[[10, 100, 20, 50, 30]], [3, 10], [5, 1, 20], [5, 20], [3, 4, 15], [10, 50]]
|
||||
Output
|
||||
[null, true, true, true, false, false]
|
||||
|
||||
Explanation
|
||||
Bank bank = new Bank([10, 100, 20, 50, 30]);
|
||||
bank.withdraw(3, 10); // return true, account 3 has a balance of $20, so it is valid to withdraw $10.
|
||||
// Account 3 has $20 - $10 = $10.
|
||||
bank.transfer(5, 1, 20); // return true, account 5 has a balance of $30, so it is valid to transfer $20.
|
||||
// Account 5 has $30 - $20 = $10, and account 1 has $10 + $20 = $30.
|
||||
bank.deposit(5, 20); // return true, it is valid to deposit $20 to account 5.
|
||||
// Account 5 has $10 + $20 = $30.
|
||||
bank.transfer(3, 4, 15); // return false, the current balance of account 3 is $10,
|
||||
// so it is invalid to transfer $15 from it.
|
||||
bank.withdraw(10, 50); // return false, it is invalid because account 10 does not exist.
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- n == balance.length
|
||||
- 1 <= n, account, account1, account2 <= 100000
|
||||
- 0 <= balance[i], money <= 1000000000000
|
||||
- At most 104 calls will be made to each function transfer, deposit, withdraw.
|
||||
|
||||
## 题目大意
|
||||
|
||||
你的任务是为一个很受欢迎的银行设计一款程序,以自动化执行所有传入的交易(转账,存款和取款)。银行共有 n 个账户,编号从 1 到 n 。每个账号的初始余额存储在一个下标从 0 开始的整数数组 balance 中,其中第 (i + 1) 个账户的初始余额是 balance[i] 。
|
||||
|
||||
请你执行所有 有效的 交易。如果满足下面全部条件,则交易 有效 :
|
||||
|
||||
- 指定的账户数量在 1 和 n 之间,且
|
||||
- 取款或者转账需要的钱的总数 小于或者等于 账户余额。
|
||||
|
||||
实现 Bank 类:
|
||||
|
||||
- Bank(long[] balance) 使用下标从 0 开始的整数数组 balance 初始化该对象。
|
||||
- boolean transfer(int account1, int account2, long money) 从编号为 account1 的账户向编号为 account2 的账户转帐 money 美元。如果交易成功,返回 true ,否则,返回 false 。
|
||||
- boolean deposit(int account, long money) 向编号为 account 的账户存款 money 美元。如果交易成功,返回 true ;否则,返回 false 。
|
||||
- boolean withdraw(int account, long money) 从编号为 account 的账户取款 money 美元。如果交易成功,返回 true ;否则,返回 false 。
|
||||
|
||||
## 解题思路
|
||||
|
||||
根据题意进行简单模拟
|
||||
|
||||
# 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
type Bank struct {
|
||||
accounts []int64
|
||||
n int
|
||||
}
|
||||
|
||||
func Constructor(balance []int64) Bank {
|
||||
return Bank{
|
||||
accounts: balance,
|
||||
n: len(balance),
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Bank) Transfer(account1 int, account2 int, money int64) bool {
|
||||
if account1 > this.n || account2 > this.n {
|
||||
return false
|
||||
}
|
||||
if this.accounts[account1-1] < money {
|
||||
return false
|
||||
}
|
||||
this.accounts[account1-1] -= money
|
||||
this.accounts[account2-1] += money
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Bank) Deposit(account int, money int64) bool {
|
||||
if account > this.n {
|
||||
return false
|
||||
}
|
||||
this.accounts[account-1] += money
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Bank) Withdraw(account int, money int64) bool {
|
||||
if account > this.n {
|
||||
return false
|
||||
}
|
||||
if this.accounts[account-1] < money {
|
||||
return false
|
||||
}
|
||||
this.accounts[account-1] -= money
|
||||
return true
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/2000~2099/2038.Remove-Colored-Pieces-if-Both-Neighbors-are-the-Same-Color/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/2100~2199/2164.Sort-Even-and-Odd-Indices-Independently/">下一页➡️</a></p>
|
||||
</div>
|
@ -95,6 +95,6 @@ func sortEvenOdd(nums []int) []int {
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/2000~2099/2022.Convert-1D-Array-Into-2D-Array/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/2000~2099/2043.Simple-Bank-System/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/2100~2199/2165.Smallest-Value-of-the-Rearranged-Number/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
Reference in New Issue
Block a user