头像

呀哈喽

山东大学




离线:21小时前


最近来访(32)
用户头像
Haiinf
用户头像
zhangxc
用户头像
理想不大
用户头像
糖豆
用户头像
V1
用户头像
一缕平凡
用户头像
QiaoQi
用户头像
puyagang
用户头像
季之秋
用户头像
冰之韵
用户头像
好孩子都会写代码
用户头像
Seveness
用户头像
tan_90
用户头像
2010
用户头像
易思人
用户头像
Kat_
用户头像
冰凝
用户头像
yxc
用户头像
AcWing2AK
用户头像
荒途孤影

活动打卡代码 LeetCode 1282. 用户分组

/*
 * @lc app=leetcode.cn id=1282 lang=cpp
 *
 * [1282] 用户分组
 */

// @lc code=start
class Solution {
public:
    vector<vector<int>> groupThePeople(vector<int>& gp) {
        vector<vector<int>> res;
        unordered_map<int, vector<int>> ivm;
        for(int i = 0; i < gp.size(); i ++) {
            int t = gp[i];
            if(!ivm.count(t)) ivm[t] = vector<int>();
            auto &v = ivm[t];
            v.push_back(i);
            if(v.size() == t) res.push_back(v), ivm.erase(t);
        }
        return res;
    }
};
// @lc code=end





class WordDictionary {
public:
    struct Node {
        bool fg = false;
        int son[26] = {0};
    };
    vector<Node*> tr;
    WordDictionary() {
        tr.push_back(new Node());
    }

    void addWord(string word) {
        int p = 0;
        for(int i = 0; i < word.size(); i ++)
        {
            int a = word[i] - 'a';
            if(!tr[p]->son[a]) tr.push_back(new Node), tr[p]->son[a] = tr.size() - 1;
            p = tr[p]->son[a];
        }
        tr[p]->fg = true;
    }

    bool search(string word) {  
       return _search(word, 0);
    }
    bool _search(string word, int p) {
        for(int i = 0; i < word.size(); i ++)
        {
            if(word[i] == '.') {
                for(int j = 0; j < 26; j ++) {
                    if(tr[p]->son[j] && _search(word.substr(i + 1), tr[p]->son[j]))
                        return true; 
                }
                return false;
            }
            else {
                int a = word[i] - 'a';
                if(!tr[p]->son[a]) return false;
                p = tr[p]->son[a];
            }
        }  
        return tr[p]->fg;
    }
};


第二次

class WordDictionary {
public:
    struct Node {
        bool fg = false;
        int son[26] = {0};
    };
    vector<Node*> tr;
    WordDictionary() {
        tr.push_back(new Node());
    }

    void addWord(string word) {
        int p = 0;
        for(int i = 0; i < word.size(); i ++)
        {
            int a = word[i] - 'a';
            if(!tr[p]->son[a]) tr.push_back(new Node), tr[p]->son[a] = tr.size() - 1;
            p = tr[p]->son[a];
        }
        tr[p]->fg = true;
    }

    bool search(string word) {  
       return _search(word, 0, 0);
    }
    bool _search(string &word, int p, int st) {
        for(int i = st; i < word.size(); i ++)
        {
            if(word[i] == '.') {
                for(int j = 0; j < 26; j ++) {
                    if(tr[p]->son[j] && _search(word, tr[p]->son[j], i + 1)) 
                            return true;
                }
                return false;
            }
            else {
                int a = word[i] - 'a';
                if(!tr[p]->son[a]) return false;
                p = tr[p]->son[a];
            }
        }  
        return tr[p]->fg;
    }
};



/*
 * @lc app=leetcode.cn id=208 lang=cpp
 *
 * [208] 实现 Trie (前缀树)
 */

// @lc code=start
class Trie {
public:
    struct Node {
        char c;
        int son[26];
        bool fg;
        Node(char _c) : c(_c) {
            memset(son, 0, sizeof son);
            fg = false;
        } 
    };
    vector<Node*> tr;
    Trie() {
        tr.push_back(new Node(0));
    }

    void insert(string word) {
        int p = 0;
        for(int i = 0; i < word.size(); i ++) {
            int a = word[i] - 'a';
            if(!tr[p]->son[a]) tr.push_back(new Node(word[i])), tr[p]->son[a] = (int)tr.size() - 1;
            p = tr[p]->son[a];
        }
        tr[p]->fg = true;
    }

    bool search(string word) {
        int p = 0;
        for(int i = 0; i < word.size(); i ++)
        {
            int a = word[i] - 'a';
            if(!tr[p]->son[a]) return false;
            p = tr[p]->son[a];
        }
        return tr[p]->fg;
    }

