服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java实现LeetCode(组合总和)

Java实现LeetCode(组合总和)

2021-09-22 00:56藏呆羊 Java教程

这篇文章主要介绍了Java实现LeetCode(组合总数),本文通过使用java实现leetcode的组合总数题目和实现思路分析,需要的朋友可以参考下

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实现组合总数内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

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

延伸 · 阅读

精彩推荐