Java实现LeetCode(组合总和)

2025-05-29 0 86

leetcode题目

组合总和leetcode 39

题目描述

给定一个无重复元素的数组 candidates 和一个目标数 target ,

找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的数字可以无限制重复被选取。

说明:

所有数字(包括 target)都是正整数。

解集不能包含重复的组合。

示例 1:

输入: candidates = [2,3,6,7], target = 7,

所求解集为:

[

[7],

[2,2,3]

]

示例 2:

输入: candidates = [2,3,5], target = 8,

所求解集为:

[

[2,2,2,2],

[2,3,3],

[3,5]

]

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/combination-sum

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

回溯算法

递归找和为target的组合,出口为和超过了target

代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111
package com.leetcode.array;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

/**

* 题目:

* 组合总和 -- leetcode 39

*

* 题目描述:

*

给定一个无重复元素的数组 candidates 和一个目标数 target ,

找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的数字可以无限制重复被选取。

说明:

所有数字(包括 target)都是正整数。

解集不能包含重复的组合。

示例 1:

输入: candidates = [2,3,6,7], target = 7,

所求解集为:

[

[7],

[2,2,3]

]

示例 2:

输入: candidates = [2,3,5], target = 8,

所求解集为:

[

[2,2,2,2],

[2,3,3],

[3,5]

]

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/combination-sum

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

*/

public class CombinationSum {

/**

* 思路:

* 1、回溯算法

* 2、递归找和为target的组合,出口为和超过了target

*/

public List<List<Integer>> combinationSum(int[] bb, int target) {

List<List<Integer>> res = new ArrayList<>();

if (bb == null) {

return res;

}

addCombinations(bb, 0, target, new ArrayList<Integer>(), res);

return res;

}

private void addCombinations(int[] bb, int start, int target, List<Integer> cache, List<List<Integer>> res) {

if (target < 0) {

return;

}

if (target == 0) {

res.add(new ArrayList<>(cache));

return;

}

for (int i=start; i<bb.length; i++) {

cache.add(bb[i]);

addCombinations(bb,i,target-bb[i],cache,res);

cache.remove(cache.size()-1);

}

}

/**

* 思路:

* 优化后的回溯

*/

public List<List<Integer>> combinationSumII(int[] bb, int target) {

List<List<Integer>> res = new ArrayList<>();

if (bb == null) {

return res;

}

// 排序数组后 可以在递归的时候减少递归次数,配合 if (bb[i] > target) break;

Arrays.sort(bb);

addCombinationsII(bb, 0, target, new ArrayList<Integer>(), res);

return res;

}

private void addCombinationsII(int[] bb, int start, int target, List<Integer> cache, List<List<Integer>> res) {

if (target < 0) {

return;

}

if (target == 0) {

res.add(new ArrayList<>(cache));

return;

}

for (int i=start; i<bb.length; i++) {

// 配合排序后的数组 提升性能

if (bb[i] > target) {

break;

}

cache.add(bb[i]);

addCombinationsII(bb,i,target-bb[i],cache,res);

cache.remove(cache.size()-1);

}

}

}

到此这篇关于Java实现LeetCode(组合总和)的文章就介绍到这了,更多相关Java实现组合总数内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!

原文链接:https://blog.csdn.net/zangdaiyang1991/article/details/94554195

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java实现LeetCode(组合总和) https://www.kuaiidc.com/106275.html

相关文章

发表评论
暂无评论