    bool startsWith(string prefix) {
        int p = 0;
        for(int i = 0; i < prefix.size(); i ++)
        {
            int a = prefix[i] - 'a';
            if(!tr[p]->son[a]) return false;
            p = tr[p]->son[a];
        }
        return true;
    }
};
/**
 * Your Trie object will be instantiated and called as such:
 * Trie* obj = new Trie();
 * obj->insert(word);
 * bool param_2 = obj->search(word);
 * bool param_3 = obj->startsWith(prefix);
 */
// @lc code=end




活动打卡代码 LeetCode 210. 课程表 II

/*
 * @lc app=leetcode.cn id=210 lang=cpp
 *
 * [210] 课程表 II
 */

// @lc code=start
class Solution {
public:
    vector<int> findOrder(int numCourses, vector<vector<int>>& pre) {
        vector<int> res;
        queue<int> q;
        unordered_map<int, vector<int>> g;
        unordered_map<int, int> d;
        for(auto i : pre) g[i[1]].push_back(i[0]), d[i[0]] ++;
        for(int i = 0; i < numCourses; i ++) {
            if(d.count(i) == 0) 
                q.push(i);
        }
        while(q.size()) {
            int t = q.front(); q.pop();
            res.push_back(t);
            for(auto i : g[t]) {
                d[i] --;
                if(d[i] == 0) q.push(i);
            }
        }
        if(res.size() == numCourses) return res;
        return {};
    }
};
// @lc code=end





/*
 * @lc app=leetcode.cn id=209 lang=cpp
 *
 * [209] 长度最小的子数组
 */

// @lc code=start
class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int res = nums.size() + 1;
        int sum = 0;
        //答案一定是以数组中某个数位结尾的长度最小的子数组
        //枚举j,使得[i,j]是以i结尾的长度最小的子数组
        for(int i = 0, j = 0; j < nums.size(); j ++) {
            sum += nums[j];
            while(i <= j && sum - nums[i] >= target) {
                sum -= nums[i ++];
            }
            if(sum >= target) res = min(res, j - i + 1);
        }
        return res == nums.size() + 1 ? 0 : res;
    }
};
// @lc code=end




活动打卡代码 LeetCode 207. 课程表

/*
 * @lc app=leetcode.cn id=207 lang=cpp
 *
 * [207] 课程表
 */

// @lc code=start
class Solution {
public:
    bool canFinish(int numCourses, vector<vector<int>>& pre) {
        unordered_map<int, int> d;
        for(int i = 0; i < numCourses; i ++) d[i] = 0;
        unordered_map<int, vector<int>> g;
        for(int i = 0; i < pre.size(); i ++) {
            g[pre[i][1]].push_back(pre[i][0]);
            d[pre[i][0]] ++;
        }
        queue<int> q;
        for(auto i : d) {
            if(i.second == 0)
                q.push(i.first);
        }
        while(q.size()) {
            int t = q.front(); q.pop();
            numCourses --;
            if(numCourses == 0) return true;
            for(auto i : g[t]) {
                d[i] --;
                if(d[i] == 0) q.push(i);
            }
        }
        return false;
    }
};
// @lc code=end




活动打卡代码 LeetCode 206. 反转链表

/*
 * @lc app=leetcode.cn id=206 lang=cpp
 *
 * [206] 反转链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(!head) return head;
        ListNode *cur = head->next;
        ListNode *pre = head;
        while(cur) {
            ListNode *t = cur->next;
            cur->next = pre;
            pre = cur;
            cur = t;
        }
        head->next = NULL;
        return pre;
    }
};
// @lc code=end





/*
 * @lc app=leetcode.cn id=203 lang=cpp
 *
 * [203] 移除链表元素
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode dummy = ListNode();
        dummy.next = head;
        ListNode *p = &dummy;
        while(p->next) {
            if(p->next->val == val) p->next = p->next->next;
            else p = p->next;
        }
        return dummy.next;
    }
};
// @lc code=end





/*
 * @lc app=leetcode.cn id=201 lang=cpp
 *
 * [201] 数字范围按位与
 */

// @lc code=start
class Solution {
public:
    int rangeBitwiseAnd(int m, int n) {
        int res = 0;
        for(int i = 30; i >= 0; i --) {
            if((m >> i & 1) != (n >> i & 1)) break;
            if(m >> i & 1) res |= (1 << i);
        }
        return res;
    }
};
// @lc code=end




活动打卡代码 AcWing 3526. 素数

#include <iostream>

using namespace std;

int n;

int main() {
    while(cin >> n) {
        bool has = false;
        for(int i = 2; i < n; i ++)
        {
            bool fg = true;
            for(int j = 2; j <= i / j; j ++) {
                if(i % j == 0) {
                    fg = false;
                    break;
                }
            }
            if(fg && i % 10 == 1) {
                has = true;
                cout << i << " ";
            }
        }
        if(!has) cout << "-1";
        cout << endl;
    }
    return 0;
}