头像

kidforever911




离线:3小时前


最近来访(296)
用户头像
summerrr
用户头像
快乐小子
用户头像
事在人为_4
用户头像
yxc的小迷妹
用户头像
Suzuka_
用户头像
tobylhy
用户头像
Y._75
用户头像
冷静对待一切
用户头像
PseudorandomGenerators
用户头像
20090514
用户头像
macheny
用户头像
茶茶_4
用户头像
深色的云
用户头像
不吃萝卜的Henry
用户头像
GonT
用户头像
KIND.
用户头像
𝓽𝓻𝓲𝓬𝓴._9
用户头像
xujc
用户头像
acwing_7404
用户头像
WA_1


class Solution {
public:
    bool isAlienSorted(vector<string>& words, string order) {
        unordered_map<char, int> record;
        for(int i = 0; i < order.size(); i ++) record[order[i]] = i;
        for(int i = 0; i < words.size() - 1; i ++) {
            string a = words[i], b = words[i + 1];
            int x = 0, y = 0;
            while(x < a.size() && y < b.size()) {
                if(record[a[x]] > record[b[y]]) return false;
                if(record[a[x]] < record[b[y]]) break;
                x ++, y ++;
            }
            if(x < a.size() && y == b.size()) return false;
        }
        return true;
    }
};



class Solution {
public:
    bool isAlienSorted(vector<string>& words, string order) {
        unordered_map<char, int> record;
        for(int i = 0; i < order.size(); i ++) record[order[i]] = i;
        for(int i = 0; i < words.size() - 1; i ++) {
            string a = words[i], b = words[i + 1];
            int x = 0, y = 0;
            while(x < a.size() && y < b.size()) {
                if(record[a[x]] > record[b[y]]) return false;
                if(record[a[x]] < record[b[y]]) break;
                x ++, y ++;
            }
            if(x < a.size() && y == b.size()) return false;
        }
        return true;
    }
};



class Solution {
public:
    string gcdOfStrings(string str1, string str2) {
        if(str1 + str2 != str2 + str1) return "";
        return str1.substr(0, gcd(str1.size(), str2.size()));
    }

    int gcd(int a, int b) {
        return b ? gcd(b, a % b) : a;
    }
};



class Solution {
public:
    string gcdOfStrings(string str1, string str2) {
        if(str1 + str2 != str2 + str1) return "";
        return str1.substr(0, gcd(str1.size(), str2.size()));
    }

    int gcd(int a, int b) {
        return b ? gcd(b, a % b) : a;
    }
};



class Solution {
public:
    string predictPartyVictory(string senate) {
        queue<int> r, d;
        for(int i = 0; i < senate.size(); i ++) {
            if(senate[i] == 'R') r.push(i);
            else d.push(i);
        }
        int n = senate.size();
        while(r.size() && d.size()) {
            if(r.front() < d.front()) r.push(r.front() + n);
            else d.push(d.front() + n);
            r.pop(), d.pop();
        }

        if(r.size()) return "Radiant";
        return "Dire";
    }
};


活动打卡代码 LeetCode 649. Dota2 参议院

class Solution {
public:
    string predictPartyVictory(string senate) {
        queue<int> r, d;
        for(int i = 0; i < senate.size(); i ++) {
            if(senate[i] == 'R') r.push(i);
            else d.push(i);
        }
        int n = senate.size();
        while(r.size() && d.size()) {
            if(r.front() < d.front()) r.push(r.front() + n);
            else d.push(d.front() + n);
            r.pop(), d.pop();
        }

        if(r.size()) return "Radiant";
        return "Dire";
    }
};



class Solution {
public:
    string replaceWords(vector<string>& dictionary, string sentence) {
        typedef unsigned long long ULL;
        const int P = 131;
        unordered_set<ULL> record;

        for(auto& s : dictionary) {
            ULL h = 0;
            for(auto& c : s) h = h * P + c;
            record.insert(h);
        }

        stringstream ssin(sentence);
        string result, word;
        while(ssin >> word) {
            string s;
            ULL h = 0;
            for(auto& c : word) {
                s += c;
                h = h * P + c;
                if(record.count(h)) break;
            }
            result += s + ' ';
        }
        result.pop_back();
        return result;
    }
};


活动打卡代码 LeetCode 648. 单词替换

class Solution {
public:
    string replaceWords(vector<string>& dictionary, string sentence) {
        typedef unsigned long long ULL;
        const int P = 131;
        unordered_set<ULL> record;

        for(auto& s : dictionary) {
            ULL h = 0;
            for(auto& c : s) h = h * P + c;
            record.insert(h);
        }

        stringstream ssin(sentence);
        string result, word;
        while(ssin >> word) {
            string s;
            ULL h = 0;
            for(auto& c : word) {
                s += c;
                h = h * P + c;
                if(record.count(h)) break;
            }
            result += s + ' ';
        }
        result.pop_back();
        return result;
    }
};


活动打卡代码 LeetCode 647. 回文子串

class Solution {
public:
    int countSubstrings(string s) {
        int result = 0;
        for(int i = 0; i < s.size(); i ++) {
            //枚举长度为奇数的情况
            for(int j = i, k = i; j >= 0 && k < s.size(); j --, k ++) {
                if(s[j] != s[k]) break;
                result ++;
            }

            //枚举长度为偶数的情况
            for(int j = i, k = i + 1; j >= 0 && k < s.size(); j --, k ++) {
                if(s[j] != s[k]) break;
                result ++;
            }
        }
        return result;
    }
};



class Solution {
public:
    int countSubstrings(string s) {
        int result = 0;
        for(int i = 0; i < s.size(); i ++) {
            //枚举长度为奇数的情况
            for(int j = i, k = i; j >= 0 && k < s.size(); j --, k ++) {
                if(s[j] != s[k]) break;
                result ++;
            }

            //枚举长度为偶数的情况
            for(int j = i, k = i + 1; j >= 0 && k < s.size(); j --, k ++) {
                if(s[j] != s[k]) break;
                result ++;
            }
        }
        return result;
    }
};