【每日刷题】Day65

【每日刷题】Day65

🥕个人主页:开敲🍉

🔥所属专栏:每日刷题🍍

🌼文章目录🌼

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

2. 序列找数_牛客题霸_牛客网 (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;

}

  • 22
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值