Skip to content

Commit 3a7b041

Browse files
edit 216
1 parent ae684d3 commit 3a7b041

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ Your ideas/fixes/algorithms are more than welcome!
358358
|219|[Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/ContainsDuplicateII.java)| O(n)|O(n) | Easy| HashMap
359359
|218|[The Skyline Problem](https://leetcode.com/problems/the-skyline-problem/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_218.java)| O(n)|O(n) | Hard| TreeMap, Design
360360
|217|[Contains Duplicate](https://leetcode.com/problems/contains-duplicate/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_217.java)| O(n)|O(n) | Easy| HashSet
361+
|216|[Combination Sum III](https://leetcode.com/problems/combination-sum-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_216.java)| O(k * C(n, k))|O(k) | Medium| Backtracking
361362
|215|[Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_215.java)| O(nlogn)|O(n) | Medium| Heap
362363
|214|[Shortest Palindrome](https://leetcode.com/problems/shortest-palindrome/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_214.java)| O(?)|O(?)| Hard | KMP
363364
|213|[House Robber II](https://leetcode.com/problems/house-robber-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_213.java)| O(n)|O(n)| Medium | DP

‎src/main/java/com/fishercoder/solutions/CombinationSumIII.java renamed to ‎src/main/java/com/fishercoder/solutions/_216.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import java.util.*;
44

5-
/**Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.
5+
/**
6+
* 216. Combination Sum III
7+
* Find all possible combinations of k numbers that add up to a number n,
8+
* given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.
69
710
811
Example 1:
@@ -20,7 +23,8 @@
2023
Output:
2124
2225
[[1,2,6], [1,3,5], [2,3,4]]*/
23-
public class CombinationSumIII {
26+
public class _216 {
27+
2428
public List<List<Integer>> combinationSum3(int k, int n) {
2529
List<List<Integer>> result = new ArrayList();
2630
int[] nums = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
@@ -32,11 +36,9 @@ void helper(int k, int n, int[] nums, int start, List<Integer> curr, List<List<I
3236
if (n > 0) {
3337
for (int i = start; i < nums.length; i++) {
3438
curr.add(nums[i]);
35-
helper(k, n - nums[i], nums, i + 1, curr, result);// it needs to be a unique set of
36-
// numbers, so we need to set it
37-
// as i+1 here: each number is
38-
// used only once in this array:
39-
// [1,2,3,4,5,6,7,8,9]
39+
/** it needs to be a unique set of numbers, so we need to set it
40+
as i+1 here: each number is used only once in this array: [1,2,3,4,5,6,7,8,9]*/
41+
helper(k, n - nums[i], nums, i + 1, curr, result);
4042
curr.remove(curr.size() - 1);
4143
}
4244
} else if (n == 0 && curr.size() == k) {//this is the major difference here: check size of curr list is of k before adding it

0 commit comments

Comments
 (0)