-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path39.java
More file actions
161 lines (148 loc) · 5.3 KB
/
39.java
File metadata and controls
161 lines (148 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
__________________________________________________________________________________________________
0ms
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> ans = new LinkedList<List<Integer>>();
if (candidates == null || candidates.length == 0) {
return ans;
}
int[] visited = new int[candidates.length];
bt(candidates, visited, 0, target, ans);
return ans;
}
public void bt(int[] candidates, int[] visited, int start, int target, List<List<Integer>> ans) {
if (target == 0) {
List<Integer> combination = new LinkedList<>();
for (int i = 0; i < visited.length; i++) {
for (int j = 0; j < visited[i]; j++) {
combination.add(candidates[i]);
}
}
ans.add(combination);
return;
}
for (int i = start; i < candidates.length; i++) {
if (target - candidates[i] >= 0) {
visited[i]++;
bt(candidates, visited, i, target - candidates[i], ans);
visited[i]--;
}
}
}
}
__________________________________________________________________________________________________
2ms
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> res = new ArrayList<>();
cal(candidates, target, res, new ArrayList<>(), 0);
return res;
}
public void cal(int[] ll, int target, List<List<Integer>> res, List<Integer> row, int k){
for(int i = k; i < ll.length; i++){
if(target == ll[i]){
row.add(ll[i]);
res.add(new ArrayList<>(row));
row.remove(row.size()-1);
break;
} else if(target < ll[i]) {
break;
} else {
row.add(ll[i]);
cal(ll, target - ll[i], res, row, i);
row.remove(row.size()-1);
}
}
}
}
__________________________________________________________________________________________________
5ms
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> res = new ArrayList<List<Integer>>();
Stack<Integer> temp = new Stack<Integer>();
int tempCount = 0;
int used = -1;
combine(res, candidates, target, temp, tempCount, used);
return res;
}
public void combine(List<List<Integer>> res, int[] candidates, int target, Stack<Integer> temp, int tempCount, int used){
for(int i = 0; i < candidates.length; i++){
if(i < used){
continue;
}
if(tempCount + candidates[i] == target){
temp.push(candidates[i]);
List<Integer> tempRes = new ArrayList<Integer>();
tempRes.addAll(temp);
res.add(tempRes);
temp.pop();
return;
}else if(tempCount + candidates[i] > target){
return;
}else{
temp.push(candidates[i]);
combine(res, candidates, target, temp, tempCount+candidates[i], i);
temp.pop();
}
}
}
}
__________________________________________________________________________________________________
35752 kb
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
List<List<Integer>> [] dp = new List[target + 1];
dp[0] = new ArrayList<>();
for(int i = 1; i <= target; i++)
{
dp[i] = new ArrayList<>();
for(int coin: candidates)
{
if(coin > i) continue;
if(coin == i)
{
dp[i].add(Arrays.asList(coin));
} else {
for(List<Integer> list: dp[i - coin])
{
if(coin >= list.get(list.size() - 1))
{
List<Integer> tmp = new ArrayList<>(list);
tmp.add(coin);
dp[i].add(tmp);
}
}
}
}
}
return dp[target];
}
}
__________________________________________________________________________________________________
35816 kb
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new LinkedList<>();
List<Integer> permutes = new LinkedList<>();
dfs(res, permutes,candidates, target,0);
return res;
}
public void dfs(List<List<Integer>> res, List<Integer> permutes, int[] candidates, int target,int start){
if(target == 0 ){
res.add(new LinkedList<>(permutes));
return;
}
for(int i = start;i<candidates.length;i++){
if(candidates[i] <= target){
permutes.add(candidates[i]);
dfs(res, permutes,candidates,target-candidates[i],i);
permutes.remove(permutes.size()-1);
}
}
}
}
__________________________________________________________________________________________________