Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

feat: use slices.Clone() in go code snippets #2100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions lcof2/剑指 Offer II 079. 所有子集/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 1 addition & 3 deletions lcof2/剑指 Offer II 079. 所有子集/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down
8 changes: 2 additions & 6 deletions solution/0000-0099/0039.Combination Sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand All @@ -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] {
Expand Down
8 changes: 2 additions & 6 deletions solution/0000-0099/0039.Combination Sum/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand All @@ -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] {
Expand Down
4 changes: 1 addition & 3 deletions solution/0000-0099/0039.Combination Sum/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down
22 changes: 9 additions & 13 deletions solution/0000-0099/0040.Combination Sum II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand All @@ -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
Expand Down
22 changes: 9 additions & 13 deletions solution/0000-0099/0040.Combination Sum II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions solution/0000-0099/0040.Combination Sum II/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down
4 changes: 1 addition & 3 deletions solution/0000-0099/0046.Permutations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions solution/0000-0099/0046.Permutations/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions solution/0000-0099/0046.Permutations/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions solution/0000-0099/0047.Permutations II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down
4 changes: 1 addition & 3 deletions solution/0000-0099/0047.Permutations II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down
4 changes: 1 addition & 3 deletions solution/0000-0099/0047.Permutations II/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down
8 changes: 2 additions & 6 deletions solution/0000-0099/0077.Combinations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
8 changes: 2 additions & 6 deletions solution/0000-0099/0077.Combinations/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions solution/0000-0099/0077.Combinations/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions solution/0100-0199/0113.Path Sum II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions solution/0100-0199/0113.Path Sum II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions solution/0100-0199/0113.Path Sum II/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading