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

Each number in C may only be used once in the combination.

Note:

  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.

For example, given candidate set 10,1,2,7,6,1,5 and target 8
A solution set is: 
[1, 7] 
[1, 2, 5] 
[2, 6] 
[1, 1, 6]

public class Solution {
    public IList<IList<int>> CombinationSum2(int[] candidates, int target) {
        List<IList<int>> result = new List<IList<int>>();
        List<int> res=new List<int>();
        Array.Sort(candidates);
        calulate(candidates,,target,res,result);
        return result;
    }

    void calulate(int[] candidates, int cur,int target,List<int> res,List<IList<int>> result)
    {
        )
        {
            result.Add(new List<int>(res));
            return;
        }
        )
        {
            return;
        }

        for(int i=cur;i<candidates.Length;i++)
        {
           ])
              continue;
           res.Insert(res.Count,candidates[i]);
           calulate(candidates,i+,target-candidates[i],res,result);
           res.RemoveAt(res.Count-);
        }
    }
}

1 这个算法可以非常高效的计算出结果。

2 这个算法是在方法中调用方法本身。

没有懂的leetcode的更多相关文章

  1. 刷LeetCode的正确姿势——第1、125题

    最近刷LeetCode比较频繁,就购买了官方的参考电子书 (CleanCodeHandbook),里面有题目的解析和范例源代码,可以省去非常多寻找免费经验分享内容和整理这些资料的时间.惊喜的是,里面的 ...

  2. [LeetCode] Encode String with Shortest Length 最短长度编码字符串

    Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...

  3. [LeetCode] All O`one Data Structure 全O(1)的数据结构

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

  4. [LeetCode] Majority Element 求众数

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  5. [LeetCode] Read N Characters Given Read4 II - Call multiple times 用Read4来读取N个字符之二 - 多次调用

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

  6. [LeetCode] Word Ladder II 词语阶梯之二

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  7. [LeetCode] Climbing Stairs 爬梯子问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  8. [LeetCode] ZigZag Converesion 之字型转换字符串

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  9. Leetcode分类刷题答案&心得

    Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...

随机推荐

  1. iOS触摸事件处理--备用

    主要是记录下iOS的界面触摸事件处理机制,然后用一个实例来说明下应用场景. 一.处理机制 界面响应消息机制分两块,(1)首先在视图的层次结构里找到能响应消息的那个视图.(2)然后在找到的视图里处理消息 ...

  2. Unity网络斗地主 服务端

    Unity网络斗地主  服务端 @by梦想之家2工作室 阿龙 已经做好了服务器框架,并且能实现服务器给客户端分牌的问题!

  3. SCVMM和SQL分别建在不同服务器上报错:Error ID 319 during database creation on remote SQL Server

    问题发生了,测试过权限,帐号,服务,工具问题,均不是. 在微软网站找到解决办法. 就是MASTER.SQL的脚本在数据库服务器上单独运行. 然后,安装SCVMM时,不新建数据库,直接指现建好的库. 搞 ...

  4. 【HDOJ】2267 How Many People Can Survive

    BFS. #include <iostream> #include <cstdio> #include <cstring> #include <queue&g ...

  5. ubuntu下新建VPN连接

    1. 安装VPN Client#sudo apt-get install pptp-linux2. 安装网络管理器对VPN的支持#sudo apt-get install network-manage ...

  6. Android WebView播放视频flash(判断是否安装flash插件)

    Android WebView播放flash(判断是否安装flash插件)  最近帮一个同学做一个项目,断断续续的一些知识点记录一下.一个页面中有一个WebView,用来播放swf,如果系统中未安装f ...

  7. 2015第37周五javascript函数arguments对象巧用一

    Javascript函数的一个巧妙利用:假定action中有一个JSONObject类型的对象data,其值有可能为空,则前台JSP页面的JS代码中想直接通过EL表达式,即${data}的形式访问对象 ...

  8. 主席树:HDU 4417 Super Mario

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. Redis结合EntityFramework结合使用的操作类

    最近一段时间在研究redis.  各种不懂, 各种问题.也看了N多的资料. 最终参照着  张占岭 的博客  http://www.cnblogs.com/lori/p/3435483.html   写 ...

  10. TabHost结合RadioButton实现主页的导航效果

    布局文件的设置,如下 <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:androi ...