从零开始的刷LeetCode生活 第20期 201-210

在这里插入图片描述

class Solution {
public:
    int rangeBitwiseAnd(int m, int n) {
        int i = 0;
        while(m != n)
        {
            m >>= 1;
            n >>= 1;
            i ++;
        }
        return n << i;
    }
};

在这里插入图片描述

class Solution {
public:
    int bitSquareSum(int n)
    {
        int sum = 0;
        while(n > 0)
        {
            int bit = n % 10;
            sum += bit * bit;
            n /= 10;
        }
        return sum;
    }

    bool isHappy(int n) {
        int slow = n, fast = n;
        do{
            slow = bitSquareSum(slow);
            fast = bitSquareSum(fast);
            fast = bitSquareSum(fast);
        }while(slow != fast);

        return slow == 1;
    }
};

在这里插入图片描述

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode *dummy = new ListNode(-1);
        dummy->next = head;
        for(ListNode *p = dummy; p; )
        {
            if(p->next && p->next->val == val)
                p->next = p->next->next;
            else
                p = p->next;
        }
        return dummy->next;
    }
};

在这里插入图片描述

class Solution {
public:
    int countPrimes(int n) {
        vector<int> primes;
        vector<bool> st(n + 1);
        for(int i = 2; i < n; i ++)
        {
            if(!st[i]) primes.push_back(i);
            for(int j = 0; i * primes[j] < n; j ++)
            {
                st[i * primes[j]] = true;
                if(i % primes[j] == 0) break;
            }
        }
        return primes.size();
    }
};

在这里插入图片描述

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        unordered_map<char, char>st, ts;
        for(int i = 0; i < s.size(); i ++)
        {
            if(st.count(s[i]))
            {
                if(st[s[i]] != t[i]) return false;
            }
            else st[s[i]] = t[i];
            if(ts.count(t[i]))
            {
                if(ts[t[i]] != s[i]) return false;
            }
            else ts[t[i]] = s[i];
        }
        return true;
    }
};

在这里插入图片描述

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(!head || !head->next) return head;
        ListNode *p = reverseList(head->next);
        head->next->next = head;
        head->next = NULL;
        return p;
    }
};
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(!head) return head;
        ListNode *p = NULL;
        while(head)
        {
            ListNode *next = head->next;
            head->next = p;
            p = head;
            head = next;
        }
        return p;
    }
};

在这里插入图片描述

class Solution {
public:
    bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
        vector<vector<int>> graph(numCourses);
        vector<int> in_degree(numCourses, 0);
        for(int i = 0; i < prerequisites.size(); i ++)
        {
            in_degree[prerequisites[i][0]] ++;
            graph[prerequisites[i][1]].push_back(prerequisites[i][0]);
        }

        queue<int> q;
        vector<bool> vis(numCourses, false);
        for(int i = 0; i < numCourses; i ++)
            if(in_degree[i] == 0)
                q.push(i);
        while(!q.empty())
        {
            int st = q.front();
            q.pop();
            vis[st] = true;
            for(int i = 0; i < graph[st].size(); i ++)
            {
                in_degree[graph[st][i]] --;
                if(in_degree[graph[st][i]] == 0)
                    q.push(graph[st][i]);
            }
        }
        for(int i = 0; i < numCourses; i ++)
            if(vis[i] == false)
                return false;
        return true;
    }
};

在这里插入图片描述

class Trie {
public:
    /** Initialize your data structure here. */

    struct Node
    {
        bool is_end;
        Node *next[26];
        Node()
        {
            is_end = false;
            for(int i = 0; i < 26; i ++)
                next[i] = 0;
        }
    }*root;

    Trie() {
        root = new Node();
    }
    
    /** Inserts a word into the trie. */
    void insert(string word) {
        Node *p = root;
        for(char c : word)
        {
            int son = c - 'a';
            if(!p->next[son]) p->next[son] = new Node();
            p = p->next[son];
        }
        p->is_end = true;
    }
    
    /** Returns if the word is in the trie. */
    bool search(string word) {
        Node *p = root;
        for(char c : word)
        {
            if(p->next[c - 'a']) p = p->next[c - 'a'];
            else return false;
        }
        return p->is_end;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    bool startsWith(string prefix) {
        Node *p = root;
        for(char c : prefix)
        {
            if(p->next[c - 'a']) p = p->next[c - 'a'];
            else return false;
        }
        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);
 */

在这里插入图片描述

class Solution {
public:
   int minSubArrayLen(int s, vector<int>& nums) {
       int n = nums.size();
       int start = 0, end = 0, tot = 0, res = n + 1;
       while(end < n)
       {
           tot += nums[end];
           while(tot >= s)
           {
               res = min(res, end - start + 1);
               tot -= nums[start];
               start ++;
           }
           end ++;
       }
       if(res == n + 1)
           res = 0;
       return res;
   }
};

在这里插入图片描述
在这里插入图片描述

class Solution {
public:    
    vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
        vector<int>res;
        vector<vector<int>>graph(numCourses);
        vector<int> in_degree(numCourses, 0);
        for(int i = 0; i < prerequisites.size(); i ++)
        {
            in_degree[prerequisites[i][0]] ++;
            graph[prerequisites[i][1]].push_back(prerequisites[i][0]);
        }
        queue<int>q;
        vector<bool>vis(numCourses, false);
        for(int i = 0; i < numCourses; i ++)
            if(in_degree[i] == 0)
                q.push(i);
        while(!q.empty())
        {
            int st = q.front();
            q.pop();
            res.push_back(st);
            vis[st] = true;
            for(int i = 0; i < graph[st].size(); i ++)
            {
                in_degree[graph[st][i]] --;
                if(in_degree[graph[st][i]] == 0)
                    q.push(graph[st][i]);
            }
        }
        for(int i = 0; i < numCourses; i ++)
            if(vis[i] == false)
                return vector<int>();
        return res;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值