LeetCode077 Combinations

详细见:leetcode.com/problems/combinations


Java Solution: github

package leetcode;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/*
 * 	Given two integers n and k, return all possible combinations 
 * 	of k numbers out of 1 ... n.

	For example,
	If n = 4 and k = 2, a solution is:
	
	[
	  [2,4],
	  [3,4],
	  [2,3],
	  [1,2],
	  [1,3],
	  [1,4],
	]
 */

public class P077_Combinations {
	public static void main(String[] args) {
		List<List<Integer>> ans = new Solution().combine(5, 4);
		Iterator<List<Integer>> it = ans.iterator();
		while (it.hasNext()) {
			tools.Utils.B_打印List_Integer_OneLine(it.next());
		}
	}
	/*
	 * 	挺简单的一道题,却AC得非常虚。。。虽然一次AC
	 * 	诶,自己还是得多练回溯
	 * 	39 ms
	 */
	static class Solution {
	    public List<List<Integer>> combine(int n, int k) {
	    	List<List<Integer>> ans = new LinkedList<List<Integer>>();
	    	int[] ks = new int[k];
	    	search(ks, 0, ans, n, k, 0);
	    	return ans;
	    }
	    void search(int[] ks, int ki, List<List<Integer>> ans, int n, int k, int ni) {
	    	if (ki == k) {
	    		List<Integer> temp = new ArrayList<Integer>(k);
	    		for (int i = 0; i != k; i ++)
	    			temp.add(ks[i]);
	    		ans.add(temp);
	    		return;
	    	}
	    	for (int i = ni; i != n; i ++) {
	    		if (ki != 0 && ks[ki - 1] >= i + 1)
	    			continue;
	    		ks[ki] = i + 1;
	    		ni ++;
	    		search(ks, ki + 1, ans, n, k, ni);
	    		ni --;
	    	}
	    }
	}
}


C Solution: github

/*
    url: leetcode.com/problems/combinations/
    AC 33ms 97.06%
*/

#include <stdio.h>
#include <stdlib.h>

typedef int* T;
typedef struct al sal;
typedef struct al * pal;

struct al {
    int capacity;
    int size;
    T* arr;
};

pal al_init(int capacity) {
    pal l = (pal) malloc(sizeof(sal));
    if (capacity < 1) return NULL;
    l->arr = (T*) malloc(sizeof(T) * capacity);
    l->capacity = capacity;
    l->size = 0;
    return l;
}

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 ++;
}

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

int* arr_copy(int* save, int n) {
    int* copy = (int*) malloc(sizeof(int) * n);
    int i = 0;
    for (i = 0; i < n; i ++)
        copy[i] = save[i];
    return copy;
}

void search(int ni, int n, int* save, int si, int sn, pal l) {
    int k = 0;
    if (si == sn) {
        al_add_last(l, arr_copy(save, sn));
        return;
    }
    for (k = ni; k <= n; k ++) {
        save[si] = k;
        search(k+1, n, save, si+1, sn, l);
    }
}

int** combine(int n, int k, int** cn, int* ren) {
    pal l = al_init(16);
    int i = 0;
    int* save = (int*) malloc(sizeof(int) * k);
    search(1, n, save, 0, k, l);
    *ren = l->size;
    *cn = (int*) malloc(sizeof(int) * l->size);
    for (i = 0; i < l->size; i ++)
        (*cn)[i] = k;
    return al_convert_to_array_free_l(l);
}


Python Solution: github

#coding=utf-8

'''
    url: leetcode.com/problems/combinations
    @author:     zxwtry
    @email:      zxwtry@qq.com
    @date:       2017年4月18日
    @details:    Solution: 779ms 13.24%
'''

class Solution(object):
    def search(self, n, ni, nn, s, si, sn, a):
        if si == sn:
            a.append(list(s))
            return
        for i in range(ni , nn):
            s[si] = n[i]
            self.search(n, i+1, nn, s, si+1, sn, a)
            
    def combine(self, n, k):
        """
        :type n: int
        :type k: int
        :rtype: List[List[int]]
        """
        a, s = [], [0]*k
        self.search(n, 0, len(n), s, 0, k, a)
        return a


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值