Skip to content

Commit be97b13

Browse files
authored
feat: use slices.Clone in go code snippets (#2100)
1 parent 574a854 commit be97b13

File tree

39 files changed

+64
-159
lines changed

39 files changed

+64
-159
lines changed

lcof/面试题34. 二叉树中和为某一值的路径/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,7 @@ func pathSum(root *TreeNode, target int) (ans [][]int) {
193193
t = append(t, root.Val)
194194
s -= root.Val
195195
if root.Left == nil && root.Right == nil && s == 0 {
196-
cp := make([]int, len(t))
197-
copy(cp, t)
198-
ans = append(ans, cp)
196+
ans = append(ans, slices.Clone(t))
199197
}
200198
dfs(root.Left, s)
201199
dfs(root.Right, s)

lcof/面试题34. 二叉树中和为某一值的路径/Solution.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ func pathSum(root *TreeNode, target int) (ans [][]int) {
1616
t = append(t, root.Val)
1717
s -= root.Val
1818
if root.Left == nil && root.Right == nil && s == 0 {
19-
cp := make([]int, len(t))
20-
copy(cp, t)
21-
ans = append(ans, cp)
19+
ans = append(ans, slices.Clone(t))
2220
}
2321
dfs(root.Left, s)
2422
dfs(root.Right, s)

lcof2/剑指 Offer II 079. 所有子集/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,7 @@ func subsets(nums []int) [][]int {
144144
}
145145

146146
func dfs(i int, nums, t []int, res *[][]int) {
147-
cp := make([]int, len(t))
148-
copy(cp, t)
149-
*res = append(*res, cp)
147+
*res = append(*res, slices.Clone(t))
150148
if i == len(nums) {
151149
return
152150
}

lcof2/剑指 Offer II 079. 所有子集/Solution.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ func subsets(nums []int) [][]int {
66
}
77

88
func dfs(i int, nums, t []int, res *[][]int) {
9-
cp := make([]int, len(t))
10-
copy(cp, t)
11-
*res = append(*res, cp)
9+
*res = append(*res, slices.Clone(t))
1210
if i == len(nums) {
1311
return
1412
}

lcof2/剑指 Offer II 080. 含有 k 个元素的组合/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,7 @@ func combine(n int, k int) [][]int {
135135

136136
func dfs(i, n, k int, t []int, res *[][]int) {
137137
if len(t) == k {
138-
cp := make([]int, k)
139-
copy(cp, t)
140-
*res = append(*res, cp)
138+
*res = append(*res, slices.Clone(t))
141139
return
142140
}
143141
for j := i; j <= n; j++ {

lcof2/剑指 Offer II 080. 含有 k 个元素的组合/Solution.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ func combine(n int, k int) [][]int {
77

88
func dfs(i, n, k int, t []int, res *[][]int) {
99
if len(t) == k {
10-
cp := make([]int, k)
11-
copy(cp, t)
12-
*res = append(*res, cp)
10+
*res = append(*res, slices.Clone(t))
1311
return
1412
}
1513
for j := i; j <= n; j++ {

lcof2/剑指 Offer II 082. 含有重复元素集合的组合/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,7 @@ func combinationSum2(candidates []int, target int) [][]int {
174174
return
175175
}
176176
if s == target {
177-
cp := make([]int, len(t))
178-
copy(cp, t)
179-
ans = append(ans, cp)
177+
ans = append(ans, slices.Clone(t))
180178
return
181179
}
182180
for i := u; i < len(candidates); i++ {

lcof2/剑指 Offer II 082. 含有重复元素集合的组合/Solution.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ func combinationSum2(candidates []int, target int) [][]int {
88
return
99
}
1010
if s == target {
11-
cp := make([]int, len(t))
12-
copy(cp, t)
13-
ans = append(ans, cp)
11+
ans = append(ans, slices.Clone(t))
1412
return
1513
}
1614
for i := u; i < len(candidates); i++ {

solution/0000-0099/0039.Combination Sum/README.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,7 @@ func combinationSum(candidates []int, target int) (ans [][]int) {
247247
var dfs func(i, s int)
248248
dfs = func(i, s int) {
249249
if s == 0 {
250-
cp := make([]int, len(t))
251-
copy(cp, t)
252-
ans = append(ans, cp)
250+
ans = append(ans, slices.Clone(t))
253251
return
254252
}
255253
if s < candidates[i] {
@@ -273,9 +271,7 @@ func combinationSum(candidates []int, target int) (ans [][]int) {
273271
var dfs func(i, s int)
274272
dfs = func(i, s int) {
275273
if s == 0 {
276-
cp := make([]int, len(t))
277-
copy(cp, t)
278-
ans = append(ans, cp)
274+
ans = append(ans, slices.Clone(t))
279275
return
280276
}
281277
if i >= len(candidates) || s < candidates[i] {

solution/0000-0099/0039.Combination Sum/README_EN.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,7 @@ func combinationSum(candidates []int, target int) (ans [][]int) {
239239
var dfs func(i, s int)
240240
dfs = func(i, s int) {
241241
if s == 0 {
242-
cp := make([]int, len(t))
243-
copy(cp, t)
244-
ans = append(ans, cp)
242+
ans = append(ans, slices.Clone(t))
245243
return
246244
}
247245
if s < candidates[i] {
@@ -265,9 +263,7 @@ func combinationSum(candidates []int, target int) (ans [][]int) {
265263
var dfs func(i, s int)
266264
dfs = func(i, s int) {
267265
if s == 0 {
268-
cp := make([]int, len(t))
269-
copy(cp, t)
270-
ans = append(ans, cp)
266+
ans = append(ans, slices.Clone(t))
271267
return
272268
}
273269
if i >= len(candidates) || s < candidates[i] {

solution/0000-0099/0039.Combination Sum/Solution.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ func combinationSum(candidates []int, target int) (ans [][]int) {
44
var dfs func(i, s int)
55
dfs = func(i, s int) {
66
if s == 0 {
7-
cp := make([]int, len(t))
8-
copy(cp, t)
9-
ans = append(ans, cp)
7+
ans = append(ans, slices.Clone(t))
108
return
119
}
1210
if i >= len(candidates) || s < candidates[i] {

solution/0000-0099/0040.Combination Sum II/README.md

+9-13
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,7 @@ func combinationSum2(candidates []int, target int) (ans [][]int) {
264264
var dfs func(i, s int)
265265
dfs = func(i, s int) {
266266
if s == 0 {
267-
cp := make([]int, len(t))
268-
copy(cp, t)
269-
ans = append(ans, cp)
267+
ans = append(ans, slices.Clone(t))
270268
return
271269
}
272270
if i >= len(candidates) || s < candidates[i] {
@@ -293,22 +291,20 @@ func combinationSum2(candidates []int, target int) (ans [][]int) {
293291
var dfs func(i, s int)
294292
dfs = func(i, s int) {
295293
if s == 0 {
296-
cp := make([]int, len(t))
297-
copy(cp, t)
298-
ans = append(ans, cp)
294+
ans = append(ans, slices.Clone(t))
299295
return
300296
}
301297
if i >= len(candidates) || s < candidates[i] {
302298
return
303299
}
304-
x := candidates[i]
305-
t = append(t, x)
306-
dfs(i+1, s-x)
307-
t = t[:len(t)-1]
308-
for i < len(candidates) && candidates[i] == x {
309-
i++
300+
for j := i; j < len(candidates); j++ {
301+
if j > i && candidates[j] == candidates[j-1] {
302+
continue
303+
}
304+
t = append(t, candidates[j])
305+
dfs(j+1, s-candidates[j])
306+
t = t[:len(t)-1]
310307
}
311-
dfs(i, s)
312308
}
313309
dfs(0, target)
314310
return

solution/0000-0099/0040.Combination Sum II/README_EN.md

+9-13
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,7 @@ func combinationSum2(candidates []int, target int) (ans [][]int) {
256256
var dfs func(i, s int)
257257
dfs = func(i, s int) {
258258
if s == 0 {
259-
cp := make([]int, len(t))
260-
copy(cp, t)
261-
ans = append(ans, cp)
259+
ans = append(ans, slices.Clone(t))
262260
return
263261
}
264262
if i >= len(candidates) || s < candidates[i] {
@@ -285,22 +283,20 @@ func combinationSum2(candidates []int, target int) (ans [][]int) {
285283
var dfs func(i, s int)
286284
dfs = func(i, s int) {
287285
if s == 0 {
288-
cp := make([]int, len(t))
289-
copy(cp, t)
290-
ans = append(ans, cp)
286+
ans = append(ans, slices.Clone(t))
291287
return
292288
}
293289
if i >= len(candidates) || s < candidates[i] {
294290
return
295291
}
296-
x := candidates[i]
297-
t = append(t, x)
298-
dfs(i+1, s-x)
299-
t = t[:len(t)-1]
300-
for i < len(candidates) && candidates[i] == x {
301-
i++
292+
for j := i; j < len(candidates); j++ {
293+
if j > i && candidates[j] == candidates[j-1] {
294+
continue
295+
}
296+
t = append(t, candidates[j])
297+
dfs(j+1, s-candidates[j])
298+
t = t[:len(t)-1]
302299
}
303-
dfs(i, s)
304300
}
305301
dfs(0, target)
306302
return

solution/0000-0099/0040.Combination Sum II/Solution.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ func combinationSum2(candidates []int, target int) (ans [][]int) {
44
var dfs func(i, s int)
55
dfs = func(i, s int) {
66
if s == 0 {
7-
cp := make([]int, len(t))
8-
copy(cp, t)
9-
ans = append(ans, cp)
7+
ans = append(ans, slices.Clone(t))
108
return
119
}
1210
if i >= len(candidates) || s < candidates[i] {

solution/0000-0099/0046.Permutations/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,7 @@ func permute(nums []int) (ans [][]int) {
188188
var dfs func(int)
189189
dfs = func(i int) {
190190
if i == n {
191-
cp := make([]int, n)
192-
copy(cp, t)
193-
ans = append(ans, cp)
191+
ans = append(ans, slices.Clone(t))
194192
return
195193
}
196194
for j, v := range nums {

solution/0000-0099/0046.Permutations/README_EN.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,7 @@ func permute(nums []int) (ans [][]int) {
167167
var dfs func(int)
168168
dfs = func(i int) {
169169
if i == n {
170-
cp := make([]int, n)
171-
copy(cp, t)
172-
ans = append(ans, cp)
170+
ans = append(ans, slices.Clone(t))
173171
return
174172
}
175173
for j, v := range nums {

solution/0000-0099/0046.Permutations/Solution.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ func permute(nums []int) (ans [][]int) {
55
var dfs func(int)
66
dfs = func(i int) {
77
if i == n {
8-
cp := make([]int, n)
9-
copy(cp, t)
10-
ans = append(ans, cp)
8+
ans = append(ans, slices.Clone(t))
119
return
1210
}
1311
for j, v := range nums {

solution/0000-0099/0047.Permutations II/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,7 @@ func permuteUnique(nums []int) (ans [][]int) {
168168
var dfs func(int)
169169
dfs = func(i int) {
170170
if i == n {
171-
cp := make([]int, n)
172-
copy(cp, t)
173-
ans = append(ans, cp)
171+
ans = append(ans, slices.Clone(t))
174172
return
175173
}
176174
for j := 0; j < n; j++ {

solution/0000-0099/0047.Permutations II/README_EN.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,7 @@ func permuteUnique(nums []int) (ans [][]int) {
158158
var dfs func(int)
159159
dfs = func(i int) {
160160
if i == n {
161-
cp := make([]int, n)
162-
copy(cp, t)
163-
ans = append(ans, cp)
161+
ans = append(ans, slices.Clone(t))
164162
return
165163
}
166164
for j := 0; j < n; j++ {

solution/0000-0099/0047.Permutations II/Solution.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ func permuteUnique(nums []int) (ans [][]int) {
66
var dfs func(int)
77
dfs = func(i int) {
88
if i == n {
9-
cp := make([]int, n)
10-
copy(cp, t)
11-
ans = append(ans, cp)
9+
ans = append(ans, slices.Clone(t))
1210
return
1311
}
1412
for j := 0; j < n; j++ {

solution/0000-0099/0077.Combinations/README.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,7 @@ func combine(n int, k int) (ans [][]int) {
239239
var dfs func(int)
240240
dfs = func(i int) {
241241
if len(t) == k {
242-
cp := make([]int, len(t))
243-
copy(cp, t)
244-
ans = append(ans, cp)
242+
ans = append(ans, slices.Clone(t))
245243
return
246244
}
247245
if i > n {
@@ -263,9 +261,7 @@ func combine(n int, k int) (ans [][]int) {
263261
var dfs func(int)
264262
dfs = func(i int) {
265263
if len(t) == k {
266-
cp := make([]int, len(t))
267-
copy(cp, t)
268-
ans = append(ans, cp)
264+
ans = append(ans, slices.Clone(t))
269265
return
270266
}
271267
if i > n {

solution/0000-0099/0077.Combinations/README_EN.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,7 @@ func combine(n int, k int) (ans [][]int) {
226226
var dfs func(int)
227227
dfs = func(i int) {
228228
if len(t) == k {
229-
cp := make([]int, len(t))
230-
copy(cp, t)
231-
ans = append(ans, cp)
229+
ans = append(ans, slices.Clone(t))
232230
return
233231
}
234232
if i > n {
@@ -250,9 +248,7 @@ func combine(n int, k int) (ans [][]int) {
250248
var dfs func(int)
251249
dfs = func(i int) {
252250
if len(t) == k {
253-
cp := make([]int, len(t))
254-
copy(cp, t)
255-
ans = append(ans, cp)
251+
ans = append(ans, slices.Clone(t))
256252
return
257253
}
258254
if i > n {

solution/0000-0099/0077.Combinations/Solution.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ func combine(n int, k int) (ans [][]int) {
33
var dfs func(int)
44
dfs = func(i int) {
55
if len(t) == k {
6-
cp := make([]int, len(t))
7-
copy(cp, t)
8-
ans = append(ans, cp)
6+
ans = append(ans, slices.Clone(t))
97
return
108
}
119
if i > n {

solution/0100-0199/0113.Path Sum II/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,7 @@ func pathSum(root *TreeNode, targetSum int) (ans [][]int) {
189189
s -= root.Val
190190
t = append(t, root.Val)
191191
if root.Left == nil && root.Right == nil && s == 0 {
192-
cp := make([]int, len(t))
193-
copy(cp, t)
194-
ans = append(ans, cp)
192+
ans = append(ans, slices.Clone(t))
195193
}
196194
dfs(root.Left, s)
197195
dfs(root.Right, s)

solution/0100-0199/0113.Path Sum II/README_EN.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,7 @@ func pathSum(root *TreeNode, targetSum int) (ans [][]int) {
178178
s -= root.Val
179179
t = append(t, root.Val)
180180
if root.Left == nil && root.Right == nil && s == 0 {
181-
cp := make([]int, len(t))
182-
copy(cp, t)
183-
ans = append(ans, cp)
181+
ans = append(ans, slices.Clone(t))
184182
}
185183
dfs(root.Left, s)
186184
dfs(root.Right, s)

solution/0100-0199/0113.Path Sum II/Solution.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ func pathSum(root *TreeNode, targetSum int) (ans [][]int) {
1616
s -= root.Val
1717
t = append(t, root.Val)
1818
if root.Left == nil && root.Right == nil && s == 0 {
19-
cp := make([]int, len(t))
20-
copy(cp, t)
21-
ans = append(ans, cp)
19+
ans = append(ans, slices.Clone(t))
2220
}
2321
dfs(root.Left, s)
2422
dfs(root.Right, s)

0 commit comments

Comments
 (0)