Skip to content

Commit

Permalink
Merge pull request youngyangyang04#791 from KingArthur0205/remote
Browse files Browse the repository at this point in the history
添加 0491.递增子序列.md C语言版本
  • Loading branch information
youngyangyang04 authored Sep 29, 2021
2 parents f294123 + d39a728 commit a608847
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions problems/0491.递增子序列.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,72 @@ var findSubsequences = function(nums) {

```

C:
```c
int* path;
int pathTop;
int** ans;
int ansTop;
int* length;
//将当前path中的内容复制到ans中
void copy() {
int* tempPath = (int*)malloc(sizeof(int) * pathTop);
memcpy(tempPath, path, pathTop * sizeof(int));
length[ansTop] = pathTop;
ans[ansTop++] = tempPath;
}

//查找uset中是否存在值为key的元素
int find(int* uset, int usetSize, int key) {
int i;
for(i = 0; i < usetSize; i++) {
if(uset[i] == key)
return 1;
}
return 0;
}

void backTracking(int* nums, int numsSize, int startIndex) {
//当path中元素大于1个时,将path拷贝到ans中
if(pathTop > 1) {
copy();
}
int* uset = (int*)malloc(sizeof(int) * numsSize);
int usetTop = 0;
int i;
for(i = startIndex; i < numsSize; i++) {
//若当前元素小于path中最后一位元素 || 在树的同一层找到了相同的元素,则continue
if((pathTop > 0 && nums[i] < path[pathTop - 1]) || find(uset, usetTop, nums[i]))
continue;
//将当前元素放入uset
uset[usetTop++] = nums[i];
//将当前元素放入path
path[pathTop++] = nums[i];
backTracking(nums, numsSize, i + 1);
//回溯
pathTop--;
}
}

int** findSubsequences(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){
//辅助数组初始化
path = (int*)malloc(sizeof(int) * numsSize);
ans = (int**)malloc(sizeof(int*) * 33000);
length = (int*)malloc(sizeof(int*) * 33000);
pathTop = ansTop = 0;

backTracking(nums, numsSize, 0);

//设置数组中返回元素个数,以及每个一维数组的长度
*returnSize = ansTop;
*returnColumnSizes = (int*)malloc(sizeof(int) * ansTop);
int i;
for(i = 0; i < ansTop; i++) {
(*returnColumnSizes)[i] = length[i];
}
return ans;
}
```
-----------------------
Expand Down

0 comments on commit a608847

Please sign in to comment.