• leetcode39


    public class Solution
        {
            List<IList<int>> list = new List<IList<int>>();//全部记录
            List<int> records = new List<int>();//一条记录        
            bool bk = false;
            private void BackTrack(List<int> candidates, int target, int sum)
            {
                if (sum == target)
                {
                    int[] temp = new int[records.Count];
                    records.CopyTo(temp);
                    bool same = false;
                    foreach (var l in list)
                    {
                        if (l.Count == temp.Length)
                        {
                            var l1 = l.OrderBy(x => x).ToList();
                            var l2 = temp.OrderBy(x => x).ToList();
                            var samecount = 0;
                            for (int x = 0; x < l1.Count; x++)
                            {
                                if (l1[x] == l2[x])
                                {
                                    samecount++;
                                }
                            }
                            if (samecount == l.Count)
                            {
                                same = true;
                                break;
                            }
                        }                    
                    }
                    if (!same)
                    {
                        list.Add(temp.ToList());
                    }
                    bk = true;
                    return;
                }
                else if (sum < target)
                {
                    bk = true;
                    for (int position = 0; position < candidates.Count; position++)
                    {
                        var cur = candidates[position];
                        sum += cur;
                        records.Add(cur);
                        BackTrack(candidates, target, sum);
                        //回溯
                        records.RemoveAt(records.Count - 1);
                        sum -= cur;
                        if (bk)
                        {
                            bk = false;
                            break;
                        }
                    }
                }
                else
                {
                    bk = true;
                    return;
                }
            }
    
            public IList<IList<int>> CombinationSum(int[] candidates, int target)
            {
                var can = candidates.OrderBy(x => x).ToList();
                BackTrack(can, target, 0);
                return list;
            }
        }

    补充一个python的实现,写的要简单的多了:

     1 class Solution:    
     2     def dfs(self,candidates,target,index,path,l):
     3         if target < 0:
     4             return
     5         elif target == 0:
     6             l.append(path)
     7             return
     8         for i in range(index,len(candidates)):
     9             self.dfs(candidates,target-candidates[i],i,path+[candidates[i]],l)
    10         return
    11 
    12     def combinationSum(self, candidates: 'List[int]', target: 'int') -> 'List[List[int]]':
    13         l = list()
    14         path = []
    15         candidates.sort()
    16         self.dfs(candidates,target,0,path,l)
    17         return l
  • 相关阅读:
    mysql日期默认值格式('0000-00-00')错误解决
    休闲一下-精华-IMDB评分8分左右影片400多部!
    开机动画流程
    SystemServer启动服务
    理解Android进程创建流程
    android开机动画启动流程
    Android系统的启动优化
    Android 系统启动(init,zygote)
    Zygote
    init
  • 原文地址:https://www.cnblogs.com/asenyang/p/9756126.html
Copyright © 2020-2023  润新知