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

Java實現(xiàn)LeetCode(組合總和)

 更新時間:2021年06月30日 10:38:20   作者:藏呆羊  
這篇文章主要介紹了Java實現(xiàn)LeetCode(組合總數(shù)),本文通過使用java實現(xiàn)leetcode的組合總數(shù)題目和實現(xiàn)思路分析,需要的朋友可以參考下

leetcode題目

組合總和 -- leetcode 39

題目描述

給定一個無重復(fù)元素的數(shù)組 candidates 和一個目標數(shù) target ,

找出 candidates 中所有可以使數(shù)字和為 target 的組合。

candidates 中的數(shù)字可以無限制重復(fù)被選取。

說明:

所有數(shù)字(包括 target)都是正整數(shù)。

解集不能包含重復(fù)的組合。 

示例 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

著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。

思路

回溯算法

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

代碼

package com.leetcode.array;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 題目: 
 * 組合總和 -- leetcode 39
 * 
 * 題目描述:
 * 
給定一個無重復(fù)元素的數(shù)組 candidates 和一個目標數(shù) target ,
找出 candidates 中所有可以使數(shù)字和為 target 的組合。
candidates 中的數(shù)字可以無限制重復(fù)被選取。

說明:
所有數(shù)字(包括 target)都是正整數(shù)。
解集不能包含重復(fù)的組合。 

示例 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
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
 */
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;
        }
        
        // 排序數(shù)組后 可以在遞歸的時候減少遞歸次數(shù),配合 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++) {
        	// 配合排序后的數(shù)組 提升性能
            if (bb[i] > target) {
                break;
            }
            cache.add(bb[i]);
            addCombinationsII(bb,i,target-bb[i],cache,res);
            cache.remove(cache.size()-1);
        }
    }
}

到此這篇關(guān)于Java實現(xiàn)LeetCode(組合總和)的文章就介紹到這了,更多相關(guān)Java實現(xiàn)組合總數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

财经| 无锡市| 六安市| 隆化县| 瑞丽市| 江达县| 聊城市| 安远县| 彰化县| 新营市| 汉阴县| 平舆县| 定兴县| 新巴尔虎左旗| 莱芜市| 静宁县| 开远市| 五家渠市| 海林市| 全椒县| 丹寨县| 青阳县| 神池县| 寻乌县| 西安市| 东台市| 沛县| 佛坪县| 牟定县| 河曲县| 修武县| 五台县| 安宁市| 库伦旗| 安远县| 武功县| 南京市| 斗六市| 两当县| 莲花县| 田东县|