LeetCode077 Combinations

来源:互联网 发布:淘宝店铺如何更换行业 编辑:程序博客网 时间:2024/05/29 02:18

详细见: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