[刷力扣] 21-30题

21. 合并两个有序链表

/**
 * 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* mergeTwoLists(ListNode* list1, ListNode* list2) {
        if (!list1 || !list2) {
            if (list1) return list1;
            return list2;
        }
        if (list1->val > list2->val) return mergeTwoLists(list2, list1);
        auto p = list1, a = list1->next, b = list2;
        while (a && b) {
            if (a->val <= b->val)
                p->next = a, a = a->next;
            else
                p->next = b, b = b->next;
            p = p->next;
        }
        while(a) p->next = a, a = a->next, p = p->next;
        while(b) p->next = b, b = b->next, p = p->next;
        return list1;
    }
};

22. 括号生成

class Solution {
public:
    void dfs (int cur, int pre, int a, int b, int end, vector<string>& ans, string& s) {
        if (cur == end) {
            if (pre == 0) ans.push_back(s);
            return;
        }
        if (a) {
            s.push_back('(');
            dfs (cur + 1, pre + 1, a - 1, b, end, ans, s);
            s.pop_back();
        }
        if (b && pre) {
            s.push_back(')');
            dfs (cur + 1, pre - 1, a, b - 1, end, ans, s);
            s.pop_back();
        }
    }
    vector<string> generateParenthesis(int n) {
        vector<string> ans;
        string s;
        s.push_back('(');
        dfs (1, 1, n - 1, n, n * 2, ans, s);
        return ans;
    }
};

23. 合并K个升序链表

/**
 * 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* merge_two(ListNode* list1, ListNode* list2) {
        if (!list1 || !list2) {
            if (list1) return list1;
            return list2;
        }
        if (list1->val > list2->val) return merge_two(list2, list1);
        auto p = list1, a = list1->next, b = list2;
        while (a && b) {
            if (a->val <= b->val)
                p->next = a, a = a->next;
            else
                p->next = b, b = b->next;
            p = p->next;
        }
        while(a) p->next = a, a = a->next, p = p->next;
        while(b) p->next = b, b = b->next, p = p->next;
        return list1;
    }
    ListNode* merge(int l, int r, vector<ListNode*>& lists) {
        if (l == r) return lists[l];
        int mid = l + r >> 1;
        ListNode* a = merge(l, mid, lists);
        ListNode* b = merge(mid + 1, r, lists);
        return merge_two(a, b);
    }
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        if (lists.size())
            return merge(0, lists.size() - 1, lists);
        else return nullptr;
    }
};

24. 两两交换链表中的节点

/**
 * 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* swapPairs(ListNode* head) {
        if (!(head && head->next)) return head;
        ListNode now(0, head);
        ListNode *pre = &now, *cur = head, *nex = head->next;
        while (nex) {
            pre->next = nex;
            cur->next = nex->next;
            nex->next = cur;
            ListNode* tmp = cur;
            cur = nex;
            nex = tmp;
            if (nex->next && nex->next->next) {
                pre = nex, cur = pre->next, nex = pre->next->next;
            } else break;
        }
        return now.next;
    }
};

25. K 个一组翻转链表

/**
 * 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* rever(ListNode* head) {
        if (head == nullptr) return nullptr;
        ListNode *cur = head, *next = head->next;
        head->next = nullptr;
        while (next) {
            ListNode* temp = next->next;
            next->next = cur;
            cur = next;
            next = temp;
        }
        return cur;
    }
    ListNode* reverseKGroup(ListNode* head, int k) {
        ListNode node(0, head);
        ListNode* list = head;
        ListNode* pre = &node;
        while (list) {
            int f = 1;
            ListNode* cur = list, *clist = list;
            for (int i = 0; i < k && f; ++ i) {
                if (list) clist = list, list = list->next;
                else f = 0;
            }
            if (!f) break;
            ListNode* next = list;
            clist->next = nullptr;
            ListNode* res = rever(cur);
            pre->next = res;
            cur->next = next;
            pre = cur;
        }
        return node.next;
    }
};

26. 删除有序数组中的重复项

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int len = 0;
        for (int i = 1; i < nums.size(); ++ i) 
            if (nums[i] != nums[i - 1])
                nums[++ len] = nums[i];
        return len + 1;
    }
};

27. 移除元素

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int len = -1;
        for (auto &i : nums)
            if (i != val) nums[++ len] = i;
        return len + 1;
    }
};

28. 实现 strStr()

class Solution {
public:
    int strStr(string haystack, string needle) {
        if (needle.size() == 0) return 0;
        for (int i = 0; i + needle.size() <= haystack.size(); ++ i)
            if (haystack.substr(i, needle.size()) == needle) return i;
        return -1;
    }
};

29. 两数相除

class Solution {
public:
    int pow (int x, int c, int y) {
        int res = 0, t = x;
        while (c) {
            if (c & 1) {
                if (res < y - t) return false;
                res += t;
            }
            c >>= 1;
            if (c) {
                if (t < y - t) return false;
                t += t;
            }
        }
        return true;
    }
    int divide(int dividend, int divisor) {
        if (dividend == INT_MIN) {
            if (divisor == 1) 
                return INT_MIN;
            if (divisor == -1) 
                return INT_MAX;
        }
        if (divisor == INT_MIN) return dividend == INT_MIN ? 1 : 0;
        if (dividend == 0) return 0;
        int falg = 0;
        if (dividend > 0) {
            dividend = -dividend;
            falg = !falg;
        }
        if (divisor > 0) {
            divisor = -divisor;
            falg = !falg;
        }
        int l = 1, r = INT_MAX, ans = 0;
        while (l <= r) {
            int mid = l + ((r - l) >> 1);
            if (pow(divisor, mid, dividend)) {
                ans = mid;
                if (mid == INT_MAX) break;
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        return falg ? -ans: ans;
    }
};

30. 串联所有单词的子串

class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words) {
        unordered_map<string, int> mp, cur;
        vector<int> ans;
        for (auto& i : words) mp[i] ++;
        int len = words[0].size();
        for (int i = 0; i < len; ++ i) {
            queue<int> que;
            for (int j = i; j + len <= s.size(); j += len) {
                string st = s.substr(j, len);
                while (que.size() && cur[st] == mp[st]) {
                    int t = que.front(); que.pop();
                    cur[s.substr(t, len)] --;
                }
                if (mp[st]) {
                    que.push(j);
                    cur[st] ++;
                }
                if (que.size() == words.size()) ans.push_back(que.front());
            }
            cur.clear();
        }
        return ans;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值