Skip to content

Commit

Permalink
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
Browse files Browse the repository at this point in the history
  • Loading branch information
youngyangyang04 committed Sep 24, 2021
2 parents 3221a85 + 78dcace commit 0f0bb6e
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
3 changes: 2 additions & 1 deletion problems/0053.最大子序和(动态规划).md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ JavaScript:
```javascript
const maxSubArray = nums => {
// 数组长度,dp初始化
const [len, dp] = [nums.length, [nums[0]]];
const len = nums.length;
let dp = new Array(len).fill(0);
// 最大值初始化为dp[0]
let max = dp[0];
for (let i = 1; i < len; i++) {
Expand Down
68 changes: 68 additions & 0 deletions problems/0090.子集II.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,74 @@ var subsetsWithDup = function(nums) {

```

C:
```c
int* path;
int pathTop;
int** ans;
int ansTop;
//负责存放二维数组中每个数组的长度
int* lengths;
//快排cmp函数
int cmp(const void* a, const void* b) {
return *((int*)a) - *((int*)b);
}

//复制函数,将当前path中的元素复制到ans中。同时记录path长度
void copy() {
int* tempPath = (int*)malloc(sizeof(int) * pathTop);
int i;
for(i = 0; i < pathTop; i++) {
tempPath[i] = path[i];
}
ans = (int**)realloc(ans, sizeof(int*) * (ansTop + 1));
lengths[ansTop] = pathTop;
ans[ansTop++] = tempPath;
}

void backTracking(int* nums, int numsSize, int startIndex, int* used) {
//首先将当前path复制
copy();
//若startIndex大于数组最后一位元素的位置,返回
if(startIndex >= numsSize)
return ;

int i;
for(i = startIndex; i < numsSize; i++) {
//对同一树层使用过的元素进行跳过
if(i > 0 && nums[i] == nums[i-1] && used[i-1] == false)
continue;
path[pathTop++] = nums[i];
used[i] = true;
backTracking(nums, numsSize, i + 1, used);
used[i] = false;
pathTop--;
}
}

int** subsetsWithDup(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){
//声明辅助变量
path = (int*)malloc(sizeof(int) * numsSize);
ans = (int**)malloc(0);
lengths = (int*)malloc(sizeof(int) * 1500);
int* used = (int*)malloc(sizeof(int) * numsSize);
pathTop = ansTop = 0;

//排序后查重才能生效
qsort(nums, numsSize, sizeof(int), cmp);
backTracking(nums, numsSize, 0, used);

//设置一维数组和二维数组的返回大小
*returnSize = ansTop;
*returnColumnSizes = (int*)malloc(sizeof(int) * ansTop);
int i;
for(i = 0; i < ansTop; i++) {
(*returnColumnSizes)[i] = lengths[i];
}
return ans;
}
```
-----------------------
Expand Down
33 changes: 32 additions & 1 deletion problems/0406.根据身高重建队列.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class Solution:
```

Go:
```golang
```go
func reconstructQueue(people [][]int) [][]int {
//先将身高从大到小排序,确定最大个子的相对位置
sort.Slice(people,func(i,j int)bool{
Expand All @@ -241,7 +241,38 @@ func reconstructQueue(people [][]int) [][]int {
return result
}
```
```go
//链表法
func reconstructQueue(people [][]int) [][]int {
sort.Slice(people,func (i,j int) bool {
if people[i][0]==people[j][0]{
return people[i][1]<people[j][1]//当身高相同时,将K按照从小到大排序
}
//先将身高从大到小排序,确定最大个子的相对位置
return people[i][0]>people[j][0]
})
l:=list.New()//创建链表
for i:=0;i<len(people);i++{
position:=people[i][1]
mark:=l.PushBack(people[i])//插入元素
e:=l.Front()
for position!=0{//获取相对位置
position--
e=e.Next()
}
l.MoveBefore(mark,e)//移动位置

}
res:=[][]int{}
for e:=l.Front();e!=nil;e=e.Next(){
res=append(res,e.Value.([]int))
}
return res
}
```

Javascript:

```Javascript
var reconstructQueue = function(people) {
let queue = []
Expand Down

0 comments on commit 0f0bb6e

Please sign in to comment.