restructure solutions dir
This commit is contained in:
parent
f9ddad5f88
commit
ccb8b5673b
10 changed files with 0 additions and 0 deletions
26
solutions/2/q209/solution.go
Normal file
26
solutions/2/q209/solution.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package q209
|
||||
|
||||
import "math"
|
||||
|
||||
func minSubArrayLen(target int, nums []int) int {
|
||||
minLen := math.MaxInt
|
||||
|
||||
l, sum := 0, 0
|
||||
for r := range nums {
|
||||
sum += nums[r]
|
||||
for l < r && sum-nums[l] >= target {
|
||||
sum -= nums[l]
|
||||
l++
|
||||
}
|
||||
if sum >= target {
|
||||
minLen = min(minLen, r-l+1)
|
||||
}
|
||||
}
|
||||
|
||||
if minLen == math.MaxInt {
|
||||
return 0
|
||||
}
|
||||
return minLen
|
||||
}
|
||||
|
||||
var _ = minSubArrayLen
|
||||
24
solutions/2/q238/solution.go
Normal file
24
solutions/2/q238/solution.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package q238
|
||||
|
||||
func productExceptSelf(nums []int) []int {
|
||||
ret := make([]int, len(nums))
|
||||
|
||||
// from left
|
||||
t := 1
|
||||
for i := range len(nums) - 1 {
|
||||
t *= nums[i]
|
||||
ret[i+1] = t
|
||||
}
|
||||
ret[0] = 1
|
||||
|
||||
// from right
|
||||
t = 1
|
||||
for i := len(nums) - 1; i > 0; i-- {
|
||||
t *= nums[i]
|
||||
ret[i-1] *= t
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
var _ = productExceptSelf
|
||||
32
solutions/2/q238/solution_test.go
Normal file
32
solutions/2/q238/solution_test.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package q238
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_productExceptSelf(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string // description of this test case
|
||||
// Named input parameters for target function.
|
||||
nums []int
|
||||
want []int
|
||||
}{
|
||||
{
|
||||
nums: []int{1, 2, 3, 4},
|
||||
want: []int{24, 12, 8, 6},
|
||||
},
|
||||
{
|
||||
nums: []int{-1, 1, 0, -3, 3},
|
||||
want: []int{0, 0, 9, 0, 0},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := productExceptSelf(tt.nums)
|
||||
if !slices.Equal(got, tt.want) {
|
||||
t.Errorf("productExceptSelf() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue