没有懂的leetcode
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的更多相关文章
- 刷LeetCode的正确姿势——第1、125题
最近刷LeetCode比较频繁,就购买了官方的参考电子书 (CleanCodeHandbook),里面有题目的解析和范例源代码,可以省去非常多寻找免费经验分享内容和整理这些资料的时间.惊喜的是,里面的 ...
- [LeetCode] Encode String with Shortest Length 最短长度编码字符串
Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...
- [LeetCode] All O`one Data Structure 全O(1)的数据结构
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
- [LeetCode] Majority Element 求众数
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [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 ...
- [LeetCode] Word Ladder II 词语阶梯之二
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- [LeetCode] Climbing Stairs 爬梯子问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LeetCode] ZigZag Converesion 之字型转换字符串
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- Leetcode分类刷题答案&心得
Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...
随机推荐
- while if 循环判断
temp=input("猜一下我想的那个数字吧:") guess=int(temp) while guess!=8: temp=input("诶呀错误了在输入一次吧:&q ...
- vim操作命令-笔记
显示行号:在vim命令行模式下输入 :set nu 或 :set number 取消显示行号:在vim命令行模式下输入 :set nonu 或 :set nonumber 查看文件编码格式: :set ...
- lsmod
http://blog.csdn.net/yuan892173701/article/details/8960607 抽空写下
- Kendo UI for Angular 2 控件
Kendo UI for Angular 2 控件 伴随着 Angular 2 的正式 release,Kendo UI for Angular 2 的第一批控件已经发布了,当前是 Beta 版本,免 ...
- C#关于窗体的keysdown事件,无法获取到焦点
当窗体中包含button之类的控件时,按下方向键时它们会自动获取焦点,导致窗体keysdown事件无法执行.解决方法很简单.将按钮之类控件放到panel容器中控件就无法获取焦点了.这时焦点会在整个窗体 ...
- hadoop job执行完的统计信息
Total committed heap usage (bytes)= Physical memory (bytes) snapshot= Virtual memory (bytes) snapsho ...
- Dao 处理
1. 写一个基础的接口和类来做基本的操作 /** * */ package com.wolfgang.dao; import java.util.List; /** * @author Adminis ...
- struts中的ignoreHierarchy 参数和root 参数
struts2 1.ignoreHierarchy 参数:表示是否忽略等级,也就是继承关系,比如:TestAction继承于BaseAction,那么TestAction中返回的json字符串默认是不 ...
- JavaScript+CSS实现经典的树形导航栏
在一些管理系统里面,一般右侧都会有树形的导航栏,点击一下就会出现下拉菜单,显示出来该父菜单下面的子菜单 项目,然后配以图片,和CSS的效果,可以说是非常常用的功能,现在做一个项目,正好用到这个功能,于 ...
- 数据结构(左偏树):HDU 1512 Monkey King
Monkey King Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...