第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

【LEETCODE】模擬面試-39. Combination Sum

和subset区别:规定了子集的sum==target

注意,这里传递的起始位置是i,而不是position+1,but why???

helper(res, path, candidate, sum - candidate[i], i);

code区别:
notice:需要sort
if条件里 sum >= candidate[i]

Arrays.sort(candidate);    //需要sorthelper(res, path, candidate, sum, 0);     //参数多了sum//每次path加入新元素后,sum减去元素:sum - candidate[i]if ( sum == 0 )      //stop条件是sum被减为0了for ( int i = position; i < candidate.length && sum >= candidate[i]; i++ )//sum余额不足覆盖下一个i的话,就不用再走了,这就是sort的好处

图:新生大学

https://leetcode.com/problems/combination-sum/

Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

The same repeated number may be chosen from C unlimited number of times.

Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7] and target 7,
A solution set is:
[
[7],
[2, 2, 3]
]

input: a candidate set, eg, [1, 2, 3, 6, 7], a target sum integer, eg, 4
output: find all possible unique combinations which can sum to be the target, and in each combination, the numbers can be repeated at unlimited times. eg, [2, 2] -> 4

We need a list of list to store result, and a list called path to store current single path. There is also a position to locate the starting point for every path.

Notice the sum target is able to decrease itself during the process.

For convenience, we will sort the candidate array first.

The starting position will iterate from 0 to candidate.length - 1

Since this question allows repeated numbers, for each path, we can do this repeated work by passing the same starting point i to next level, rather than i + 1.

So that, for each unique number, the path will firstly add it for unlimited times until return. Then remove this number one by one, to leave space for next number. Moreover, the next number will also repeatedly add itself first until return.

And every time if current sum is larger than current number, we can add the number to current path, and pass the remaining sum = current sum - current number to next level.

Until current sum is zero, we will add the path to final result list.

After each path has finished its trip and returns to upper level, we should remove the last number of current path, to accept other new numbers.

public class Solution {    public List<List<Integer>> combinationSum(int[] candidate, int sum){        
        List<List<Integer>> res = new ArrayList<List<Integer>>();        //corner
        
        //core
        List<Integer> path = new ArrayList<>();
        
        Arrays.sort(candidate);
        
        helper(res, path, candidate, sum, 0);       //start from position = 0
        
        return res;
    }    
    public void helper(List<List<Integer>> res, List<Integer> path, int[] candidate, int sum, int position){        //base
        if ( sum == 0 ){                                    
            res.add(new ArrayList<Integer>(path));            return ;                                        
        }        
        //current
        for ( int i = position; i < candidate.length && sum >= candidate[i]; i++ ){ 
            path.add(candidate[i]);            //next: pass down remaining 'sum', and afterwards 'start position'
            helper(res, path, candidate, sum - candidate[i], i);        
            path.remove(path.size() - 1);
        }        
        return ;
    }
}
點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消