【每日刷题】Day65

【每日刷题】Day65

🥕个人主页:开敲🍉

🔥所属专栏:每日刷题🍍

🌼文章目录🌼

[1. LCR 175. 计算二叉树的深度 - 力扣(LeetCode)](#1. LCR 175. 计算二叉树的深度 - 力扣(LeetCode))

[2. 序列找数_牛客题霸_牛客网 (nowcoder.com)](#2. 序列找数_牛客题霸_牛客网 (nowcoder.com))

[3. 删除重复字符_牛客题霸_牛客网 (nowcoder.com)](#3. 删除重复字符_牛客题霸_牛客网 (nowcoder.com))

1. LCR 175. 计算二叉树的深度 - 力扣(LeetCode)

//思路:分治思想+深度优先遍历。将每一个结点视为根节点,返回其左右子树较深的深度。

int _calculateDepth(struct TreeNode* root)

{

if(!root)

return 0;

int left = _calculateDepth(root->left);//计算左子树深度

int right = _calculateDepth(root->right);//计算右子树深度

return 1+(left>right?left:right);//返回更大的,同时加上自身

}

int calculateDepth(struct TreeNode* root)

{

return _calculateDepth(root);

}

2. 序列找数_牛客题霸_牛客网 (nowcoder.com)

//思路:哈希表。

int main()

{

int ans = 0;

int n = 0;

scanf("%d",&n);

int x = 0;

int hash[20] = {0};

while (scanf("%d", &x) != EOF)

{

hash[x] = 1;

}

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

{

if(hash[i]==0)

ans = i;

}

printf("%d",ans);

return 0;

}

3. 删除重复字符_牛客题霸_牛客网 (nowcoder.com)

//思路:哈希表。

int main()

{

char s[1001] = {0};

int count = 0;

char c = 0;

int hash[1001] = {0};

while(scanf("%c",&c)!=EOF)

{

s[count++] = c;//获取字符串

}

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

{

hash[s[i]-'a'] = 1;//字符-'a'的值作为key,val为1,确保之后重复出现的字符只出现一次

}

char ans[1001] = {0};

int n = 0;

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

{

if(hash[s[i]-'a'])//遍历字符串,如果其key的val为1,存入答案字符串中

{

ans[n++] = s[i];

hash[s[i]-'a'] = 0;//再将val置为0,确保重复字符只出现一次

}

}

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

{

printf("%c",ans[i]);

}

return 0;

}

相关推荐
张李浩5 小时前
Leetcode 054螺旋矩阵 采用方向数组解决
算法·leetcode·矩阵
big_rabbit05026 小时前
[算法][力扣101]对称二叉树
数据结构·算法·leetcode
WolfGang0073216 小时前
代码随想录算法训练营 Day11 | 二叉树 part01
数据结构
美好的事情能不能发生在我身上6 小时前
Hot100中的:贪心专题
java·数据结构·算法
2301_821700536 小时前
C++编译期多态实现
开发语言·c++·算法
xixihaha13246 小时前
C++与FPGA协同设计
开发语言·c++·算法
小小怪7507 小时前
C++中的函数式编程
开发语言·c++·算法
xixixiLucky7 小时前
编程入门算法题---小明爬楼梯求爬n层台阶一共多少种方法
算法
剑锋所指,所向披靡!7 小时前
数据结构之线性表
数据结构·算法
Yupureki7 小时前
《MySQL数据库基础》1. 数据库基础
c语言·开发语言·数据库·c++·mysql·oracle·github