Skip to content

Commit be97b13

Browse files
authored
feat: use slices.Clone in go code snippets (doocs#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

Lines changed: 1 addition & 3 deletions
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

Lines changed: 1 addition & 3 deletions
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

Lines changed: 1 addition & 3 deletions
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

Lines changed: 1 addition & 3 deletions
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

Lines changed: 1 addition & 3 deletions
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

Lines changed: 1 addition & 3 deletions
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

Lines changed: 1 addition & 3 deletions
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

Lines changed: 1 addition & 3 deletions
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

Lines changed: 2 additions & 6 deletions
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

Lines changed: 2 additions & 6 deletions
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] {

0 commit comments

Comments
 (0)