从零开始的刷LeetCode生活 第14期 141-150

在这里插入图片描述

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        if(!head || !head->next) return 0;
        ListNode* a = head, *b = head->next;
        while(a && b)
        {
            if(a == b) return true;
            a = a->next;
            b = b->next;
            if(b) b = b->next;
        }
        return false;
    }
};

在这里插入图片描述

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        if(!head) return head;
        ListNode* a = head, *b = head;
        while(a && b)
        {
            a = a->next;
            b = b->next;
            if(b) b = b->next;
            else return 0;
            if(a == b)
            {
                a = head;
                while(a != b)
                {
                    a = a->next;
                    b = b->next;
                }
                return b;
            }
        }
        return 0;
    }
};

在这里插入图片描述

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void reorderList(ListNode* head) {
        if(!head) return;
        int n = 0;
        for(auto p = head; p; p = p->next) n ++;
        if(n <= 2) return;
        ListNode* mid = head;
        for(int i = 0; i < (n + 1) / 2 - 1; i ++) mid = mid->next;
        ListNode* a = mid->next;
        mid->next = NULL;
        ListNode* b = a->next;
        a->next = NULL;
        while(b)
        {
            ListNode* next = b->next;
            b->next = a;
            a = b;
            b = next;
        }
        b = head;
        while(a)
        {
            ListNode* next = a->next;
            a->next = b->next;
            b->next = a;
            a = next;
            b = b->next->next;
        }
    }
};

在这里插入图片描述

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> res;
    vector<int> preorderTraversal(TreeNode* root) {
        if(!root) return res;
        dfs(root);
        return res;
    }

    void dfs(TreeNode* u)
    {
        if(!u) return;
        res.push_back(u->val);
        if(u->left) dfs(u->left);
        if(u->right) dfs(u->right);
    }
};
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        stack<TreeNode*> stk;
        vector<int> res;
        TreeNode* p = root;
        while(p || !stk.empty())
        {
            while(p)
            {
                res.push_back(p->val);
                stk.push(p);
                p = p->left;
            }
            p = stk.top()->right;
            stk.pop();
        }
        return res;
    }
};

在这里插入图片描述

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> res;
    vector<int> postorderTraversal(TreeNode* root) {
        if(!root) return res;
        dfs(root);
        return res;
    }

    void dfs(TreeNode* u)
    {
        if(u->left) dfs(u->left);
        if(u->right) dfs(u->right);
        res.push_back(u->val);
    }
};
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> res;
        stack<TreeNode*> stk;
        TreeNode* p = root, * lastVisited = NULL;
        while(p || !stk.empty())
        {
            while(p)
            {
                stk.push(p);
                p = p->left;                
            }
            p =stk.top();
            if(p->right == NULL || p->right == lastVisited) //右儿子为空或者已经遍历过
            {
                stk.pop();
                res.push_back(p->val);
                lastVisited = p;
                p = NULL;
            }
            else p = p->right;
        }
        return res;
    }    
};

在这里插入图片描述

class LRUCache {    
public:
    struct Node
    {
        int key, value;
        Node(int k, int v) : key(k), value(v){}
    };
    int capacity;
    list<Node> cacheList;
    unordered_map<int, list<Node>::iterator>cacheMap;
    LRUCache(int c) : capacity(c) {        
    }
    
    int get(int key) {
        if(cacheMap.find(key) == cacheMap.end())
            return -1;
        cacheList.splice(cacheList.begin(), cacheList, cacheMap[key]);
        return cacheMap[key]->value;
    }
    
    void put(int key, int value) {
        if(cacheMap.find(key) == cacheMap.end())
        {
            if(cacheList.size() == capacity)//缓存达到上限
            {
                cacheMap.erase(cacheList.back().key);
                cacheList.pop_back();
            }
            cacheList.push_front(Node(key, value));
            cacheMap[key] = cacheList.begin();
        }
        else
        {
            cacheMap[key]->value = value;
            cacheList.splice(cacheList.begin(), cacheList, cacheMap[key]);
        }
    }
};

/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache* obj = new LRUCache(capacity);
 * int param_1 = obj->get(key);
 * obj->put(key,value);
 */

在这里插入图片描述

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

在这里插入图片描述

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* sortList(ListNode* head) {
        return head == NULL ? NULL : mergeSort(head);
    }

    ListNode* mergeSort(ListNode* node)
    {
        if(!node->next) return node;
        ListNode* a = node, *b = node;
        ListNode* breakN = node;
        while(a && a->next)
        {
            a = a->next->next;
            breakN = b;
            b = b->next; //b为中点
        }
        breakN->next = NULL;
        ListNode*l = mergeSort(node);
        ListNode*r = mergeSort(b);
        return mergeTwoList(l, r);
    }

    ListNode* mergeTwoList(ListNode*l1, ListNode*l2)
    {
        ListNode*dummy = new ListNode(-1);
        ListNode*cur = dummy;
        while(l1 && l2)
        {
            if(l1->val <= l2->val)
            {
                cur->next = l1;
                l1 = l1->next;
            }
            else
            {
                cur->next = l2;
                l2 = l2->next;
            }
            cur = cur->next;
        }
        cur->next = (l1 == NULL ? l2 : l1);
        return dummy->next;
    }
};

在这里插入图片描述

class Solution {
public:
    int maxPoints(vector<vector<int>>& points) {
        if(points.empty()) return 0;
        int res = 1;
        for(int i = 0; i < points.size(); i ++)
        {
            int ver = 1, dup = 0; //竖直方向的点 定点重合的点
            unordered_map<long double, int> hash; //记录斜率出现的次数
            for(int j = i + 1; j < points.size(); j ++)            
                if(points[i][0] == points[j][0])
                {
                    ver ++;
                    if(points[i][1] == points[j][1]) dup ++;
                }        
                        
            for(int j = i + 1; j < points.size(); j ++)      
                if(points[i][0] != points[j][0])
                {
                    long double slope = (long double)(points[i][1] - points[j][1]) / (points[i][0] - points[j][0]);
                    if(!hash.count(slope)) hash[slope] = 2;
                    else hash[slope] ++;
                    res = max(res, hash[slope] + dup);
                }          
            res = max(res, ver);
        }
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        stack<int>stk;
        for(auto &t : tokens)
            if(t == "+" || t == "-" || t == "*" || t == "/")
            {
                int a = stk.top();
                stk.pop();
                int b = stk.top();
                stk.pop();
                if(t == "+") stk.push(a + b);
                else if(t == "-") stk.push(b - a);
                else if(t == "*") stk.push(a * b);
                else stk.push(b / a);
            }
            else stk.push(atoi(t.c_str()));
        return stk.top();
    }
};
发布了121 篇原创文章 · 获赞 33 · 访问量 7306

猜你喜欢

转载自blog.csdn.net/qq_42549254/article/details/103967981