LeetCode039 Combination Sum
2017-04-05 15:47
471 查看
详细见:leetcode.com/problems/combination-sum
Java Solution:
github
C Solution:
github
/*
url: leetcode.com/problems/combination-sum/
AC 9ms 58.82%
*/
#include <stdio.h>
#include <stdlib.h>
//array list start
typedef int* T;
typedef struct al sal;
typedef struct al * pal;
struct al {
int capacity;
int size;
int* rs;
T* arr;
};
void al_expand_capacity(pal l) {
T* new_arr = (T*) malloc(sizeof(T) * (l->capacity * 2 + 1));
int* new_rs = (int*) malloc(sizeof(int) * (l->capacity * 2 + 1));
int i = 0;
for (i = 0; i < l->capacity; i ++) {
new_arr[i] = l->arr[i];
new_rs[i] = l->rs[i];
}
free(l->arr);
free(l->rs);
l->arr = new_arr;
l->rs = new_rs;
l->capacity = l->capacity * 2 + 1;
}
void al_add_last(pal l, T v, int s) {
if (l->capacity == l->size) al_expand_capacity(l);
l->arr[l->size] = v;
l->rs[l->size] = s;
l->size ++;
}
//array list end
//search start
void search(pal l, int* c, int ci, int cs, int t, int* r, int ri) {
int i = 0, *tmp1, times;
if (t == 0) {
tmp1 = (int*) malloc(sizeof(int) * ri);
for (i = 0; i < ri; i ++)
tmp1[i] = r[i];
al_add_last(l, tmp1, ri);
return;
}
if (t < 0 || ci == cs) return;
for (times = 0; times <= t / c[ci]; times ++) {
for (i = 0; i < times; i ++) {
r[ri + i] = c[ci];
}
search(l, c, ci + 1, cs, t - times * c[ci], r, ri + times);
}
}
//search end
int _min(int a, int b) {
return a < b ? a : b;
}
//use array list as answer
int** combinationSum(int* c, int cs, int t, int** rcs, int* rs) {
pal l = (pal) malloc(sizeof(sal));
int* r = NULL, **ans = NULL, i = 0, mc = cs <= 0 ? 1 : c[0];
if (cs <= 0) return NULL;
l->capacity = 16;
l->arr = (T*) malloc(sizeof(T) * l->capacity);
l->rs = (int*) malloc(sizeof(int) * l->capacity);
l->size = 0;
for (i = 1; i < cs; i ++)
mc = _min(mc, c[i]);
r = (int*) malloc(sizeof(int) * (t / mc + 10));
search(l, c, 0, cs, t, r, 0);
(*rcs) = l->rs;
(*rs) = l->size;
ans = l->arr;
free(l);
return ans;
}
int main() {
int c[] = {2,3,7};
int cs = 3;
int t = 7;
int* rcs = NULL;
int rs = 0;
int** answer = combinationSum(c, cs, t, &rcs, &rs);
int i = 0, j = 0;
printf("rs is %d \r\n", rs);
for (i = 0; i < rs; i ++) {
for (j = 0; j < rcs[i]; j ++) {
printf("%d ", answer[i][j]);
}
printf("\r\n");
free(answer[i]);
}
free(answer);
free(rcs);
}
Python Solution:
github
Java Solution:
github
package leetcode; import java.util.Arrays; import java.util.LinkedList; import java.util.List; public class P039_Combination_Sum { public static void main(String[] args) { System.out.println(new Solution1().combinationSum(new int[] {10, 1, 2, 7, 6, 1, 5, 1, 1, 1, 1}, 8)); } /* * 6 ms * 62.68% */ static class Solution1 { boolean[] isAccess = null; int[] isAnswer = null; List<List<Integer>> ans = null; public List<List<Integer>> combinationSum(int[] candidates, int target) { if (candidates == null || candidates.length == 0) return null; Arrays.sort(candidates); isAccess = new boolean[candidates.length]; isAnswer = new int[candidates.length]; isAccess[0] = true; for (int i = 1; i != candidates.length; i ++) isAccess[i] = candidates[i] == candidates[i - 1] ? false : true; ans = new LinkedList<List<Integer>>(); searchAllAns(candidates, 0, target); return ans; } private void searchAllAns(int[] candidates, int index, int target) { if (index == candidates.length || target < 0) return; if (target == 0) { List<Integer> answer = new LinkedList<Integer>(); for (int i = 0; i != isAnswer.length; i ++) for (int j = 0; j != isAnswer[i]; j ++) answer.add(candidates[i]); if (answer.size() != 0) ans.add(answer); } for (int i = index; i != candidates.length; i ++) { if (! isAccess[i]) continue; isAnswer[i] ++; searchAllAns(candidates, i, target - candidates[i]); isAnswer[i] --; } } } }
C Solution:
github
/*
url: leetcode.com/problems/combination-sum/
AC 9ms 58.82%
*/
#include <stdio.h>
#include <stdlib.h>
//array list start
typedef int* T;
typedef struct al sal;
typedef struct al * pal;
struct al {
int capacity;
int size;
int* rs;
T* arr;
};
void al_expand_capacity(pal l) {
T* new_arr = (T*) malloc(sizeof(T) * (l->capacity * 2 + 1));
int* new_rs = (int*) malloc(sizeof(int) * (l->capacity * 2 + 1));
int i = 0;
for (i = 0; i < l->capacity; i ++) {
new_arr[i] = l->arr[i];
new_rs[i] = l->rs[i];
}
free(l->arr);
free(l->rs);
l->arr = new_arr;
l->rs = new_rs;
l->capacity = l->capacity * 2 + 1;
}
void al_add_last(pal l, T v, int s) {
if (l->capacity == l->size) al_expand_capacity(l);
l->arr[l->size] = v;
l->rs[l->size] = s;
l->size ++;
}
//array list end
//search start
void search(pal l, int* c, int ci, int cs, int t, int* r, int ri) {
int i = 0, *tmp1, times;
if (t == 0) {
tmp1 = (int*) malloc(sizeof(int) * ri);
for (i = 0; i < ri; i ++)
tmp1[i] = r[i];
al_add_last(l, tmp1, ri);
return;
}
if (t < 0 || ci == cs) return;
for (times = 0; times <= t / c[ci]; times ++) {
for (i = 0; i < times; i ++) {
r[ri + i] = c[ci];
}
search(l, c, ci + 1, cs, t - times * c[ci], r, ri + times);
}
}
//search end
int _min(int a, int b) {
return a < b ? a : b;
}
//use array list as answer
int** combinationSum(int* c, int cs, int t, int** rcs, int* rs) {
pal l = (pal) malloc(sizeof(sal));
int* r = NULL, **ans = NULL, i = 0, mc = cs <= 0 ? 1 : c[0];
if (cs <= 0) return NULL;
l->capacity = 16;
l->arr = (T*) malloc(sizeof(T) * l->capacity);
l->rs = (int*) malloc(sizeof(int) * l->capacity);
l->size = 0;
for (i = 1; i < cs; i ++)
mc = _min(mc, c[i]);
r = (int*) malloc(sizeof(int) * (t / mc + 10));
search(l, c, 0, cs, t, r, 0);
(*rcs) = l->rs;
(*rs) = l->size;
ans = l->arr;
free(l);
return ans;
}
int main() {
int c[] = {2,3,7};
int cs = 3;
int t = 7;
int* rcs = NULL;
int rs = 0;
int** answer = combinationSum(c, cs, t, &rcs, &rs);
int i = 0, j = 0;
printf("rs is %d \r\n", rs);
for (i = 0; i < rs; i ++) {
for (j = 0; j < rcs[i]; j ++) {
printf("%d ", answer[i][j]);
}
printf("\r\n");
free(answer[i]);
}
free(answer);
free(rcs);
}
Python Solution:
github
#coding=utf-8 ''' url: leetcode.com/problems/combination-sum/ @author: zxwtry @email: zxwtry@qq.com @date: 2017年4月5日 @details: Solution: 245ms 14.70% ''' class Solution(object): def search(self, c, ci, t, ans, rec, ri): if t == 0: ans_t = [] for i in range(ri): ans_t.append(rec[i]) ans.append(ans_t) return elif t < 0:return if ci == len(c): return for j in range((t // c[ci]) + 1): for i in range(j): rec[ri+i]=c[ci] self.search(c, ci+1, t-j*c[ci], ans, rec, ri+j) def combinationSum(self, c, t): """ :type c: List[int] :type t: int :rtype: List[List[int]] """ ans,rec=[],[0]*(t//min(c)+1) self.search(c, 0, t, ans, rec, 0) return ans if __name__ == "__main__": c, t = [2, 3, 6, 7], 7 ans = Solution().combinationSum(c, t) print(ans)
相关文章推荐
- Java for LeetCode 039 Combination Sum
- [LeetCode]题解(python):039-Combination Sum
- [LeetCode]039-Combination Sum
- LeetCode 039 Combination Sum
- LeetCode 039 Combination Sum
- LeetCode 039 Combination Sum
- LeetCode 039 Combination Sum
- [LeetCode]39 Combination Sum
- LeetCode: Combination Sum
- Leetcode--Combination Sum
- LeetCode 114 Combination Sum
- 【Leetcode】Combination Sum (Backtracking)
- [leetcode 39] Combination Sum
- [LeetCode][Java] Combination Sum
- [Leetcode] Combination Sum
- LeetCode 38 Combination Sum
- [LeetCode] 017: Combination Sum
- 【LeetCode】Combination Sum - Medium
- 回溯法(leetcode-Combination Sum)
- Leetcode: Combination Sum