LeetCode40 Combination Sum II 解析

详细见:leetcode.com/problems/combination-sum-ii


C和Python的去重算法,应该记住。

Java是很久之前写的,并不好。

规则是:相同数字。

        1,前面选了,后面一定要选。

        2,前面没选,后面可选可不选。

注意一定要选到最后一位,才能判断。


Java Solution: github

package leetcode;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;


public class P040_CombinationSumII {
	public static void main(String[] args) {
//		System.out.println(new Solution2().combinationSum2(new int[] {10, 1, 2, 7, 6, 1, 5}, 8));
//		System.out.println(new Solution2().combinationSum2(new int[] {2, 5, 2, 1, 2}, 5));
		System.out.println(new Solution2().combinationSum2(new int[] {3,1,3,5,1,1}, 8));
	}
	
	/*
	 * 	15 ms
	 * 	26.62%
	 */
	static class Solution2 {
		List<List<Integer>> ans = null;
		boolean[] isAnswer = null;
		int[] index1 = null, index2 = null;
		public List<List<Integer>> combinationSum2(int[] candidates, int target) {
			if (candidates == null || candidates.length == 0)
	    		return ans;
			Arrays.sort(candidates);
			ans = new LinkedList<List<Integer>>();
			isAnswer = new boolean[candidates.length];
			index1 = new int[candidates.length];
			index1[0] = 0;
			for (int i = 1; i != candidates.length; i ++)
				index1[i] = candidates[i] == candidates[i - 1] ? index1[i - 1] : i;
			index2 = new int[candidates.length];
			index2[index2.length - 1] = index2.length - 1;
			for (int i = index2.length - 2; i != -1; i --)
				index2[i] = candidates[i] == candidates[i + 1] ? index2[i + 1] : i;
			searchAllAns(candidates, -1, target);
			return ans;
		}
		private void searchAllAns(int[] candidates, int index, int target) {
			if (target == 0) {
				List<Integer> answer = new LinkedList<Integer>();
				for (int i = 0; i != candidates.length; i ++)
					if (isAnswer[i])
						answer.add(candidates[i]);
				if (answer.size() != 0)
					ans.add(answer);
			}
			if (index == candidates.length || target < 0)
				return;
			for (int i = index + 1; i != candidates.length; i ++) {
				if (isAnswer[i])
					continue;
				if (index1[i] == i && index2[i] == i) {
					isAnswer[i] = true;
					searchAllAns(candidates, i, target - candidates[i]);
					isAnswer[i] = false;
				} else {
					int num = index2[i] - index1[i] + 1;
					i = index2[i];
					for (int j = 1; j <= num; j ++) {
						for (int k = 0; k != j; k ++)
							isAnswer[k + index1[i]] = true;
						searchAllAns(candidates, i, target - j * candidates[i]);
						for (int k = 0; k != j; k ++)
							isAnswer[k + index1[i]] = false;
					}
				}
			}
		}
	}
}


C Solution: github

/*
    url: leetcode.com/problems/combination-sum-ii/
    AC 12ms 27.27%
*/

#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;
    T* arr;
};

void al_expand_capacity(pal l) {
    T* new_arr = (T*) malloc(sizeof(T) * (l->capacity * 2 + 1));
    int i = 0;
    for (i = 0; i < l->capacity; i ++)
        new_arr[i] = l->arr[i];
    free(l->arr);
    l->arr = new_arr;
    l->capacity = l->capacity * 2 + 1;
}

void al_add_last(pal l, T v) {
    if (l->capacity == l->size) al_expand_capacity(l);
    l->arr[l->size] = v;
    l->size ++;
}

void al_add_first(pal l, T v) {
    int i = 0;
    if (l->capacity == l->size) al_expand_capacity(l);
    for (i = l->size; i > 0; i --)
        l->arr[i] = l->arr[i - 1];
    l->arr[0] = v;
    l->size ++;
}

void al_add_to_index(pal l, T v, int index) {
    int i = 0;
    if (index > l->size) return;
    if (l->capacity == l->size) al_expand_capacity(l);
    for (i = l->size - 1; i >= index; i --) {
        l->arr[i+1] = l->arr[i];
    }
    l->arr[index] = v;
    l->size ++;
}

//if T is ptr, need to free l->size - 1
void al_remove_last(pal l) {
    if (l->size == 0) return;
    l->arr[l->size - 1] = 0; //or NULL and free
    l->size --;
}

