最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實現LeetCode(組合總和)

 更新時間:2021年06月30日 10:38:20   作者:藏呆羊  
這篇文章主要介紹了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

著作權歸領扣網絡所有。商業(yè)轉載請聯系官方授權,非商業(yè)轉載請注明出處。

思路

回溯算法

遞歸找和為target的組合,出口為和超過了target

代碼

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
著作權歸領扣網絡所有。商業(yè)轉載請聯系官方授權,非商業(yè)轉載請注明出處。
 */
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);
        }
    }
	
    
    /**
     * 思路: 
     * 優(yōu)化后的回溯
     */
    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實現組合總數內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

佛山市| 遂平县| 西贡区| 安溪县| 湄潭县| 绵竹市| 乌什县| 历史| 闽侯县| 珲春市| 石楼县| 安西县| 遵化市| 五华县| 松原市| 洛浦县| 田东县| 体育| 利川市| 广灵县| 海南省| 平阳县| 富顺县| 大名县| 渝北区| 托克托县| 竹溪县| 科技| 高密市| 临澧县| 丹寨县| 昔阳县| 冕宁县| 华阴市| 阳城县| 阜康市| 南昌县| 衡南县| 临颍县| 阿尔山市| 元氏县|