Merge branch 'master' of github.com:youngyangyang04/leetcode-master

This commit is contained in:
youngyangyang04
2021-09-24 11:06:13 +08:00
3 changed files with 102 additions and 2 deletions

View File

@ -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++) {

View File

@ -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;
}
```
-----------------------

View File

@ -222,7 +222,7 @@ class Solution:
```
Go
```golang
```go
func reconstructQueue(people [][]int) [][]int {
//先将身高从大到小排序,确定最大个子的相对位置
sort.Slice(people,func(i,j int)bool{
@ -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 = []