diff --git "a/lcof/\351\235\242\350\257\225\351\242\23034. \344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23034. \344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204/README.md" index 67635eab1ce2d..3d7bfb705ffb3 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23034. \344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23034. \344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204/README.md" @@ -193,9 +193,7 @@ func pathSum(root *TreeNode, target int) (ans [][]int) { t = append(t, root.Val) s -= root.Val if root.Left == nil && root.Right == nil && s == 0 { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) } dfs(root.Left, s) dfs(root.Right, s) diff --git "a/lcof/\351\235\242\350\257\225\351\242\23034. \344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204/Solution.go" "b/lcof/\351\235\242\350\257\225\351\242\23034. \344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204/Solution.go" index 6e28d151125a4..eb289b001a3ab 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23034. \344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204/Solution.go" +++ "b/lcof/\351\235\242\350\257\225\351\242\23034. \344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204/Solution.go" @@ -16,9 +16,7 @@ func pathSum(root *TreeNode, target int) (ans [][]int) { t = append(t, root.Val) s -= root.Val if root.Left == nil && root.Right == nil && s == 0 { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) } dfs(root.Left, s) dfs(root.Right, s) diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 079. \346\211\200\346\234\211\345\255\220\351\233\206/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 079. \346\211\200\346\234\211\345\255\220\351\233\206/README.md" index 335053fab3391..5bdec237ee130 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 079. \346\211\200\346\234\211\345\255\220\351\233\206/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 079. \346\211\200\346\234\211\345\255\220\351\233\206/README.md" @@ -144,9 +144,7 @@ func subsets(nums []int) [][]int { } func dfs(i int, nums, t []int, res *[][]int) { - cp := make([]int, len(t)) - copy(cp, t) - *res = append(*res, cp) + *res = append(*res, slices.Clone(t)) if i == len(nums) { return } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 079. \346\211\200\346\234\211\345\255\220\351\233\206/Solution.go" "b/lcof2/\345\211\221\346\214\207 Offer II 079. \346\211\200\346\234\211\345\255\220\351\233\206/Solution.go" index 2862a6a40172d..58d9f88a2ee45 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 079. \346\211\200\346\234\211\345\255\220\351\233\206/Solution.go" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 079. \346\211\200\346\234\211\345\255\220\351\233\206/Solution.go" @@ -6,9 +6,7 @@ func subsets(nums []int) [][]int { } func dfs(i int, nums, t []int, res *[][]int) { - cp := make([]int, len(t)) - copy(cp, t) - *res = append(*res, cp) + *res = append(*res, slices.Clone(t)) if i == len(nums) { return } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 080. \345\220\253\346\234\211 k \344\270\252\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 080. \345\220\253\346\234\211 k \344\270\252\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/README.md" index d42ad90f017fa..c9b320813e7cf 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 080. \345\220\253\346\234\211 k \344\270\252\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 080. \345\220\253\346\234\211 k \344\270\252\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/README.md" @@ -135,9 +135,7 @@ func combine(n int, k int) [][]int { func dfs(i, n, k int, t []int, res *[][]int) { if len(t) == k { - cp := make([]int, k) - copy(cp, t) - *res = append(*res, cp) + *res = append(*res, slices.Clone(t)) return } for j := i; j <= n; j++ { diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 080. \345\220\253\346\234\211 k \344\270\252\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/Solution.go" "b/lcof2/\345\211\221\346\214\207 Offer II 080. \345\220\253\346\234\211 k \344\270\252\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/Solution.go" index 6b8cdc89e03ce..8bbb927196e04 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 080. \345\220\253\346\234\211 k \344\270\252\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/Solution.go" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 080. \345\220\253\346\234\211 k \344\270\252\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/Solution.go" @@ -7,9 +7,7 @@ func combine(n int, k int) [][]int { func dfs(i, n, k int, t []int, res *[][]int) { if len(t) == k { - cp := make([]int, k) - copy(cp, t) - *res = append(*res, cp) + *res = append(*res, slices.Clone(t)) return } for j := i; j <= n; j++ { diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 082. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\347\273\204\345\220\210/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 082. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\347\273\204\345\220\210/README.md" index 3dd5d167f51e8..82890959b19f3 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 082. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\347\273\204\345\220\210/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 082. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\347\273\204\345\220\210/README.md" @@ -174,9 +174,7 @@ func combinationSum2(candidates []int, target int) [][]int { return } if s == target { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) return } for i := u; i < len(candidates); i++ { diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 082. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\347\273\204\345\220\210/Solution.go" "b/lcof2/\345\211\221\346\214\207 Offer II 082. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\347\273\204\345\220\210/Solution.go" index f03b1c015a089..8be311e01f1c0 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 082. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\347\273\204\345\220\210/Solution.go" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 082. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\347\273\204\345\220\210/Solution.go" @@ -8,9 +8,7 @@ func combinationSum2(candidates []int, target int) [][]int { return } if s == target { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) return } for i := u; i < len(candidates); i++ { diff --git a/solution/0000-0099/0039.Combination Sum/README.md b/solution/0000-0099/0039.Combination Sum/README.md index 4fb390624681e..99dfdcfbdd70c 100644 --- a/solution/0000-0099/0039.Combination Sum/README.md +++ b/solution/0000-0099/0039.Combination Sum/README.md @@ -247,9 +247,7 @@ func combinationSum(candidates []int, target int) (ans [][]int) { var dfs func(i, s int) dfs = func(i, s int) { if s == 0 { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) return } if s < candidates[i] { @@ -273,9 +271,7 @@ func combinationSum(candidates []int, target int) (ans [][]int) { var dfs func(i, s int) dfs = func(i, s int) { if s == 0 { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) return } if i >= len(candidates) || s < candidates[i] { diff --git a/solution/0000-0099/0039.Combination Sum/README_EN.md b/solution/0000-0099/0039.Combination Sum/README_EN.md index 866b695870a0c..9c95b0aa203b5 100644 --- a/solution/0000-0099/0039.Combination Sum/README_EN.md +++ b/solution/0000-0099/0039.Combination Sum/README_EN.md @@ -239,9 +239,7 @@ func combinationSum(candidates []int, target int) (ans [][]int) { var dfs func(i, s int) dfs = func(i, s int) { if s == 0 { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) return } if s < candidates[i] { @@ -265,9 +263,7 @@ func combinationSum(candidates []int, target int) (ans [][]int) { var dfs func(i, s int) dfs = func(i, s int) { if s == 0 { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) return } if i >= len(candidates) || s < candidates[i] { diff --git a/solution/0000-0099/0039.Combination Sum/Solution.go b/solution/0000-0099/0039.Combination Sum/Solution.go index fc30931a6ead6..d290d36a348d9 100644 --- a/solution/0000-0099/0039.Combination Sum/Solution.go +++ b/solution/0000-0099/0039.Combination Sum/Solution.go @@ -4,9 +4,7 @@ func combinationSum(candidates []int, target int) (ans [][]int) { var dfs func(i, s int) dfs = func(i, s int) { if s == 0 { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) return } if i >= len(candidates) || s < candidates[i] { diff --git a/solution/0000-0099/0040.Combination Sum II/README.md b/solution/0000-0099/0040.Combination Sum II/README.md index b819266debcd2..53d15433dea76 100644 --- a/solution/0000-0099/0040.Combination Sum II/README.md +++ b/solution/0000-0099/0040.Combination Sum II/README.md @@ -264,9 +264,7 @@ func combinationSum2(candidates []int, target int) (ans [][]int) { var dfs func(i, s int) dfs = func(i, s int) { if s == 0 { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) return } if i >= len(candidates) || s < candidates[i] { @@ -293,22 +291,20 @@ func combinationSum2(candidates []int, target int) (ans [][]int) { var dfs func(i, s int) dfs = func(i, s int) { if s == 0 { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) return } if i >= len(candidates) || s < candidates[i] { return } - x := candidates[i] - t = append(t, x) - dfs(i+1, s-x) - t = t[:len(t)-1] - for i < len(candidates) && candidates[i] == x { - i++ + for j := i; j < len(candidates); j++ { + if j > i && candidates[j] == candidates[j-1] { + continue + } + t = append(t, candidates[j]) + dfs(j+1, s-candidates[j]) + t = t[:len(t)-1] } - dfs(i, s) } dfs(0, target) return diff --git a/solution/0000-0099/0040.Combination Sum II/README_EN.md b/solution/0000-0099/0040.Combination Sum II/README_EN.md index f615feabe1e1d..a7ad541c7da24 100644 --- a/solution/0000-0099/0040.Combination Sum II/README_EN.md +++ b/solution/0000-0099/0040.Combination Sum II/README_EN.md @@ -256,9 +256,7 @@ func combinationSum2(candidates []int, target int) (ans [][]int) { var dfs func(i, s int) dfs = func(i, s int) { if s == 0 { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) return } if i >= len(candidates) || s < candidates[i] { @@ -285,22 +283,20 @@ func combinationSum2(candidates []int, target int) (ans [][]int) { var dfs func(i, s int) dfs = func(i, s int) { if s == 0 { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) return } if i >= len(candidates) || s < candidates[i] { return } - x := candidates[i] - t = append(t, x) - dfs(i+1, s-x) - t = t[:len(t)-1] - for i < len(candidates) && candidates[i] == x { - i++ + for j := i; j < len(candidates); j++ { + if j > i && candidates[j] == candidates[j-1] { + continue + } + t = append(t, candidates[j]) + dfs(j+1, s-candidates[j]) + t = t[:len(t)-1] } - dfs(i, s) } dfs(0, target) return diff --git a/solution/0000-0099/0040.Combination Sum II/Solution.go b/solution/0000-0099/0040.Combination Sum II/Solution.go index 86a8af5a365ec..35a320d0d6913 100644 --- a/solution/0000-0099/0040.Combination Sum II/Solution.go +++ b/solution/0000-0099/0040.Combination Sum II/Solution.go @@ -4,9 +4,7 @@ func combinationSum2(candidates []int, target int) (ans [][]int) { var dfs func(i, s int) dfs = func(i, s int) { if s == 0 { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) return } if i >= len(candidates) || s < candidates[i] { diff --git a/solution/0000-0099/0046.Permutations/README.md b/solution/0000-0099/0046.Permutations/README.md index 3b0d2631888fd..faba7e6394bd5 100644 --- a/solution/0000-0099/0046.Permutations/README.md +++ b/solution/0000-0099/0046.Permutations/README.md @@ -188,9 +188,7 @@ func permute(nums []int) (ans [][]int) { var dfs func(int) dfs = func(i int) { if i == n { - cp := make([]int, n) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) return } for j, v := range nums { diff --git a/solution/0000-0099/0046.Permutations/README_EN.md b/solution/0000-0099/0046.Permutations/README_EN.md index e7624a7d18deb..1a417c4931394 100644 --- a/solution/0000-0099/0046.Permutations/README_EN.md +++ b/solution/0000-0099/0046.Permutations/README_EN.md @@ -167,9 +167,7 @@ func permute(nums []int) (ans [][]int) { var dfs func(int) dfs = func(i int) { if i == n { - cp := make([]int, n) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) return } for j, v := range nums { diff --git a/solution/0000-0099/0046.Permutations/Solution.go b/solution/0000-0099/0046.Permutations/Solution.go index decfdf2ea184e..2856587a3065b 100644 --- a/solution/0000-0099/0046.Permutations/Solution.go +++ b/solution/0000-0099/0046.Permutations/Solution.go @@ -5,9 +5,7 @@ func permute(nums []int) (ans [][]int) { var dfs func(int) dfs = func(i int) { if i == n { - cp := make([]int, n) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) return } for j, v := range nums { diff --git a/solution/0000-0099/0047.Permutations II/README.md b/solution/0000-0099/0047.Permutations II/README.md index 77b37cd61305d..00466d20e2500 100644 --- a/solution/0000-0099/0047.Permutations II/README.md +++ b/solution/0000-0099/0047.Permutations II/README.md @@ -168,9 +168,7 @@ func permuteUnique(nums []int) (ans [][]int) { var dfs func(int) dfs = func(i int) { if i == n { - cp := make([]int, n) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) return } for j := 0; j < n; j++ { diff --git a/solution/0000-0099/0047.Permutations II/README_EN.md b/solution/0000-0099/0047.Permutations II/README_EN.md index 0fc514335c30c..b6618ab4b679c 100644 --- a/solution/0000-0099/0047.Permutations II/README_EN.md +++ b/solution/0000-0099/0047.Permutations II/README_EN.md @@ -158,9 +158,7 @@ func permuteUnique(nums []int) (ans [][]int) { var dfs func(int) dfs = func(i int) { if i == n { - cp := make([]int, n) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) return } for j := 0; j < n; j++ { diff --git a/solution/0000-0099/0047.Permutations II/Solution.go b/solution/0000-0099/0047.Permutations II/Solution.go index 3a5f327dc242f..4ecb9325786e8 100644 --- a/solution/0000-0099/0047.Permutations II/Solution.go +++ b/solution/0000-0099/0047.Permutations II/Solution.go @@ -6,9 +6,7 @@ func permuteUnique(nums []int) (ans [][]int) { var dfs func(int) dfs = func(i int) { if i == n { - cp := make([]int, n) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) return } for j := 0; j < n; j++ { diff --git a/solution/0000-0099/0077.Combinations/README.md b/solution/0000-0099/0077.Combinations/README.md index 1a44ac73f0538..3053c9255e6fc 100644 --- a/solution/0000-0099/0077.Combinations/README.md +++ b/solution/0000-0099/0077.Combinations/README.md @@ -239,9 +239,7 @@ func combine(n int, k int) (ans [][]int) { var dfs func(int) dfs = func(i int) { if len(t) == k { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) return } if i > n { @@ -263,9 +261,7 @@ func combine(n int, k int) (ans [][]int) { var dfs func(int) dfs = func(i int) { if len(t) == k { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) return } if i > n { diff --git a/solution/0000-0099/0077.Combinations/README_EN.md b/solution/0000-0099/0077.Combinations/README_EN.md index eecb9b404b245..235fcfe8e3bb3 100644 --- a/solution/0000-0099/0077.Combinations/README_EN.md +++ b/solution/0000-0099/0077.Combinations/README_EN.md @@ -226,9 +226,7 @@ func combine(n int, k int) (ans [][]int) { var dfs func(int) dfs = func(i int) { if len(t) == k { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) return } if i > n { @@ -250,9 +248,7 @@ func combine(n int, k int) (ans [][]int) { var dfs func(int) dfs = func(i int) { if len(t) == k { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) return } if i > n { diff --git a/solution/0000-0099/0077.Combinations/Solution.go b/solution/0000-0099/0077.Combinations/Solution.go index 134e6930b778c..ec14537d8d15e 100644 --- a/solution/0000-0099/0077.Combinations/Solution.go +++ b/solution/0000-0099/0077.Combinations/Solution.go @@ -3,9 +3,7 @@ func combine(n int, k int) (ans [][]int) { var dfs func(int) dfs = func(i int) { if len(t) == k { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) return } if i > n { diff --git a/solution/0100-0199/0113.Path Sum II/README.md b/solution/0100-0199/0113.Path Sum II/README.md index 8f5e9e900a4f2..c4246c1029fe6 100644 --- a/solution/0100-0199/0113.Path Sum II/README.md +++ b/solution/0100-0199/0113.Path Sum II/README.md @@ -189,9 +189,7 @@ func pathSum(root *TreeNode, targetSum int) (ans [][]int) { s -= root.Val t = append(t, root.Val) if root.Left == nil && root.Right == nil && s == 0 { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) } dfs(root.Left, s) dfs(root.Right, s) diff --git a/solution/0100-0199/0113.Path Sum II/README_EN.md b/solution/0100-0199/0113.Path Sum II/README_EN.md index bc2ffb562085b..887efcdeb6ee9 100644 --- a/solution/0100-0199/0113.Path Sum II/README_EN.md +++ b/solution/0100-0199/0113.Path Sum II/README_EN.md @@ -178,9 +178,7 @@ func pathSum(root *TreeNode, targetSum int) (ans [][]int) { s -= root.Val t = append(t, root.Val) if root.Left == nil && root.Right == nil && s == 0 { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) } dfs(root.Left, s) dfs(root.Right, s) diff --git a/solution/0100-0199/0113.Path Sum II/Solution.go b/solution/0100-0199/0113.Path Sum II/Solution.go index 3773923f67a4a..4835eaa2180e1 100644 --- a/solution/0100-0199/0113.Path Sum II/Solution.go +++ b/solution/0100-0199/0113.Path Sum II/Solution.go @@ -16,9 +16,7 @@ func pathSum(root *TreeNode, targetSum int) (ans [][]int) { s -= root.Val t = append(t, root.Val) if root.Left == nil && root.Right == nil && s == 0 { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) } dfs(root.Left, s) dfs(root.Right, s) diff --git a/solution/0200-0299/0216.Combination Sum III/README.md b/solution/0200-0299/0216.Combination Sum III/README.md index 62bf67e391af9..bff5cee6a4e65 100644 --- a/solution/0200-0299/0216.Combination Sum III/README.md +++ b/solution/0200-0299/0216.Combination Sum III/README.md @@ -376,9 +376,7 @@ func combinationSum3(k int, n int) (ans [][]int) { dfs = func(i, s int) { if s == 0 { if len(t) == k { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) } return } @@ -402,9 +400,7 @@ func combinationSum3(k int, n int) (ans [][]int) { dfs = func(i, s int) { if s == 0 { if len(t) == k { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) } return } diff --git a/solution/0200-0299/0216.Combination Sum III/README_EN.md b/solution/0200-0299/0216.Combination Sum III/README_EN.md index ceee2b355b273..dd1c840610a55 100644 --- a/solution/0200-0299/0216.Combination Sum III/README_EN.md +++ b/solution/0200-0299/0216.Combination Sum III/README_EN.md @@ -333,9 +333,7 @@ func combinationSum3(k int, n int) (ans [][]int) { dfs = func(i, s int) { if s == 0 { if len(t) == k { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) } return } @@ -359,13 +357,11 @@ func combinationSum3(k int, n int) (ans [][]int) { dfs = func(i, s int) { if s == 0 { if len(t) == k { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) } return } - if i > 9 || i > s || len(t) > k { + if i > 9 || i > s || len(t) >= k { return } for j := i; j <= 9; j++ { diff --git a/solution/0200-0299/0216.Combination Sum III/Solution.go b/solution/0200-0299/0216.Combination Sum III/Solution.go index 89951f01ee70f..5c686f42ddff4 100644 --- a/solution/0200-0299/0216.Combination Sum III/Solution.go +++ b/solution/0200-0299/0216.Combination Sum III/Solution.go @@ -4,9 +4,7 @@ func combinationSum3(k int, n int) (ans [][]int) { dfs = func(i, s int) { if s == 0 { if len(t) == k { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) } return } diff --git a/solution/0200-0299/0254.Factor Combinations/README.md b/solution/0200-0299/0254.Factor Combinations/README.md index 2d4f20b95869b..b1659db68be44 100644 --- a/solution/0200-0299/0254.Factor Combinations/README.md +++ b/solution/0200-0299/0254.Factor Combinations/README.md @@ -163,10 +163,7 @@ func getFactors(n int) [][]int { var dfs func(n, i int) dfs = func(n, i int) { if len(t) > 0 { - cp := make([]int, len(t)) - copy(cp, t) - cp = append(cp, n) - ans = append(ans, cp) + ans = append(ans, append(slices.Clone(t), n)) } for j := i; j <= n/j; j++ { if n%j == 0 { diff --git a/solution/0200-0299/0254.Factor Combinations/README_EN.md b/solution/0200-0299/0254.Factor Combinations/README_EN.md index dc89731eb8591..e35d01122f39c 100644 --- a/solution/0200-0299/0254.Factor Combinations/README_EN.md +++ b/solution/0200-0299/0254.Factor Combinations/README_EN.md @@ -135,10 +135,7 @@ func getFactors(n int) [][]int { var dfs func(n, i int) dfs = func(n, i int) { if len(t) > 0 { - cp := make([]int, len(t)) - copy(cp, t) - cp = append(cp, n) - ans = append(ans, cp) + ans = append(ans, append(slices.Clone(t), n)) } for j := i; j <= n/j; j++ { if n%j == 0 { diff --git a/solution/0200-0299/0254.Factor Combinations/Solution.go b/solution/0200-0299/0254.Factor Combinations/Solution.go index ae887e255dd32..dc566f8d634dd 100644 --- a/solution/0200-0299/0254.Factor Combinations/Solution.go +++ b/solution/0200-0299/0254.Factor Combinations/Solution.go @@ -4,10 +4,7 @@ func getFactors(n int) [][]int { var dfs func(n, i int) dfs = func(n, i int) { if len(t) > 0 { - cp := make([]int, len(t)) - copy(cp, t) - cp = append(cp, n) - ans = append(ans, cp) + ans = append(ans, append(slices.Clone(t), n)) } for j := i; j <= n/j; j++ { if n%j == 0 { diff --git a/solution/0400-0499/0491.Non-decreasing Subsequences/README.md b/solution/0400-0499/0491.Non-decreasing Subsequences/README.md index b470cef3f509c..c41682165bd1b 100644 --- a/solution/0400-0499/0491.Non-decreasing Subsequences/README.md +++ b/solution/0400-0499/0491.Non-decreasing Subsequences/README.md @@ -143,9 +143,7 @@ func findSubsequences(nums []int) [][]int { dfs = func(u, last int, t []int) { if u == len(nums) { if len(t) > 1 { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) } return } diff --git a/solution/0400-0499/0491.Non-decreasing Subsequences/README_EN.md b/solution/0400-0499/0491.Non-decreasing Subsequences/README_EN.md index 69ad659124f77..56c78ec0af163 100644 --- a/solution/0400-0499/0491.Non-decreasing Subsequences/README_EN.md +++ b/solution/0400-0499/0491.Non-decreasing Subsequences/README_EN.md @@ -124,9 +124,7 @@ func findSubsequences(nums []int) [][]int { dfs = func(u, last int, t []int) { if u == len(nums) { if len(t) > 1 { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) } return } diff --git a/solution/0400-0499/0491.Non-decreasing Subsequences/Solution.go b/solution/0400-0499/0491.Non-decreasing Subsequences/Solution.go index 788028ebc6ea8..8f6fca57741bd 100644 --- a/solution/0400-0499/0491.Non-decreasing Subsequences/Solution.go +++ b/solution/0400-0499/0491.Non-decreasing Subsequences/Solution.go @@ -4,9 +4,7 @@ func findSubsequences(nums []int) [][]int { dfs = func(u, last int, t []int) { if u == len(nums) { if len(t) > 1 { - cp := make([]int, len(t)) - copy(cp, t) - ans = append(ans, cp) + ans = append(ans, slices.Clone(t)) } return } diff --git a/solution/1200-1299/1206.Design Skiplist/images/1702370216-mKQcTt-1506_skiplist.gif b/solution/1200-1299/1206.Design Skiplist/images/1702370216-mKQcTt-1506_skiplist.gif new file mode 100644 index 0000000000000..a129e1f94e7ab Binary files /dev/null and b/solution/1200-1299/1206.Design Skiplist/images/1702370216-mKQcTt-1506_skiplist.gif differ diff --git a/solution/2100-2199/2184.Number of Ways to Build Sturdy Brick Wall/README.md b/solution/2100-2199/2184.Number of Ways to Build Sturdy Brick Wall/README.md index 9a0bb13a1819d..72ca77a5b9a5e 100644 --- a/solution/2100-2199/2184.Number of Ways to Build Sturdy Brick Wall/README.md +++ b/solution/2100-2199/2184.Number of Ways to Build Sturdy Brick Wall/README.md @@ -280,15 +280,13 @@ func buildWall(height int, width int, bricks []int) int { mod := int(1e9) + 7 res := [][]int{} t := []int{} - var dfs func(v int) + var dfs func(int) dfs = func(v int) { if v > width { return } if v == width { - cp := make([]int, len(t)) - copy(cp, t) - res = append(res, cp) + res = append(res, slices.Clone(t)) return } for _, x := range bricks { diff --git a/solution/2100-2199/2184.Number of Ways to Build Sturdy Brick Wall/README_EN.md b/solution/2100-2199/2184.Number of Ways to Build Sturdy Brick Wall/README_EN.md index a31b90910ac96..5b4e3ca00feb2 100644 --- a/solution/2100-2199/2184.Number of Ways to Build Sturdy Brick Wall/README_EN.md +++ b/solution/2100-2199/2184.Number of Ways to Build Sturdy Brick Wall/README_EN.md @@ -261,15 +261,13 @@ func buildWall(height int, width int, bricks []int) int { mod := int(1e9) + 7 res := [][]int{} t := []int{} - var dfs func(v int) + var dfs func(int) dfs = func(v int) { if v > width { return } if v == width { - cp := make([]int, len(t)) - copy(cp, t) - res = append(res, cp) + res = append(res, slices.Clone(t)) return } for _, x := range bricks { diff --git a/solution/2100-2199/2184.Number of Ways to Build Sturdy Brick Wall/Solution.go b/solution/2100-2199/2184.Number of Ways to Build Sturdy Brick Wall/Solution.go index 9794ef8291fcf..dc41a5717e304 100644 --- a/solution/2100-2199/2184.Number of Ways to Build Sturdy Brick Wall/Solution.go +++ b/solution/2100-2199/2184.Number of Ways to Build Sturdy Brick Wall/Solution.go @@ -2,15 +2,13 @@ func buildWall(height int, width int, bricks []int) int { mod := int(1e9) + 7 res := [][]int{} t := []int{} - var dfs func(v int) + var dfs func(int) dfs = func(v int) { if v > width { return } if v == width { - cp := make([]int, len(t)) - copy(cp, t) - res = append(res, cp) + res = append(res, slices.Clone(t)) return } for _, x := range bricks {