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

边栏推荐
- The k-th node of the binary search tree [sword finger offer]
- CSDN cerebral palsy bug has wasted nearly two hours of hard work
- SSRF-lab
- "APEC industry +" biov Tech talks about the cross-border of Chinese biotechnology enterprises and "Pratt & Whitney Kangyu" | apec-hub public welfare
- Go Concurrency
- MySQL -- optimize query statements and use not in with cases
- Charles and iPhone capture
- Solve some prompt codes that pychar cannot recognize selenium
- Aiot project that is an introduction to the basics of the Internet of things and can be implemented in practice
- Multithreading and thread pool
猜你喜欢

Deep analysis of epoll reactor code
Configuration file ui5 local in SAP ui5 tools Configuration points of yaml

Interface learning

CVPR2021-Semi-supervised Domain Adaptation based on Dual-level Domain Mixing for Semantic Segmentati

C language - minesweeping

C language -- Sanzi chess

Use of pytorch tensorboard

Double recursion in deep analysis merge sort

Design of IM login server and message server

Depth of binary tree
随机推荐
Word quickly makes multiple single-sided table labels, number plates, etc
Large number operation (capable of square root, power, permutation and combination, logarithm and trigonometric value)
Go language map sorting (key/value sorting)
Interview experience - list of questions
How SAP ui5 device type detection device API works
SAP ui5 beginner tutorial No. 27 - unit test tool quNit introduction trial version for SAP ui5 application
Day19 (variable parameter, enhanced for loop traversal, generic wildcard <? >, TreeSet, linkedhashset, nested traversal of sets, set set, static import,)
[Huawei machine test] hj16 shopping list
Timed thread pool
Go microservice development toolkit
1.6.3 use tcpdump to observe DNS communication process
Interface learning
Stack and Queue
Dynamic programming full backpack
Only these four instructions are required to operate SQL data
Code learning-cvpr2020 unsupervised domain adaptive semantic segmentation: intra advance
ThreadLocal
What is the use of the subprocess module
Technology inventory: past, present and future of Message Oriented Middleware
Go Basics