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. nth_element学习

    今天学习到STL中的nth_element,她是一个默认能求第k小的数的方法,需要的头文件为algorithm. 默认为:nth_element(start, start+n, end) 使第n大元素 ...

  2. iOS的app 中的 埋点

    埋点 就是 挖个坑把 种子埋到土里 然后浇水  等待发芽 埋点就是 ,鬼子要进村,我们埋下地雷 埋点就是 小说中 作者欲扬先抑  或者欲擒故纵  设下的伏笔... 好了,用文学的手法很好的  解释了一 ...

  3. Vim C/C++的一键编译

    开始用Vim差不多有两个月的时间, 一开始用Makefile 编译一整个项目无压力, 但是当写到单个文件的时候, 编译就比较麻烦了, 每次都得 :w :!gcc -o 1.exe 1.c :!1 非常 ...

  4. BZOJ 1588 营业额统计

    Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每 ...

  5. Delphi 的运算符列表,运算符及优先级表格 good

    Delphi 的运算符列表 分类 运算符 操作 操作数 结果类型 范例 算术运算符 + 加 整数,实数 整数,实数 X + Y - 减 整数,实数 整数,实数 Result - 1 * 乘 整数,实数 ...

  6. Unity笔记

    1.使某个对象上的脚本失效和生效: GameObject.Find("xxx").transform.GetComponent<xxx>().enabled = fal ...

  7. 使用 libevent 和 libev 提高网络应用性能

    使用 libevent 和 libev 提高网络应用性能 Martin C. Brown, 作家, Freelance 简介: 构建现代的服务器应用程序需要以某种方法同时接收数百.数千甚至数万个事件, ...

  8. C++学习之容器的摸索

    初学容器,容易犯错的地方 1.vector,list和deque都是顺序容器.其中vector和deque都可以通过下标访问,而list不能 2. 容器的begin和end操作 c.begin()返回 ...

  9. Redis Sentinel实现Failover

    redis版本:2.8.17   服务器规划: 10.50.13.34(6379 master) 10.50.13.35(6379 slave) 10.50.13.36(6379 slave) 10. ...

  10. SVN服务器搭建(与apache整合)

    一.SVN介绍 SVN是一个版本控制工具,Subversion的版本库(repository),就是位于服务器,统一管理和储存数据的地方. 二.SVN数据存储方式 在Subversion中,版本库的数 ...