forked from halfrost/LeetCode-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,076 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ func interpret(command string) string { | |
i += 3 | ||
} else { | ||
res += "o" | ||
i += 1 | ||
i++ | ||
} | ||
} | ||
} | ||
|
52 changes: 52 additions & 0 deletions
52
leetcode/1678.Goal-Parser-Interpretation/1678. Goal Parser Interpretation_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package leetcode | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
type question1678 struct { | ||
para1678 | ||
ans1678 | ||
} | ||
|
||
// para 是参数 | ||
// one 代表第一个参数 | ||
type para1678 struct { | ||
command string | ||
} | ||
|
||
// ans 是答案 | ||
// one 代表第一个答案 | ||
type ans1678 struct { | ||
one string | ||
} | ||
|
||
func Test_Problem1678(t *testing.T) { | ||
|
||
qs := []question1678{ | ||
|
||
{ | ||
para1678{"G()(al)"}, | ||
ans1678{"Goal"}, | ||
}, | ||
|
||
{ | ||
para1678{"G()()()()(al)"}, | ||
ans1678{"Gooooal"}, | ||
}, | ||
|
||
{ | ||
para1678{"(al)G(al)()()G"}, | ||
ans1678{"alGalooG"}, | ||
}, | ||
} | ||
|
||
fmt.Printf("------------------------Leetcode Problem 1678------------------------\n") | ||
|
||
for _, q := range qs { | ||
_, p := q.ans1678, q.para1678 | ||
fmt.Printf("【input】:%v 【output】:%v\n", p, interpret(p.command)) | ||
} | ||
fmt.Printf("\n\n\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# [1678. Goal Parser Interpretation](https://leetcode.com/problems/goal-parser-interpretation/) | ||
|
||
## 题目 | ||
|
||
You own a **Goal Parser** that can interpret a string `command`. The `command` consists of an alphabet of `"G"`, `"()"` and/or `"(al)"` in some order. The Goal Parser will interpret `"G"` as the string `"G"`, `"()"` as the string `"o"`, and `"(al)"` as the string `"al"`. The interpreted strings are then concatenated in the original order. | ||
|
||
Given the string `command`, return *the **Goal Parser**'s interpretation of* `command`. | ||
|
||
**Example 1:** | ||
|
||
``` | ||
Input: command = "G()(al)" | ||
Output: "Goal" | ||
Explanation: The Goal Parser interprets the command as follows: | ||
G -> G | ||
() -> o | ||
(al) -> al | ||
The final concatenated result is "Goal". | ||
``` | ||
|
||
**Example 2:** | ||
|
||
``` | ||
Input: command = "G()()()()(al)" | ||
Output: "Gooooal" | ||
``` | ||
|
||
**Example 3:** | ||
|
||
``` | ||
Input: command = "(al)G(al)()()G" | ||
Output: "alGalooG" | ||
``` | ||
|
||
**Constraints:** | ||
|
||
- `1 <= command.length <= 100` | ||
- `command` consists of `"G"`, `"()"`, and/or `"(al)"` in some order. | ||
|
||
## 题目大意 | ||
|
||
请你设计一个可以解释字符串 command 的 Goal 解析器 。command 由 "G"、"()" 和/或 "(al)" 按某种顺序组成。Goal 解析器会将 "G" 解释为字符串 "G"、"()" 解释为字符串 "o" ,"(al)" 解释为字符串 "al" 。然后,按原顺序将经解释得到的字符串连接成一个字符串。给你字符串 command ,返回 Goal 解析器 对 command 的解释结果。 | ||
|
||
## 解题思路 | ||
|
||
- 简单题,按照题意修改字符串即可。由于是简单题,这一题也不用考虑嵌套的情况。 | ||
|
||
## 代码 | ||
|
||
```go | ||
package leetcode | ||
|
||
func interpret(command string) string { | ||
if command == "" { | ||
return "" | ||
} | ||
res := "" | ||
for i := 0; i < len(command); i++ { | ||
if command[i] == 'G' { | ||
res += "G" | ||
} else { | ||
if command[i] == '(' && command[i+1] == 'a' { | ||
res += "al" | ||
i += 3 | ||
} else { | ||
res += "o" | ||
i ++ | ||
} | ||
} | ||
} | ||
return res | ||
} | ||
``` |
48 changes: 48 additions & 0 deletions
48
leetcode/1679.Max-Number-of-K-Sum-Pairs/1679. Max Number of K-Sum Pairs.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package leetcode | ||
|
||
// 解法一 优化版 | ||
func maxOperations(nums []int, k int) int { | ||
counter, res := make(map[int]int), 0 | ||
for _, n := range nums { | ||
counter[n]++ | ||
} | ||
if (k & 1) == 0 { | ||
res += counter[k>>1] >> 1 | ||
// 能够由 2 个相同的数构成 k 的组合已经都排除出去了,剩下的一个单独的也不能组成 k 了 | ||
// 所以这里要把它的频次置为 0 。如果这里不置为 0,下面代码判断逻辑还需要考虑重复使用数字的情况 | ||
counter[k>>1] = 0 | ||
} | ||
for num, freq := range counter { | ||
if num <= k/2 { | ||
remain := k - num | ||
if counter[remain] < freq { | ||
res += counter[remain] | ||
} else { | ||
res += freq | ||
} | ||
} | ||
} | ||
return res | ||
} | ||
|
||
// 解法二 | ||
func maxOperations_(nums []int, k int) int { | ||
counter, res := make(map[int]int), 0 | ||
for _, num := range nums { | ||
counter[num]++ | ||
remain := k - num | ||
if num == remain { | ||
if counter[num] >= 2 { | ||
res++ | ||
counter[num] -= 2 | ||
} | ||
} else { | ||
if counter[remain] > 0 { | ||
res++ | ||
counter[remain]-- | ||
counter[num]-- | ||
} | ||
} | ||
} | ||
return res | ||
} |
58 changes: 58 additions & 0 deletions
58
leetcode/1679.Max-Number-of-K-Sum-Pairs/1679. Max Number of K-Sum Pairs_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package leetcode | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
type question1679 struct { | ||
para1679 | ||
ans1679 | ||
} | ||
|
||
// para 是参数 | ||
// one 代表第一个参数 | ||
type para1679 struct { | ||
nums []int | ||
k int | ||
} | ||
|
||
// ans 是答案 | ||
// one 代表第一个答案 | ||
type ans1679 struct { | ||
one int | ||
} | ||
|
||
func Test_Problem1679(t *testing.T) { | ||
|
||
qs := []question1679{ | ||
|
||
{ | ||
para1679{[]int{1, 2, 3, 4}, 5}, | ||
ans1679{2}, | ||
}, | ||
|
||
{ | ||
para1679{[]int{3, 1, 3, 4, 3}, 6}, | ||
ans1679{1}, | ||
}, | ||
|
||
{ | ||
para1679{[]int{2, 5, 4, 4, 1, 3, 4, 4, 1, 4, 4, 1, 2, 1, 2, 2, 3, 2, 4, 2}, 3}, | ||
ans1679{4}, | ||
}, | ||
|
||
{ | ||
para1679{[]int{2, 5, 5, 5, 1, 3, 4, 4, 1, 4, 4, 1, 3, 1, 3, 1, 3, 2, 4, 2}, 6}, | ||
ans1679{8}, | ||
}, | ||
} | ||
|
||
fmt.Printf("------------------------Leetcode Problem 1679------------------------\n") | ||
|
||
for _, q := range qs { | ||
_, p := q.ans1679, q.para1679 | ||
fmt.Printf("【input】:%v 【output】:%v\n", p, maxOperations(p.nums, p.k)) | ||
} | ||
fmt.Printf("\n\n\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# [1679. Max Number of K-Sum Pairs](https://leetcode.com/problems/max-number-of-k-sum-pairs/) | ||
|
||
|
||
## 题目 | ||
|
||
You are given an integer array `nums` and an integer `k`. | ||
|
||
In one operation, you can pick two numbers from the array whose sum equals `k` and remove them from the array. | ||
|
||
Return *the maximum number of operations you can perform on the array*. | ||
|
||
**Example 1:** | ||
|
||
``` | ||
Input: nums = [1,2,3,4], k = 5 | ||
Output: 2 | ||
Explanation: Starting with nums = [1,2,3,4]: | ||
- Remove numbers 1 and 4, then nums = [2,3] | ||
- Remove numbers 2 and 3, then nums = [] | ||
There are no more pairs that sum up to 5, hence a total of 2 operations. | ||
``` | ||
|
||
**Example 2:** | ||
|
||
``` | ||
Input: nums = [3,1,3,4,3], k = 6 | ||
Output: 1 | ||
Explanation: Starting with nums = [3,1,3,4,3]: | ||
- Remove the first two 3's, then nums = [1,4,3] | ||
There are no more pairs that sum up to 6, hence a total of 1 operation. | ||
``` | ||
|
||
**Constraints:** | ||
|
||
- `1 <= nums.length <= 105` | ||
- `1 <= nums[i] <= 109` | ||
- `1 <= k <= 109` | ||
|
||
## 题目大意 | ||
|
||
给你一个整数数组 nums 和一个整数 k 。每一步操作中,你需要从数组中选出和为 k 的两个整数,并将它们移出数组。返回你可以对数组执行的最大操作数。 | ||
|
||
## 解题思路 | ||
|
||
- 读完题第一感觉这道题是 TWO SUM 题目的加强版。需要找到所有满足和是 k 的数对。先考虑能不能找到两个数都是 k/2 ,如果能找到多个这样的数,可以先移除他们。其次在利用 TWO SUM 的思路,找出和为 k 的数对。利用 TWO SUM 里面 map 的做法,时间复杂度 O(n)。 | ||
|
||
## 代码 | ||
|
||
```go | ||
package leetcode | ||
|
||
// 解法一 优化版 | ||
func maxOperations(nums []int, k int) int { | ||
counter, res := make(map[int]int), 0 | ||
for _, n := range nums { | ||
counter[n]++ | ||
} | ||
if (k & 1) == 0 { | ||
res += counter[k>>1] >> 1 | ||
// 能够由 2 个相同的数构成 k 的组合已经都排除出去了,剩下的一个单独的也不能组成 k 了 | ||
// 所以这里要把它的频次置为 0 。如果这里不置为 0,下面代码判断逻辑还需要考虑重复使用数字的情况 | ||
counter[k>>1] = 0 | ||
} | ||
for num, freq := range counter { | ||
if num <= k/2 { | ||
remain := k - num | ||
if counter[remain] < freq { | ||
res += counter[remain] | ||
} else { | ||
res += freq | ||
} | ||
} | ||
} | ||
return res | ||
} | ||
|
||
// 解法二 | ||
func maxOperations_(nums []int, k int) int { | ||
counter, res := make(map[int]int), 0 | ||
for _, num := range nums { | ||
counter[num]++ | ||
remain := k - num | ||
if num == remain { | ||
if counter[num] >= 2 { | ||
res++ | ||
counter[num] -= 2 | ||
} | ||
} else { | ||
if counter[remain] > 0 { | ||
res++ | ||
counter[remain]-- | ||
counter[num]-- | ||
} | ||
} | ||
} | ||
return res | ||
} | ||
``` |
26 changes: 26 additions & 0 deletions
26
...nation-of-Consecutive-Binary-Numbers/1680. Concatenation of Consecutive Binary Numbers.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package leetcode | ||
|
||
import ( | ||
"math/bits" | ||
) | ||
|
||
// 解法一 模拟 | ||
func concatenatedBinary(n int) int { | ||
res, mod, shift := 0, 1000000007, 0 | ||
for i := 1; i <= n; i++ { | ||
if (i & (i - 1)) == 0 { | ||
shift++ | ||
} | ||
res = ((res << shift) + i) % mod | ||
} | ||
return res | ||
} | ||
|
||
// 解法二 位运算 | ||
func concatenatedBinary1(n int) int { | ||
res := 0 | ||
for i := 1; i <= n; i++ { | ||
res = (res<<bits.Len(uint(i)) | i) % (1e9 + 7) | ||
} | ||
return res | ||
} |
File renamed without changes.
Oops, something went wrong.