从零开始的刷LeetCode生活 第2期 21-30

在这里插入图片描述

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

在这里插入图片描述

class Solution {
public:
    vector<string>res;
    int m;
    vector<string> generateParenthesis(int n) {
        m = n;
        dfs("", 0, 0);
        return res;
    }

    void dfs(string path, int l, int r)
    {
        if(l == m && r == m)
        {
            res.push_back(path);
            return;
        }
        if(l < m) dfs(path + "(", l + 1, r);
        if(r < l) dfs(path + ")", l, r + 1);
    }
};

在这里插入图片描述

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
bool cmp(ListNode* a, ListNode* b)
{
    return a->val < b->val;
}

class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        vector<ListNode*> node_vec;
        for(int i = 0; i < lists.size(); i ++)
        {
            ListNode* head = lists[i];
            while(head)
            {
                node_vec.push_back(head);
                head = head->next;
            }
        }
        if(node_vec.size() == 0) return NULL;
        sort(node_vec.begin(), node_vec.end(), cmp);
        for(int i = 1; i < node_vec.size(); i ++)
            node_vec[i - 1]->next = node_vec[i];
        node_vec[node_vec.size() - 1]->next = NULL;
        return node_vec[0];    
    }
};
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        if(lists.size() == 0) return NULL;
        if(lists.size() == 1) return lists[0];
        if(lists.size() == 2) return mergeTwoLists(lists[0], lists[1]);
        int mid = lists.size() / 2;
        vector<ListNode*> sub1_lists;
        vector<ListNode*> sub2_lists;
        for(int i = 0; i < mid; i ++)        
            sub1_lists.push_back(lists[i]);        
        for(int i = mid; i < lists.size(); i ++)
            sub2_lists.push_back(lists[i]);
        ListNode* l1 = mergeKLists(sub1_lists);
        ListNode* l2 = mergeKLists(sub2_lists);
        return mergeTwoLists(l1, l2);
    }

    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2)
    {
        ListNode* dummy = new ListNode(-1);
        ListNode* head = dummy;
        while(l1 && l2)
        {
            if(l1->val <= l2->val)
            {
                head->next = l1;
                l1 = l1->next;
            }
            else
            {
                head->next = l2;
                l2 = l2->next;
            }
            head = head->next;
        }
        head->next = (l1 == NULL) ? l2 : l1;
        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* swapPairs(ListNode* head) {
        if(head == NULL || head->next == NULL) return head;
        ListNode* headNext = head->next;
        head->next = swapPairs(headNext->next);
        headNext->next = head;
        return headNext;
    }
};

在这里插入图片描述

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        ListNode* pre = NULL; //翻转的头
        ListNode* next = NULL; //翻转的尾
        ListNode* cur = head;
        ListNode* check = head; //计算链表长度
        int len = 0, count = 0;
        while(len < k && check)
        {
            check = check->next;
            len ++;
        }
        if(len == k)
        {
            while(count < k && cur) //找到翻转的头尾
            {
                next = cur->next;
                cur->next = pre;
                pre = cur;
                cur = next;
                count ++;
            }
            if(next) //下一个翻转链表
                head->next = reverseKGroup(next, k);
            return pre;

        }
        else
            return head;
    }
};

在这里插入图片描述

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        if(nums.empty()) return NULL;
        int i = 0, j;
        for(j = 1; j < nums.size(); )
            if(nums[j] == nums[i])
                j ++;
            else{
                i ++;
                nums[i] = nums[j];
            }
        return i + 1;
    }
};

在这里插入图片描述

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        if(nums.empty()) return 0;
        int i = 0;
        for(int j = 0; j < nums.size(); j ++)
        {
            if(nums[j] != val)
            {
                nums[i ++] = nums[j];                
            }
        }
        return i;
    }
};

在这里插入图片描述

class Solution {
public:
    int strStr(string haystack, string needle) {
        if(needle.empty()) return 0;
        if(needle.size() > haystack.size()) return -1;
        for(int i = 0; i < haystack.size() - needle.size() + 1; i ++)
        {
            int j;
            for(j = 0; j < needle.size(); j ++)
            {
                if(haystack[i + j] != needle[j])
                    break;
            }
            if(j == needle.size())
                return i;
        }
        return -1;
    }
};

在这里插入图片描述

#define INT_MIN_ 0x80000000
class Solution {
public:
    int divide(int dividend, int divisor) {
        if(dividend == 0) return 0;
        if(dividend == INT_MIN && divisor == -1)
            return INT_MAX;
        bool neg;
        neg = (dividend ^ divisor) < 0;
        unsigned int t = dividend == INT_MIN ? INT_MIN_ : abs(dividend);
        unsigned int d = divisor == INT_MIN ? INT_MIN_ : abs(divisor);
        int res = 0;
        for(int i = 31; i >= 0; i --)
        {
            if((t >> i) >= d){
                res += 1 << i;
                t -= d << i;
            }
        }
        if(res == INT_MIN_) return INT_MIN;
        return neg ? -res : res;
    }
};

在这里插入图片描述

class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words) {
        unordered_map<string, int> hash;
        vector<int> res;
        int n = s.size(), m = words.size();
        if(!n || !m) return res;
        int len = words[0].size(), end = n - m * len;
        if(end < 0) return res;
        for(auto word : words) hash[word] ++;//统计每个单词数
        int size = hash.size();
        for(int k = 0; k < len; k ++)
        {
            unordered_map<string, int> cur_hash;
            int satisfy = 0;
            for(int i = k, j = k; j <= n - len; )
            {
                string cur = s.substr(j, len);
                if(hash.find(cur) == hash.end()) //不匹配
                {
                    j += len;
                    i = j;
                    cur_hash.clear();
                    satisfy = 0;
                }else{
                    cur_hash[cur] ++;
                    if(cur_hash[cur] == hash[cur])
                        satisfy ++;
                    else if(cur_hash[cur] > hash[cur])
                    {
                        while(i < j && cur_hash[cur] > hash[cur]) //匹配多,i右移看能不能满足
                        {
                            string temp = s.substr(i, len);
                            i += len;
                            cur_hash[temp] --;
                            if(cur_hash[temp] == hash[temp] - 1)
                                satisfy --;
                        }
                    }
                    if(satisfy == size)
                    {
                        string temp = s.substr(i, len);
                        cur_hash[temp] --;
                        satisfy --;
                        res.push_back(i);
                        i += len;
                    }
                    j += len;
                }
            }
        }
        return res;
    }
};
发布了121 篇原创文章 · 获赞 33 · 访问量 7318

猜你喜欢

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