40. Combination Sum II
Medium
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.
Each number in candidates may only be used once in the combination.
Note:
- All numbers (including
target) will be positive integers. - The solution set must not contain duplicate combinations.
Example 1:
1 | |
Example 2:
1 | |
1 | |
41. First Missing Positive
Hard
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
1 | |
Example 2:
1 | |
Example 3:
1 | |
最直接的思路就是排序,然后滤掉负值,进行判断
1 | |
这种思路很直接,但是由于排序算法还要自己实现的话很麻烦。
另一种思路就更简单了,我们直接按照正整数排列
对于-1,2,4,1,3,第0位必须是1,以此类推,因此有效的第i项需要放在nums[i]-1项,那么交换之后的第i项同样需要换位置,直到他是一个无效值或者处于正确的位置
1 | |
或者用字典处理
1 | |