LeetCode 力扣 刷题记录(19 - 23)题目+算法分析+Cpp解答

19.Remove Nth Node From End of List

开 vector 保存链表各个 node:

​ 特殊情况:删去链表的第一个和最后一个。

​ 第一个:直接返回链表的第二个 node,其为新的链表头。

​ 最后一个:在 vector 的最后再存入一个 NULL,就可以应用到通用公式。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        vector<ListNode*> node;
        int count = 0;
        node.push_back(head);
        ListNode* index = head;
        while(index->next != NULL){
            index = index->next;
            count++;
            node.push_back(index);
        }
        node.push_back(NULL);
        if(count == n-1)
            return node[1];
        node[count-n]->next = node[count-n+2];
        return node[0];
    }
};

20.Valid Parentheses

未完成队列:

​ 遍历字符串,每次遇到 “ { ” “ [ ” “ ( ” 将其加入未完成队列中。

​ 遇到 “ } ” “ ] ” “ ) ” 时,取出未完成队列的最后一个字符进行判断,正确就从未完成队列中除去,错误就返回 false。

​ 最后如果未完成队列中还有字符,则返回 false,若未完成队列为空,则返回 true。

class Solution {
public:
    bool isValid(string s) {
        int len = s.length();
        int count = 0;
        string front = "";
        for(int i=0;i<len;i++){
            if(s[i] == '{' || s[i] == '[' || s[i] == '('){
                front += s[i];
                count++;
            }
            if(count == 0)
                return false;
            if(s[i] == ')'){
                if(front[count-1] == '('){
                    front = front.substr(0,front.length()-1);
                    count--;
                }
                else
                    return false;
            }
            if(s[i] == ']'){
                if(front[count-1] == '['){
                    front = front.substr(0,front.length()-1);
                    count--;
                }
                else
                    return false;
            }
            if(s[i] == '}'){
                if(front[count-1] == '{'){
                    front = front.substr(0,front.length()-1);
                    count--;
                }
                else
                    return false;
            }
        }
        if(count == 0)
            return true;
        else
            return false;
    }
};

21.Merge Two Sorted Lists

简单归并:

/**
 * 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 ans;
        ListNode* index;
        index = &ans;
        ans.next = NULL;
        while(l1 != NULL && l2 != NULL){
            if(l1->val <= l2->val){
                index->next = l1;
                l1 = l1->next;
            }
            else{
                index->next = l2;
                l2 = l2->next;
            }
            index = index->next;
        }
        if(l1 == NULL && l2 != NULL)
            index->next = l2;
        if(l1 != NULL && l2 == NULL)
            index->next = l1;
        return ans.next;
    }
};

22.Generate Parentheses

回溯法:

class Solution {
public:
    vector<string> generateParenthesis(int n) {
        vector<string> ans;
        Back_Tracking("",n,0,ans);
        return ans;
    }
    void Back_Tracking(string now,int front,int behind,vector<string> &ans){
        if(front == 0 && behind == 0){
            if(find(ans.begin(),ans.end(),now) == ans.end())
                ans.push_back(now);
            return;
        }
        if(front > 0){
            now += '(';
            front--;
            behind++;
            Back_Tracking(now,front,behind,ans);
            behind--;
            front++;
            now = now.substr(0,now.length()-1);
        }
        if(behind > 0){
            now += ')';
            behind--;
            Back_Tracking(now,front,behind,ans);
            behind++;
            now = now.substr(0,now.length()-1);
        }
        return;
    }
};

优化后的回溯法:

class Solution {
public:
    vector<string> generateParenthesis(int n) {
        vector<string> ans;
        Back_Tracking("",n,n,ans);
        return ans;
    }
    void Back_Tracking(string now,int front,int behind,vector<string> &ans){
        if(front == 0){
            now.append(behind,')');
            ans.push_back(now);
            return;
        }
        for(int i=front;i>=0;i--){
            string temp = now;
            if(front-i > behind-1)
                return;
            temp.append(i,'(');
            temp.append(1,')');
            Back_Tracking(temp,front-i,behind - 1,ans);
        }
        return;
    }
};

23.Merge k Sorted Lists

分治法(类似于归并排序):

​ 将等待合并的链表集合先进行划分,把链表集合分为两个部分,对两个部分进行讨论。

​ 如果此时两个部分的链表个数仍然大于 1,则要继续划分,将一个部分继续划分为两个部分,直到每个部分的链表个数为 1 时,返回该链表。

​ 由于每次都进行两两划分,所以最后可以把每个部分都视为是两个链表的合并,把多链表的问题分治为两链表的问题。

​ 每个大块会划分为两个小块,两个小块会返回一个有序链表,大块的事情就是把这两个小块的链表进行合并,得到一条合并的有序链表,大块将返回这条链表,这条链表将给更大的块去进行合并。

​ 将多链表的合并进行逐层划分,变为多个两链表的合并,类似于归并排序的思想。

/**
 * 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) {
        int size = lists.size();
        if(size == 0)
            return NULL;
        ListNode* ans = mergesort(lists,0,size-1);
        return ans;
    }
    ListNode* mergesort(vector<ListNode*>& lists,int left,int right){
        if(left == right)
            return lists[left];
        int mid = (left + right)/2;
        ListNode* l1 = mergesort(lists,left,mid);
        ListNode* l2 = mergesort(lists,mid+1,right);
        ListNode* lm = merge(l1,l2);
        return lm;
    }
    ListNode* merge(ListNode* l1,ListNode* l2){
        ListNode ans;
        ListNode* index;
        index = &ans;
        ans.next = NULL;
        while(l1 != NULL && l2 != NULL){
            if(l1->val <= l2->val){
                index->next = l1;
                l1 = l1->next;
            }
            else{
                index->next = l2;
                l2 = l2->next;
            }
            index = index->next;
        }
        if(l1 == NULL && l2 != NULL)
            index->next = l2;
        if(l1 != NULL && l2 == NULL)
            index->next = l1;
        return ans.next;
    }
};
发布了36 篇原创文章 · 获赞 5 · 访问量 5230

猜你喜欢

转载自blog.csdn.net/qq_43413123/article/details/105050182
今日推荐