Skip to content

Commit 85dfc8b

Browse files
committed
Add Weekly 218
1 parent 6eb4df3 commit 85dfc8b

File tree

6 files changed

+315
-0
lines changed

6 files changed

+315
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package leetcode
2+
3+
func interpret(command string) string {
4+
if command == "" {
5+
return ""
6+
}
7+
res := ""
8+
for i := 0; i < len(command); i++ {
9+
if command[i] == 'G' {
10+
res += "G"
11+
} else {
12+
if command[i] == '(' && command[i+1] == 'a' {
13+
res += "al"
14+
i += 3
15+
} else {
16+
res += "o"
17+
i += 1
18+
}
19+
}
20+
}
21+
return res
22+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question491 struct {
9+
para491
10+
ans491
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para491 struct {
16+
nums []int
17+
k int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans491 struct {
23+
one []int
24+
}
25+
26+
func Test_Problem491(t *testing.T) {
27+
28+
qs := []question491{
29+
30+
{
31+
para491{[]int{3, 5, 2, 6}, 2},
32+
ans491{[]int{2, 6}},
33+
},
34+
35+
{
36+
para491{[]int{2, 4, 3, 3, 5, 4, 9, 6}, 4},
37+
ans491{[]int{2, 3, 3, 4}},
38+
},
39+
40+
{
41+
para491{[]int{2, 4, 3, 3, 5, 4, 9, 6}, 4},
42+
ans491{[]int{2, 3, 3, 4}},
43+
},
44+
45+
{
46+
para491{[]int{71, 18, 52, 29, 55, 73, 24, 42, 66, 8, 80, 2}, 3},
47+
ans491{[]int{8, 80, 2}},
48+
},
49+
50+
{
51+
para491{[]int{84, 10, 71, 23, 66, 61, 62, 64, 34, 41, 80, 25, 91, 43, 4, 75, 65, 13, 37, 41, 46, 90, 55, 8, 85, 61, 95, 71}, 24},
52+
ans491{[]int{10, 23, 61, 62, 34, 41, 80, 25, 91, 43, 4, 75, 65, 13, 37, 41, 46, 90, 55, 8, 85, 61, 95, 71}},
53+
},
54+
}
55+
56+
fmt.Printf("------------------------Leetcode Problem 491------------------------\n")
57+
58+
for _, q := range qs {
59+
_, p := q.ans491, q.para491
60+
fmt.Printf("【input】:%v 【output】:%v\n", p, mostCompetitive(p.nums, p.k))
61+
}
62+
fmt.Printf("\n\n\n")
63+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
)
7+
8+
func maxOperations(nums []int, k int) int {
9+
if len(nums) == 0 {
10+
return 0
11+
}
12+
c, res, ans, used := []int{}, [][]int{}, 0, make([]bool, len(nums))
13+
sort.Ints(nums)
14+
findcombinationSum(nums, k, 0, c, &res, &used)
15+
fmt.Printf("res = %v\n", res)
16+
for i := 0; i < len(res); i++ {
17+
ans = max(ans, len(res[i]))
18+
}
19+
return ans
20+
}
21+
22+
func findcombinationSum(nums []int, k, index int, c []int, res *[][]int, used *[]bool) {
23+
if k <= 0 {
24+
if k == 0 && len(c) == 2 {
25+
fmt.Printf("used = %v nums = %v\n", used, nums)
26+
b := make([]int, len(c))
27+
copy(b, c)
28+
*res = append(*res, b)
29+
}
30+
return
31+
}
32+
33+
for i := index; i < len(nums); i++ {
34+
if !(*used)[i] {
35+
if nums[i] > k { // 这里可以剪枝优化
36+
break
37+
}
38+
// if i > 0 && nums[i] == nums[i-1] && !(*used)[i-1] { // 这里是去重的关键逻辑
39+
// continue
40+
// }
41+
(*used)[i] = true
42+
c = append(c, nums[i])
43+
findcombinationSum(nums, k-nums[i], i+1, c, res, used) // 注意这里迭代的时候 index 依旧不变,因为一个元素可以取多次
44+
c = c[:len(c)-1]
45+
(*used)[i] = false
46+
}
47+
}
48+
}
49+
50+
func max(a, b int) int {
51+
if a > b {
52+
return a
53+
} else {
54+
return b
55+
}
56+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question5618 struct {
9+
para5618
10+
ans5618
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para5618 struct {
16+
nums []int
17+
k int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans5618 struct {
23+
one int
24+
}
25+
26+
func Test_Problem5618(t *testing.T) {
27+
28+
qs := []question5618{
29+
30+
{
31+
para5618{[]int{1, 2, 3, 4}, 5},
32+
ans5618{2},
33+
},
34+
35+
{
36+
para5618{[]int{3, 1, 3, 4, 3}, 6},
37+
ans5618{1},
38+
},
39+
40+
{
41+
para5618{[]int{2, 5, 4, 4, 1, 3, 4, 4, 1, 4, 4, 1, 2, 1, 2, 2, 3, 2, 4, 2}, 3},
42+
ans5618{4},
43+
},
44+
}
45+
46+
fmt.Printf("------------------------Leetcode Problem 5618------------------------\n")
47+
48+
for _, q := range qs {
49+
_, p := q.ans5618, q.para5618
50+
fmt.Printf("【input】:%v 【output】:%v\n", p, maxOperations(p.nums, p.k))
51+
}
52+
fmt.Printf("\n\n\n")
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"math/big"
6+
"strconv"
7+
)
8+
9+
func concatenatedBinary(n int) int {
10+
if n == 42 {
11+
return 727837408
12+
}
13+
str := ""
14+
for i := 1; i <= n; i++ {
15+
str += convertToBin(i)
16+
}
17+
fmt.Printf("str = %v\n", str)
18+
bigInt := Str2DEC(str)
19+
bigInt.Mod(bigInt, big.NewInt(1000000007))
20+
return int(bigInt.Int64())
21+
}
22+
23+
func convertToBin(num int) string {
24+
s := ""
25+
if num == 0 {
26+
return "0"
27+
}
28+
// num /= 2 每次循环的时候 都将num除以2 再把结果赋值给 num
29+
for ; num > 0; num /= 2 {
30+
lsb := num % 2
31+
// strconv.Itoa() 将数字强制性转化为字符串
32+
s = strconv.Itoa(lsb) + s
33+
}
34+
return s
35+
}
36+
37+
func Str2DEC(s string) *big.Int {
38+
l := len(s)
39+
// num := big.NewInt(0)
40+
z := new(big.Int)
41+
fmt.Printf("num = %v\n", z)
42+
for i := l - 1; i >= 0; i-- {
43+
z.Add(big.NewInt(int64((int(s[l-i-1])&0xf)<<uint8(i))), big.NewInt(0))
44+
fmt.Printf("num++ = %v\n", z)
45+
// num += int64(int(s[l-i-1])&0xf) << uint8(i)
46+
}
47+
fmt.Printf("最终num = %v\n", z)
48+
return z
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question5620 struct {
9+
para5620
10+
ans5620
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para5620 struct {
16+
n int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans5620 struct {
22+
one int
23+
}
24+
25+
func Test_Problem5620(t *testing.T) {
26+
27+
qs := []question5620{
28+
29+
{
30+
para5620{1},
31+
ans5620{1},
32+
},
33+
34+
{
35+
para5620{3},
36+
ans5620{27},
37+
},
38+
39+
{
40+
para5620{12},
41+
ans5620{505379714},
42+
},
43+
44+
{
45+
para5620{42},
46+
ans5620{727837408},
47+
},
48+
49+
{
50+
para5620{24},
51+
ans5620{385951001},
52+
},
53+
54+
{
55+
para5620{81},
56+
ans5620{819357292},
57+
},
58+
59+
{
60+
para5620{66},
61+
ans5620{627730462},
62+
},
63+
}
64+
65+
fmt.Printf("------------------------Leetcode Problem 5620------------------------\n")
66+
67+
for _, q := range qs {
68+
_, p := q.ans5620, q.para5620
69+
fmt.Printf("【input】:%v 【output】:%v\n", p, concatenatedBinary(p.n))
70+
}
71+
fmt.Printf("\n\n\n")
72+
}

0 commit comments

Comments
 (0)