当前位置:网站首页>【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
}

边栏推荐
- Use of collection
- Go language map sorting (key/value sorting)
- Depth of binary tree
- Timed thread pool
- Trouble of setting table property to null
- Code learning-cvpr2020 unsupervised domain adaptive semantic segmentation: intra advance
- Pytorch- daily learning notes of some small functions involving training
- Technology inventory: past, present and future of Message Oriented Middleware
- MySQL transaction learning notes (I) first encounter
- Yunda's cloud based business in Taiwan construction 𞓜 practical school
猜你喜欢

Array introduction plus example 01
Wind farm visualization: wind farm data
Introduction to the main features of kyma when the cloud native application runs

C language -- Sanzi chess

Jenkins installation and configuration

JSON Library Tutorial from scratch (II): parsing digital learning and sorting notes

2022.1.21 diary

BUUCTF(web:1-50)

1.5.3 use tcpdump to observe ARP communication process
![The k-th node of the binary search tree [sword finger offer]](/img/9c/26b508100d8b49c114cf72346a8e7c.jpg)
The k-th node of the binary search tree [sword finger offer]
随机推荐
[JS basic review] scope, this, closure
Deep learning non local neural networks
Deep analysis of epoll reactor code
Kyma application connectivity feature introduction
钱堂教育的证券账户安全吗?靠谱吗?
Day19 (variable parameter, enhanced for loop traversal, generic wildcard <? >, TreeSet, linkedhashset, nested traversal of sets, set set, static import,)
Day18 (set, generic, hash table, tree, stack and queue, graph, array and linked list)
Pat 1045 quick sort
Dynamic programming example 1 leetcode 322 coin change
Pytorch- daily learning notes of some small functions involving training
Jz-066- motion range of robot
JSON Library Tutorial from scratch (I): starting to learn and organize notes
Basic bit operation symbols of C language
SAP ui5 tutorial for beginners part XXVI - detailed steps for using OData service with mock server trial version
SAP ui5 beginner tutorial No. 28 - Introduction to the integration test tool OPA for SAP ui5 applications
Semantic segmentation cvpr2020 unsupervised intra domain adaptation for semantic segmentation through self supervision
[untitled]
Word quickly makes multiple single-sided table labels, number plates, etc
Transformations of pytorch torch torch vision
Create a complete binary tree in array order