Leetcode 77. Combinations
1323 ワード
タイトル
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],]
ぶんせき
基本的な組合せ問題は,再帰や遡及などのアルゴリズムを用いることができる.12−13−14−23−24−34のようなインクリメントを使用し、後から最大nに順次インクリメントし、最大に達すると、前のインクリメントが見つかり、その後の数字は順次インクリメントする必要がある.例えば145-234ここでは、モデルを持参する必要があります.そうでなければ(*columnSizes)[i]=k;メモリの位置合わせの問題を報告して、このバグを探して長い間探してやっと見つけました...
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],]
ぶんせき
基本的な組合せ問題は,再帰や遡及などのアルゴリズムを用いることができる.12−13−14−23−24−34のようなインクリメントを使用し、後から最大nに順次インクリメントし、最大に達すると、前のインクリメントが見つかり、その後の数字は順次インクリメントする必要がある.例えば145-234ここでは、モデルを持参する必要があります.そうでなければ(*columnSizes)[i]=k;メモリの位置合わせの問題を報告して、このバグを探して長い間探してやっと見つけました...
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *columnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int** combine(int n, int k, int** columnSizes, int* returnSize) {
int n1=n,k1=k;
if(n1=1)
{
temp1=temp1*n1;
n1--;
temp2=temp2*k1;
k1--;
}
*returnSize=temp1/temp2;
int ** ans=(int**)malloc(sizeof(int *)*(*returnSize));
*columnSizes=(int *)malloc(sizeof(int)*(*returnSize));
for(int i=0;i=n+p-k+1&&p>=0)
p--;
if(p==k-1)
{
ans[i+1][p]=ans[i+1][p]+1;
}
else if(p>=0)
{
ans[i+1][p]=ans[i+1][p]+1;
for(int j=p+1;j