【每日刷题】Day70

【每日刷题】Day70

🥕个人主页:开敲🍉

🔥所属专栏:每日刷题🍍

🌼文章目录🌼

[1. 922. 按奇偶排序数组 II - 力扣(LeetCode)](#1. 922. 按奇偶排序数组 II - 力扣(LeetCode))

[2. 905. 按奇偶排序数组 - 力扣(LeetCode)](#2. 905. 按奇偶排序数组 - 力扣(LeetCode))

[3. 515. 在每个树行中找最大值 - 力扣(LeetCode)](#3. 515. 在每个树行中找最大值 - 力扣(LeetCode))

1. 922. 按奇偶排序数组 II - 力扣(LeetCode)

//思路:归并。将数组中奇、偶数分别存储进两个数组,随后根据i的奇偶性从两个数组中拿取数据。

int* sortArrayByParityII(int* nums, int numsSize, int* returnSize)

{

int even10001 = {0};//偶数数组

int count1 = 0;

int odd10001 = {0};//奇数数组

int count2 = 0;

for(int i = 0;i<numsSize;i++)

{

if(numsi%2==0)

evencount1++ = numsi;

else

oddcount2++ = numsi;

}

int flag1 = 0;

int flag2 = 0;

for(int i = 0;i<numsSize;i++)

{

if(i%2==0)

numsi = evenflag1++;

else

numsi = oddflag2++;

}

*returnSize = numsSize;

return nums;

}

2. 905. 按奇偶排序数组 - 力扣(LeetCode)

//思路:归并。与上一题类似。

int* sortArrayByParity(int* nums, int numsSize, int* returnSize)

{

int even5002 = {0};

int count1 = 0;

int odd5002 = {0};

int count2 = 0;

for(int i = 0;i<numsSize;i++)

{

if(numsi%2==0)

evencount1++ = numsi;

else

oddcount2++ = numsi;

}

int flag1 = 0;

int flag2 = 0;

while(flag1<count1)

{

numsflag1 = evenflag1;

flag1++;

}

int begin = flag1;

while(flag2<count2)

{

numsbegin++ = oddflag2;

flag2++;

}

*returnSize = numsSize;

return nums;

}

3. 515. 在每个树行中找最大值 - 力扣(LeetCode)

//思路:深度优先遍历。首先求出二叉树的最大深度,用于判断需要循环多少次。在每次循环中,我们让i的值作为我们所要求的第i层的最大值,采用深度优先遍历遍历到第i层,创建一个max变量,用于记录最大值,随后记录在答案数组中。

typedef struct TreeNode TN;

//判断是否为叶子结点

bool IsLeafNode(TN* root)

{

return !root->left&&!root->right;

}

//求二叉树最大深度

int TreeHigh(TN* root)

{

if(!root)

return 0;

if(IsLeafNode(root))

return 1;

int left = TreeHigh(root->left);

int right = TreeHigh(root->right);

return 1+(left>right?left:right);

}

//求第K层的最大值

void Max(TN* root,int k,int* max)

{

if(!root)

return;

if(k==1)//K==1说明当前层为所求层

{

(*max) = (*max)>(root->val)?(*max):root->val;//记录max,直接返回

return;

}

//深度优先遍历

Max(root->left,k-1,max);

Max(root->right,k-1,max);

}

int* largestValues(struct TreeNode* root, int* returnSize)

{

int* ans = (int*)malloc(sizeof(int)*10001);

int count = 0;

int high = TreeHigh(root);

for(int i = 1;i<=high;i++)//循环次数为要求层数

{

int max = -2147483648;

Max(root,i,&max);

anscount++ = max;

}

*returnSize = count;

return ans;

}

相关推荐
想吃火锅10055 小时前
【leetcode】14.最长公共前缀js
算法·leetcode·职场和发展
云絮.6 小时前
数据库操作
数据库·mysql·算法·oracle
小林ixn7 小时前
LeetCode 206. 反转链表(迭代 + 递归详解)
算法·leetcode·链表
凡人叶枫7 小时前
Effective C++ 条款17:以独立语句将 newed 对象置入智能指针
java·linux·开发语言·c++·算法
菜鸟‍8 小时前
LeetCode 1 27 和 704 || 两数之和 移除元素 二分查找
算法·leetcode·职场和发展
caimouse9 小时前
Reactos 第1章 概述
c语言·开发语言·架构
退休倒计时9 小时前
【每日一题】LeetCode 142. 环形链表 II TypeScript
算法·leetcode·链表·typescript
啊森要自信10 小时前
【GUI自动化测试】控件、鼠标键盘操作与多场景自动化
c语言·开发语言·python·adb·ipython
popcorn_min10 小时前
Digits 手写数字识别:随机森林多分类 + 像素级特征热力图
算法·随机森林·分类
liulilittle10 小时前
拥塞控制:排水终止的两种决策:OR 与 AND
网络·tcp/ip·计算机网络·算法·信息与通信·tcp·通信