//if T is ptr, need to free 0
void al_remove_first(pal l) {
    int i = 0;
    if (l->size == 0) return;
    l->arr[0] = 0; //or NULL and free
    for (i = 1; i < l->size; i ++) {
        l->arr[i - 1] = l->arr[i];
    }
    l->size --;
}

T* al_convert_to_array_free_l(pal l) {
    T* arr = l->arr;
    free(l);
    return arr;
}

T al_access_by_index(pal l, int index) {
    if (index >= l->size || index < 0) return 0;
    return l->arr[index];
}

void al_free_all(pal l) {
    free(l->arr);
    free(l);
}

void al_print(pal l) {
    int i = 0;
    if (l->size == 0) return;
    for (i = 0; i < l->size; i ++)
        printf("%d ", l->arr[i]);
    printf("\r\n");
}

//array list end


//quick srot start

//[l, r]
int _partition(int* n, int l, int r) {
    int s = *(n + l);
    while (l < r) {
        while (l < r && *(n + r) >= s) r --;
        *(n + l) = *(n + r);
        while (l < r && *(n + l) <= s) l ++;
        *(n + r) = *(n + l);
    }
    *(n + l) = s;
    return l;
}

//[l, r)
void _quick_sort(int* n, int l, int r) {
    int p = 0;
    if (l < r) {
        p = _partition(n, l, r - 1);
        _quick_sort(n, l, p);
        _quick_sort(n, p + 1, r);
    }
}

void quick_sort(int* n, int s) {
    _quick_sort(n, 0, s);
}

//quick sort end

void search(pal l, int* c, int ci, int cn, int** rn, int t, int sign, int* s, int si) {
    int* temp = NULL, i = 0;
    if (t == 0 && ci == cn) {
        temp = (int*) malloc(sizeof(int) * si);
        for (i = 0; i < si; i ++)
            temp[i] = s[i];
        (*rn)[l->size] = si;
        al_add_last(l, temp);
        return;
    }
    if (t < 0 || ci == cn) return;
    // sign = 1 and c[ci - 1] == c[ci] : not
    if (!(sign && c[ci - 1] == c[ci])) {
        search(l, c, ci + 1, cn, rn, t, 0, s, si);
    }
    s[si] = c[ci];
    search(l, c, ci+1, cn, rn, t-c[ci], 1, s, si+1);
}

int** combinationSum2(int* c, int cn, int t, int** rn, int* r) {
    int* s = (int*) malloc(sizeof(int) * cn);
    pal l = (pal) malloc(sizeof(sal));
    l->size = 0;
    l->capacity = 16;
    l->arr = (T*) malloc(sizeof(T) * l->capacity);
    quick_sort(c, cn);
    *rn = (int*) malloc(sizeof(int) * (cn*cn));
    search(l, c, 0, cn, rn, t, 0, s, 0);
    *r = l->size;
    free(s);
    return al_convert_to_array_free_l(l);
}

int main() {
    int c[] = {1, 1};
    int cn = 2; 
    int t = 1;
    int* rn = NULL;
    int r = 0;
    int** ans = combinationSum2(c, cn, t, &rn, &r);
    int i = 0, j = 0;
    printf("r is %d\r\n", r);
    for (i = 0; i < r; i ++) {
        for (j = 0; j < rn[i]; j ++)
            printf("%d ", ans[i][j]);
        printf("\r\n");
        free(ans[i]);
    }
    free(ans);
    return 0;
}




Python Solution: github

#coding=utf-8

'''
    url: leetcode.com/problems/combination-sum-ii/
    @author:     zxwtry
    @email:      zxwtry@qq.com
    @date:       2017年4月6日
    @details:    Solution: 286ms 16.23%
'''

class Solution(object):
    def search(self, ans, s, si, c, ci, cn, t, sign):
        if t == 0 and ci == cn:
            ans_t=[0]*si
            for i in range(si):
                ans_t[i] = s[i]
            ans.append(ans_t)
            return
        if t < 0 or ci == cn:
            return
        if not(sign and c[ci - 1] == c[ci]):
            self.search(ans, s, si, c, ci+1, cn, t, False)
        s[si] = c[ci]
        self.search(ans, s, si+1, c, ci+1, cn, t-c[ci], True)
        
                
    def combinationSum2(self, c, t):
        """
        :type c: List[int]
        :type t: int
        :rtype: List[List[int]]
        """
        cn = 0 if c == None else len(c)
        if cn == 0: return []
        c.sort(key=None, reverse=False)
        s, si, ans=[0]*cn, 0, []
        self.search(ans, s, si, c, 0, cn, t, False)
        return ans

if __name__ == "__main__":
    print(Solution().combinationSum2([1,1], 1))


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值