当前位置:网站首页>【LeetCode】40. Combined summation II (2 strokes of wrong questions)
【LeetCode】40. Combined summation II (2 strokes of wrong questions)
2022-06-25 05:49:00 【Kaimar】


- Ideas
Each number can only be used once in each combination , The same number may appear more than once .
The difficulty of this problem lies in removing the weight , The same number can appear on the same layer , But the same number can no longer appear in the same place , Such as [1, 1, 1, 7], The goal is 8, It's supposed to be [[1, 7]], Instead of going back, there will be [[1,7], [1,7], [1,7]], Therefore, it is necessary to mark whether the same number on the same layer has been used .
func sum(arr []int) int {
s := 0
for _, v := range arr {
s += v
}
return s
}
func combinationSum2(candidates []int, target int) [][]int {
res := [][]int{
}
arr := []int{
}
// used[i] == true, Indicates that the same branch has been used
// used[i] == false, Indicates that the same layer has been used
used := make([]bool, len(candidates))
var backtracking func([]int, int, int, []bool)
backtracking = func(candidates []int, target int, start int, used []bool) {
if sum(arr) == target {
temp := make([]int, len(arr))
copy(temp, arr)
res = append(res, temp)
}
for i := start; i < len(candidates); i++ {
// Same layer weight removal
if i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false {
continue
}
if sum(arr) + candidates[i] > target {
break
}
arr = append(arr, candidates[i])
used[i] = true
backtracking(candidates, target, i + 1, used)
arr = arr[: len(arr) - 1]
used[i] = false
}
}
// Sort
sort.Ints(candidates)
backtracking(candidates, target, 0, used)
return res
}

边栏推荐
- HashSet implementation class
- Analysis of IM project framework
- Vscode voice notes to enrich information (Part 1)
- Only these four instructions are required to operate SQL data
- SAP ui5 application development tutorial 32 - how to create a custom SAP ui5 control
- Semantic segmentation cvpr2019-advance: advantageous enterprise minimization for domain adaptation in semantic segmentation
- 05 virtual machine stack
- Multithreading and thread pool
- Excel splits a worksheet into multiple worksheets according to conditions, and how to split multiple worksheets into independent tables
- 2022.1.23 diary
猜你喜欢

Only these four instructions are required to operate SQL data

Trouble of setting table property to null
Wind farm visualization: wind farm data
[interview with a large factory] meituan had two meetings. Was there a surprise in the end?
SAP ui5 beginner tutorial No. 28 - Introduction to the integration test tool OPA for SAP ui5 applications

Incorrect dependency of POM file
SAP ui5 tutorial for beginners part XXVI - detailed steps for using OData service with mock server trial version
Technology inventory: Technology Evolution and Future Trend Outlook of cloud native Middleware

HashSet implementation class

1.5.3 use tcpdump to observe ARP communication process
随机推荐
Day22(File,DiGui,FileOutputStream)
05 virtual machine stack
HashSet implementation class
Create a complete binary tree in array order
Kubevela v1.2 release: the graphical operation console velaux you want is finally here!
Farewell to Lombok in 996
Unsupervised domain adaptation in semantic segmentation:a review unsupervised domain adaptation in semantic segmentation: a review
Guava new collection type
MySQL operation JSON
Try with resource close resource flow
Technology Review: what is the evolution route of container technology? What imagination space is there in the future?
[interview with a large factory] meituan had two meetings. Was there a surprise in the end?
Transformations of pytorch torch torch vision
Incorrect dependency of POM file
ThreadLocal
钱堂教育的证券账户安全吗?靠谱吗?
Differences and connections between sap ui5 and openui5
Makefile Foundation
Stack and Queue
Word of the Day