mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-25 03:11:41 +08:00
Add Biweekly 39 / weekly 215 solutions
This commit is contained in:
56
leetcode/5550/1652. Defuse the Bomb.go
Normal file
56
leetcode/5550/1652. Defuse the Bomb.go
Normal file
@ -0,0 +1,56 @@
|
||||
package leetcode
|
||||
|
||||
func decrypt(code []int, k int) []int {
|
||||
if k == 0 {
|
||||
for i := 0; i < len(code); i++ {
|
||||
code[i] = 0
|
||||
}
|
||||
return code
|
||||
}
|
||||
count, sum, res := k, 0, make([]int, len(code))
|
||||
if k > 0 {
|
||||
for i := 0; i < len(code); i++ {
|
||||
for j := i + 1; j < len(code); j++ {
|
||||
if count == 0 {
|
||||
break
|
||||
}
|
||||
sum += code[j]
|
||||
count--
|
||||
}
|
||||
if count > 0 {
|
||||
for j := 0; j < len(code); j++ {
|
||||
if count == 0 {
|
||||
break
|
||||
}
|
||||
sum += code[j]
|
||||
count--
|
||||
}
|
||||
}
|
||||
res[i] = sum
|
||||
sum, count = 0, k
|
||||
}
|
||||
}
|
||||
if k < 0 {
|
||||
for i := 0; i < len(code); i++ {
|
||||
for j := i - 1; j >= 0; j-- {
|
||||
if count == 0 {
|
||||
break
|
||||
}
|
||||
sum += code[j]
|
||||
count++
|
||||
}
|
||||
if count < 0 {
|
||||
for j := len(code) - 1; j >= 0; j-- {
|
||||
if count == 0 {
|
||||
break
|
||||
}
|
||||
sum += code[j]
|
||||
count++
|
||||
}
|
||||
}
|
||||
res[i] = sum
|
||||
sum, count = 0, k
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
53
leetcode/5550/1652. Defuse the Bomb_test.go
Normal file
53
leetcode/5550/1652. Defuse the Bomb_test.go
Normal file
@ -0,0 +1,53 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1652 struct {
|
||||
para1652
|
||||
ans1652
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1652 struct {
|
||||
code []int
|
||||
k int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1652 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
func Test_Problem1652(t *testing.T) {
|
||||
|
||||
qs := []question1652{
|
||||
|
||||
{
|
||||
para1652{[]int{5, 7, 1, 4}, 3},
|
||||
ans1652{[]int{12, 10, 16, 13}},
|
||||
},
|
||||
|
||||
{
|
||||
para1652{[]int{1, 2, 3, 4}, 0},
|
||||
ans1652{[]int{0, 0, 0, 0}},
|
||||
},
|
||||
|
||||
{
|
||||
para1652{[]int{2, 4, 9, 3}, -2},
|
||||
ans1652{[]int{12, 5, 6, 13}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1652------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1652, q.para1652
|
||||
fmt.Printf("【input】:%v 【output】:%v \n", p, decrypt(p.code, p.k))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
Reference in New Issue
Block a user