您当前的位置: 首页 >  leetcode

不脱发的程序猿

暂无认证

  • 0浏览

    0关注

    492博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

力扣(LeetCode)刷题,简单题(第21期)

不脱发的程序猿 发布时间:2020-08-14 10:05:45 ,浏览量:0

目录

第1题:最大连续1的个数

第2题:相同的树

第3题:检查平衡性

第4题:仅仅反转字母

第5题:检测大写字母

第6题:在区间范围内统计奇数数目

第7题:二分查找

第8题:字符串轮转

第9题:公交车站间的距离

第10题:有效的括号(2020 哔哩哔哩校招笔试题)

力扣(LeetCode)定期刷题,每期10道题,业务繁重的同志可以看看我分享的思路,不是最高效解决方案,只求互相提升。

第1题:最大连续1的个数

试题要求如下:

回答(C语言):

int findMaxConsecutiveOnes(int* nums, int numsSize){
    int temp = 0;

    for(int i = 0,cou = 0; i < numsSize; i++){
        if(nums[i] == 1){
            cou++;
        }
        else{
            cou = 0;
        }

        if(cou > temp){
            temp = cou;
        }
    }

    return temp;
}

运行效率如下所示:

第2题:相同的树

试题要求如下:

回答(C语言):

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
    if (p == NULL && q == NULL) {
        return true;
    } else if (p == NULL || q == NULL) {
        return false;
    } else if (p->val != q->val) {
        return false;
    } else {
        return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
    }
}

运行效率如下所示:

第3题:检查平衡性

试题要求如下:

回答(C语言):

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

#define MAX(a, b)  ((a) < (b) ? (b) : (a))

bool CoreFunc(struct TreeNode* root, int* depth)
{
    if (NULL == root) {
        *depth = 0;
        return true;
    }
    if (NULL == root->left && NULL == root->right) {
        *depth = 1;
        return true;
    }
    int left = 0;
    int right = 0;
    bool ret = CoreFunc(root->left, &left) && CoreFunc(root->right, &right);
    int res = left > right ? left - right : right - left;
    *depth = MAX(left, right) + 1;
    if (ret && res             
关注
打赏
1664101891
查看更多评论
0.0378s