Skip to content

Commit 91e1a92

Browse files
committed
Add Biweekly 39 / weekly 215 solutions
1 parent 7d7007c commit 91e1a92

8 files changed

+434
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package leetcode
2+
3+
func decrypt(code []int, k int) []int {
4+
if k == 0 {
5+
for i := 0; i < len(code); i++ {
6+
code[i] = 0
7+
}
8+
return code
9+
}
10+
count, sum, res := k, 0, make([]int, len(code))
11+
if k > 0 {
12+
for i := 0; i < len(code); i++ {
13+
for j := i + 1; j < len(code); j++ {
14+
if count == 0 {
15+
break
16+
}
17+
sum += code[j]
18+
count--
19+
}
20+
if count > 0 {
21+
for j := 0; j < len(code); j++ {
22+
if count == 0 {
23+
break
24+
}
25+
sum += code[j]
26+
count--
27+
}
28+
}
29+
res[i] = sum
30+
sum, count = 0, k
31+
}
32+
}
33+
if k < 0 {
34+
for i := 0; i < len(code); i++ {
35+
for j := i - 1; j >= 0; j-- {
36+
if count == 0 {
37+
break
38+
}
39+
sum += code[j]
40+
count++
41+
}
42+
if count < 0 {
43+
for j := len(code) - 1; j >= 0; j-- {
44+
if count == 0 {
45+
break
46+
}
47+
sum += code[j]
48+
count++
49+
}
50+
}
51+
res[i] = sum
52+
sum, count = 0, k
53+
}
54+
}
55+
return res
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 question1652 struct {
9+
para1652
10+
ans1652
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1652 struct {
16+
code []int
17+
k int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans1652 struct {
23+
one []int
24+
}
25+
26+
func Test_Problem1652(t *testing.T) {
27+
28+
qs := []question1652{
29+
30+
{
31+
para1652{[]int{5, 7, 1, 4}, 3},
32+
ans1652{[]int{12, 10, 16, 13}},
33+
},
34+
35+
{
36+
para1652{[]int{1, 2, 3, 4}, 0},
37+
ans1652{[]int{0, 0, 0, 0}},
38+
},
39+
40+
{
41+
para1652{[]int{2, 4, 9, 3}, -2},
42+
ans1652{[]int{12, 5, 6, 13}},
43+
},
44+
}
45+
46+
fmt.Printf("------------------------Leetcode Problem 1652------------------------\n")
47+
48+
for _, q := range qs {
49+
_, p := q.ans1652, q.para1652
50+
fmt.Printf("【input】:%v 【output】:%v \n", p, decrypt(p.code, p.k))
51+
}
52+
fmt.Printf("\n\n\n")
53+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package leetcode
2+
3+
func minimumDeletions(s string) int {
4+
ai, bi, sum, temp, array := 0, 0, 0, 0, []int{}
5+
for ai = 0; ai < len(s); ai++ {
6+
if s[ai] == 'a' {
7+
break
8+
}
9+
}
10+
if ai != 0 && ai != len(s) {
11+
sum += ai
12+
}
13+
for bi = ai; bi < len(s); bi++ {
14+
if s[bi] == 'b' {
15+
break
16+
}
17+
}
18+
if s[bi-1] == 'a' {
19+
ai = bi - 1
20+
}
21+
if s[bi-1] == 'b' && bi != len(s) {
22+
ai = bi + 1
23+
}
24+
for j := bi; j < len(s); j++ {
25+
if s[j] == 'b' {
26+
temp++
27+
}
28+
if s[j] == 'a' && temp != 0 {
29+
array = append(array, temp)
30+
temp = 0
31+
}
32+
}
33+
if len(array) == 0 {
34+
return sum
35+
}
36+
dp := make([]int, len(array))
37+
dp[0] = min(array[0], len(array))
38+
for i := 1; i < len(array); i++ {
39+
dp[i] = min(dp[i-1]+array[i], dp[i-1]+len(array)-(i+1)+1)
40+
}
41+
return sum + dp[len(array)-1]
42+
}
43+
44+
func min(a int, b int) int {
45+
if a > b {
46+
return b
47+
}
48+
return a
49+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1649 struct {
9+
para1649
10+
ans1649
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1649 struct {
16+
s string
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1649 struct {
22+
one int
23+
}
24+
25+
func Test_Problem1649(t *testing.T) {
26+
27+
qs := []question1649{
28+
29+
// {
30+
// para1649{"aababbab"},
31+
// ans1649{2},
32+
// },
33+
34+
// {
35+
// para1649{"bbaaaaabb"},
36+
// ans1649{2},
37+
// },
38+
39+
{
40+
para1649{"b"},
41+
ans1649{0},
42+
},
43+
44+
{
45+
para1649{"ababaaaabbbbbaaababbbbbbaaabbaababbabbbbaabbbbaabbabbabaabbbababaa"},
46+
ans1649{25},
47+
},
48+
}
49+
50+
fmt.Printf("------------------------Leetcode Problem 1649------------------------\n")
51+
52+
for _, q := range qs {
53+
_, p := q.ans1649, q.para1649
54+
fmt.Printf("【input】:%v 【output】:%v \n", p, minimumDeletions(p.s))
55+
}
56+
fmt.Printf("\n\n\n")
57+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type OrderedStream struct {
8+
ptr int
9+
stream []string
10+
}
11+
12+
func Constructor(n int) OrderedStream {
13+
ptr, stream := 1, make([]string, n+1)
14+
return OrderedStream{ptr: ptr, stream: stream}
15+
}
16+
17+
func (this *OrderedStream) Insert(id int, value string) []string {
18+
this.stream[id] = value
19+
res := []string{}
20+
fmt.Printf("%v %v %v\n", this.ptr, id, value)
21+
if this.ptr == id || this.stream[this.ptr] != "" {
22+
res = append(res, this.stream[this.ptr])
23+
for i := id + 1; i < len(this.stream); i++ {
24+
if this.stream[i] != "" {
25+
res = append(res, this.stream[i])
26+
} else {
27+
this.ptr = i
28+
return res
29+
}
30+
}
31+
}
32+
if len(res) > 0 {
33+
return res
34+
}
35+
return []string{}
36+
}
37+
38+
/**
39+
* Your OrderedStream object will be instantiated and called as such:
40+
* obj := Constructor(n);
41+
* param_1 := obj.Insert(id,value);
42+
*/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func Test_Problem707(t *testing.T) {
9+
obj := Constructor(5)
10+
fmt.Printf("obj = %v\n", obj)
11+
param1 := obj.Insert(3, "ccccc")
12+
fmt.Printf("param_1 = %v obj = %v\n", param1, obj)
13+
param1 = obj.Insert(1, "aaaaa")
14+
fmt.Printf("param_1 = %v obj = %v\n", param1, obj)
15+
param1 = obj.Insert(2, "bbbbb")
16+
fmt.Printf("param_1 = %v obj = %v\n", param1, obj)
17+
param1 = obj.Insert(5, "eeeee")
18+
fmt.Printf("param_1 = %v obj = %v\n", param1, obj)
19+
param1 = obj.Insert(4, "ddddd")
20+
fmt.Printf("param_1 = %v obj = %v\n", param1, obj)
21+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package leetcode
2+
3+
import (
4+
"sort"
5+
)
6+
7+
func closeStrings(word1 string, word2 string) bool {
8+
if len(word1) != len(word2) {
9+
return false
10+
}
11+
freqWord1, freq1, freqList1, freqWord2, freq2, freqList2, flag := map[byte]int{}, []int{}, map[int][]byte{}, map[byte]int{}, []int{}, map[int][]byte{}, false
12+
for i := 0; i < len(word1); i++ {
13+
freqWord1[word1[i]]++
14+
}
15+
for i := 0; i < len(word2); i++ {
16+
freqWord2[word2[i]]++
17+
}
18+
freqTemp1 := map[int]int{}
19+
for k, v := range freqWord1 {
20+
freqTemp1[v]++
21+
if list, ok := freqList1[v]; ok {
22+
list = append(list, k)
23+
freqList1[v] = list
24+
} else {
25+
list := []byte{}
26+
list = append(list, k)
27+
freqList1[v] = list
28+
}
29+
}
30+
for _, v := range freqTemp1 {
31+
freq1 = append(freq1, v)
32+
}
33+
freqTemp2 := map[int]int{}
34+
for k, v := range freqWord2 {
35+
freqTemp2[v]++
36+
if list, ok := freqList2[v]; ok {
37+
list = append(list, k)
38+
freqList2[v] = list
39+
} else {
40+
list := []byte{}
41+
list = append(list, k)
42+
freqList2[v] = list
43+
}
44+
}
45+
for _, v := range freqTemp2 {
46+
freq2 = append(freq2, v)
47+
}
48+
if len(freq1) != len(freq2) {
49+
return false
50+
}
51+
sort.Ints(freq1)
52+
sort.Ints(freq2)
53+
for i := 0; i < len(freq1); i++ {
54+
if freq1[i] != freq2[i] {
55+
flag = true
56+
break
57+
}
58+
}
59+
if flag == true {
60+
return false
61+
}
62+
flag = false
63+
// 频次相同,再判断字母交换是否合法存在
64+
for k, v := range freqWord1 {
65+
if list, ok := freqList2[v]; ok {
66+
for i := 0; i < len(list); i++ {
67+
if list[i] != k && list[i] != '0' {
68+
// 交换的字母不存在
69+
if _, ok := freqWord1[list[i]]; !ok {
70+
flag = true
71+
break
72+
} else {
73+
// 交换的字母存在,重置这一位,代表这一个字母被交换了,下次不用它
74+
list[i] = '0'
75+
}
76+
}
77+
}
78+
} else {
79+
// 出现频次个数相同,但是频次不同
80+
flag = true
81+
break
82+
}
83+
}
84+
if flag == true {
85+
return false
86+
}
87+
return true
88+
}

0 commit comments

Comments
 (0)