[LeetCode 1360~1363][周赛]周赛177题解

1360.日期之间隔几天
题目链接

static const auto io_speed_up = [](){
    ios::sync_with_stdio(false);
    cin.tie(0);
    return 0;
}();
class Solution {
public:
    int nleap[13]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int nsum[13]={0};
    int leap[13]={0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int sum[13]={0};
    inline bool isleap(int p){return (p%4==0&&p%100!=0)||(p%400)==0;}
    inline int lecnt(int p){return p/4-p/100 +p/400+1;}
    int days(int y, int m, int d){
        int dy = y*365 + (y?lecnt(y-1):0);
        int dm = isleap(y)?sum[m-1]:nsum[m-1];
        return dy+dm+d;
    }
    int daysBetweenDates(string date1, string date2) {
        for(int i = 1; i < 13; i++)nsum[i]=nsum[i-1]+nleap[i];
        for(int i = 1; i < 13; i++)sum[i]=sum[i-1]+leap[i];
        int y1 = stoi(string(date1.begin(),date1.begin()+4));
        int y2 = stoi(string(date2.begin(),date2.begin()+4));
        int m1 = stoi(string(date1.begin()+5, date1.begin()+7));
        int m2 = stoi(string(date2.begin()+5, date2.begin()+7));
        int d1 = stoi(string(date1.begin()+8, date1.begin()+10));
        int d2 = stoi(string(date2.begin()+8, date2.begin()+10));
        return abs(days(y1,m1,d1)-days(y2, m2, d2));
    }
};

1361.验证二叉树

题目链接
听说这个题标算错了,直接判入度为零点的个数和入度为1点的个数就能过,可以考虑一个孤立点+若干个环的情况。
正儿八经把根搜出来,然后搜一下那棵树,判定树的同时判定整张图是否连通。

static const auto io_speed_up = [](){
    ios::sync_with_stdio(false);
    cin.tie(0);
    return 0;
}();
class Solution {
public:
    deque<int>mq;
    bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {
        int *vis = new int[n]();
        bool *is = new bool[n]();
        for(int i = 0;i< n;i++){
            if(~leftChild[i])vis[leftChild[i]]++;
            if(~rightChild[i])vis[rightChild[i]]++;
        }
        int root = 0,cnt = 0;
        for(int i = 0; i < n ;i++){if(!vis[i]){root = i;break;}}
        if(root == n)return false;
        mq.emplace_back(root);
        while(!mq.empty()){
            int nw = mq.front();
            mq.pop_front();if(nw==-1)continue;
            if(is[nw])return false;
            is[nw]=1;cnt++;
            mq.emplace_back(leftChild[nw]);
            mq.emplace_back(rightChild[nw]);
        }
        if(cnt == n)return true;
        return false;
    }
};
  1. 最接近的因数

题目链接

static const auto io_speed_up = [](){
    ios::sync_with_stdio(false);
    cin.tie(0);
    return 0;
}();
class Solution {
public:
    vector<int> closestDivisors(int num) {
        int l,r;
        l = r = round(sqrt(num+1));
        for(int i = l;i>0;i--){
            if((num+1)%i==0)return {i,(num+1)/i};
            if((num+2)%i==0)return {i,(num+2)/i};
        }
        return {0};
    }
};
  1. 形成三的最大倍数

题目链接
先填满,再去掉

static const auto io_speed_up = [](){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    return 0;
}();
class Solution {
public:
    string largestMultipleOfThree(vector<int>& digits) {
        int *buc = new int[10]();
        for(int p : digits){
            buc[p]++;
        }
        int sum = 0;
        for(int i = 0;i<10;i++)sum += buc[i]*i;
        if(sum % 3 ==2){
            int p = 1;
            for(int i = 2;i < 10&&p;i+=3){if(buc[i]){buc[i]--;p--;}}
            if(p++)for(int i = 1;i < 10&&p;i+=3){while(buc[i]&&p){buc[i]--;p--;}}
        }
        else if(sum %3 == 1){
            int p = 1;
            for(int i = 1;i < 10&&p;i+=3){if(buc[i]){buc[i]--;p--;}}
            if(p++)for(int i = 2;i < 10&&p;i+=3){while(buc[i]&&p){buc[i]--;p--;}}
        }

        if(buc[0]==digits.size())return "0";
        string ans = "";
        for(int i = 9; i >= 0; i--){
            ans += string(buc[i], i + '0');
        }
        return ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值