刷leetcode的简单题第一次复盘(二)

N叉树的数据结构

struct Node{
	int val;
	int numChildren;
	struct Node **children;
}

求N叉树的最大深度

int maxDepth(struct Node *root){
	if(root==NULL){
		return 0;
	}
	int max = 0;
	for(int i=0;i<root->numChildren;i++){
		int temp = maxDepth(root->children[i]);
		max = max >= temp?max:temp;
	}
	max = max+1;
	return max;
}

另一个树的子树

bool compare(struct TreeNode *p1,struct TreeNode *p2){
	if(p1==NULL&&p2==NULL){
		return true;
	}
	if(p1==NULL||p2==NULL){
		return false;
	}
	if(p1->val!=p2->val){
		return false;
	}
	return compare(p1->left,p2->left)&&compare(p1->right,p2->right);
}

bool isSubTree(struct TreeNode *s,struct TreeNode *t){
	if(S==NULL&&t!=NULL){
		return false;
	}
	if(compare(s,t)==true){
		return true;
	}
	return isSubTree(s->left,t)||isSubTree(s->right,t);
}

qsort函数,快排,声明在stdlib.h中,根据二分法写的,时间复杂度为n*log(n)
qsort参数1:待排序数组,排序之后的结果仍然放在这个数组中
2:数组中排序元素的个数
3.各元素的占用空间大小
4.指向函数的指针,用于确定排序的顺序
qsort要求提供一个自己定义的比较函数

对于int型数组排序

int num[100]
int cmp_int(const void *a,const void *b){
	return *(int *)a-*(int *)b;
}
qsort(num,100,sizeof(int),cmp_int);

分糖果(偶数长度的数组,不同数字代表不同种类的糖果,每个数字代表一个糖果,均分给弟弟,妹妹,求使妹妹获得最大糖果的种类数,如果糖果的种类数大于等于长度的一半,则妹妹可以直接分到长度的一半的糖果种类数,如果小于就分到糖果的种类数)

int cmp(const void *a,const void *b){
	return *(int *)a-*(int *)b;
}
int distributeCandies(int *candies,int candiesSize){
	qsort(candies,candiesSize,sizeof(int),cmp);
	int temp = candies[0];
	int count = 1;
	for(int i=1;i<candiesSize;i++){
		if(candies[i]!=temp){
			temp = candies[i];
			count++;
		}
	}
	if(count>=candiesSize){
		return candiesSize/2;
	}
	return count;
}

N叉树的前序遍历

void visit(struct Node*root,int *result,int *returnSize){
	if(root){
		for(int i=0;i<root->numChildren;i++){
			result[(*returnSize)++] = root->children[i]->val;
			visit(root->children[i],result,returnSize);
		}
	}
}
int *preorder(struct Node*root,int *returnSize){
	int *result = (int *)calloc(sizeof(int),10001);
	*returnSize = 0;
	if(root == NULL){
		return result;
	}
	visit(root,result,0);
	return result;
}

最长和谐子序

int maxValue(int a,int b){
	return (a-b)>=0?a:b;
}
int findLHS(int *nums,int numsSize){
	if(nums==NULL||numsSize==0){
		return 0;
	}
	qsort(nums,numsSize,sizeof(nums[0]),cmp);
	int res = 0;
	int begin = 0;
	for(int end=0;end<numsSize;end++){
		while(nums[end]-nums[begin]>1){
			begin++;
		}
		if(nums[end]-nums[begin]==1){
			res = maxValue(res,begin-end+1);
		}
	}
}

范围求和二(在矩阵中初始元素全部为0,大小为m*n的矩阵上更新+1操作,求含有最大整数的元素的个数)

int maxCount(int m,int n,int **ops,int opsSize,int *opsColSize){
	int min_r = m;
	int min_c = n;
	for(int i=0;i<opsSize;i++){
		if(ops[i][0]<min_r){
			min_r = ops[i][0];
		}
		if(ops[i][0]<min_c){
			min_c = ops[i][1];
		}
	}
	return min_r*min_c;
}

两个列表的最小索引总和

char **findRestaurant(char **list1,int list1size,char **list2,int list2size,int *returnSize){
	int minNum = list1Size>list2Size?list2Size:list1Size;
	char **res = (char **)malloc(sizeof(char *)*minNum);
	int index = 2001;
	int num = 0;
	for(int i =0;i<list1Size;i++){
		for(int j=0;j<list2Size;j++){
			if(strcmp(list1[i],list2[j])==0){
				if(i+j<index){
					index = i+j;
					res[0] = list1[i];
					num = 1;
				}
				else if(i+j==1){
					res[num] = list1[i];
					num++;
				}
			}
		}
	}
	*returnSize = num;
	return res;
}

存在重复的元素

bool containsDuplicate(int *nums,int numsSize){
	qsort(nums,numsSize,sizeof(nums[0]),cmp);
	for(int i=0;i<numsSize-1;i++){
		if(nums[i]==nums[i+1]){
			return true;
		}
	}
	return false;
}

malloc不初始分配内存,calloc初始化已分配的内存
malloc返回一个对象,calloc返回一个数组

int *hash = (int *)calloc(sizeof(int),800);

快乐数

int reverse(int n){
	int num = 0;
	while(n){
		num += (n%10)*(n%10);
		n = n/10;
	}
	return num;
}
bool isHappy(int n){
	int num = reverse(n);
	int *a = (int *)calloc(sizeof(int),800);
	while(num!=1){
		if(a[num]==1){
			return false;
		}
		a[num]++;
		num = reverse(num);
	}
	return true;
}

移除链表元素

struct ListNode * removeElements(struct ListNode *head,int val){
	if(head==NULL){
		return NULL;
	}
	struct ListNode *res = removeElements(head->next,val);
	if(head->val==val){
		return res;
	}
	head->next = res;
	return head;
}

0和1不是质数

bool isPrime(int x){
	for(int i=2;i*i<=x;i++){
		if(x%i==0){
			return false;
		}
	}
	return true;
}
int countPrimes(int n){
	int count = 0;
	for(int i=2;i<n;i++){
		count += isPrime(i);
	}
	return count;
}

同构字符串

bool isIsomorphic(char *s,char *t){
	int ss[26] = {0};
	int tt[26] = {0};
	int sc = 0;
	int tc = 0;
	for(int i=0;i<strlen(s);i++){
		if(ss[s[i]-'a']==0){
			ss[s[i]-'a'] = ++sc;
		}
		if(tt[t[i]-'a']==0){
			tt[t[i]-'a'] = ++sc;
		}
		if((ss[s[i]-'a']!=tt[t[i]-'a']){
			return false;
		}
	}
	return true;
}

memset()是C标准库<string.h>
memset(str,c,n)复制字符c到参数str所指向的前n个字符

//将数组内的值全部初始化为-1
int a[k+1];
memset(a,-1,k+1);
//把src所指向的字符串追加到dest所指向的字符串末尾
strcat(dest,src);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

饼干饼干圆又圆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值