[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;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章