zoukankan      html  css  js  c++  java
  • leetcode——40. 组合总和 II

    好开心!!!我做到了!!!!

    第一次独立完成回溯+剪枝。

    class Solution(object):
        def combinationSum2(self, candidates, target):
            """
            :type candidates: List[int]
            :type target: int
            :rtype: List[List[int]]
            """
            candidates.sort()
            res=[]
            if len(candidates)<1:
                return candidates
            if len(candidates)==1:
                if candidates[0]!=target:
                    return []
                else:
                    return [candidates]
            if candidates.count(target)>0:
                res.append([target])
                candidates=candidates[:candidates.index(target)]
            while len(candidates)>0 and candidates[-1]>target:
                candidates.pop()
            if len(candidates)<1:
                return res
            b=candidates[:]
            i=len(candidates)-1
            while i>0 and candidates[i]>target//2 :
                if candidates[i]!=target:
                    b.remove(candidates[i])
                    i-=1
                else:
                    i-=1
            size=len(candidates)
            for i in range(len(b)):
                path=[]
                if i>0 and b[i]==b[i-1]:
                    continue
                resd=target-b[i]
                path.append(b[i])
                self.__dfs(candidates,i+1,len(candidates),path,res,resd)    
            return res
                    
        def __dfs(self,candidates,begin,size,path,res,resd):
            if resd==0:
                res.append(path[:])
            for j in range(begin,size):
                if j>begin and candidates[j]==candidates[j-1]:
                    continue
                r=resd-candidates[j]
                if r<0:
                    break
                path.append(candidates[j])
                self.__dfs(candidates,j+1,size,path,res,r)
                path.pop()
    执行用时 :48 ms, 在所有 python 提交中击败了70.87%的用户
    内存消耗 :11.7 MB, 在所有 python 提交中击败了32.37%的用户
     
    执行用时为 20 ms 的范例
    class Solution(object):
        def combinationSum2(self, candidates, target):
            """
            :type candidates: List[int]
            :type target: int
            :rtype: List[List[int]]
            """
            ans = []
            candidates.sort()
            self.backtrack(ans, candidates, target, 0, len(candidates), 0, [])
            return ans
    
        def backtrack(self, ans, candidates, target, m, n, c, l):
            if c == target:
                ans.append(l[:])
            for i in range(m, n):
                if i > m and candidates[i - 1] == candidates[i]:
                    continue
                if c + candidates[i] > target:
                    break
                else:
                    l.append(candidates[i])
                    self.backtrack(ans, candidates, target, i + 1, n,
                                   c + candidates[i], l)
                    l.pop()

    这个人家用到是加法回溯。

                                                                                            ——2019.10.14

    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
            int len = candidates.length;
            Arrays.sort(candidates);
            List<List<Integer>> res = new ArrayList<>();
            if(len == 0){
                return res;
            }
            Deque<Integer> path = new ArrayDeque<>(len);
            dfs(candidates,len,0,target,path,res);
            return res;
        }
    
        private void dfs(int[] candidates,int len,int begin, int residue, Deque<Integer> path,List<List<Integer>> res) {
            if(residue == 0){
                res.add(new ArrayList<>(path));
                return;
            }
    
            for(int i = begin;i<len;i++){
                if(residue - candidates[i] < 0){
                    break;
                }
                if(i>begin && candidates[i] == candidates[i-1]){
                    continue;
                }
                path.addLast(candidates[i]);
                dfs(candidates,len,i+1,residue-candidates[i],path,res);
                path.removeLast();
            }
        }

     不难,但是自己没理顺。

    ——2020.6.29

    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    [转]JavaScript和html5 canvas生成圆形印章
    [转]微信小程序开发:http请求
    [转]Clean up after Visual Studio
    [转]How to Clean the Global Assembly Cache
    [转].NET Core dotnet 命令大全
    [转].NET 4.5+项目迁移.NET Core的问题记录 HTTP Error 502.5
    [转]How do I run msbuild from the command line using Windows SDK 7.1?
    [转]glyphicons-halflings-regular字体 图标
    [转]程序集之GAC---Global Assembly Cache
    [转]How can I install the VS2017 version of msbuild on a build server without installing the IDE?
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/11670495.html
Copyright © 2011-2022 走看看