Leetcode——刪除數組、鏈表的重複元素總結

1 remove-duplicates-from-sorted-array

題目鏈接:https://www.nowcoder.com/practice/a519784e89fb40199fbe7993786715b1?tpId=46&tqId=29153&tPage=7&rp=7&ru=/ta/leetcode&qru=/ta/leetcode/question-ranking

class Solution {
public:
    int removeDuplicates(int A[], int n)
    {
        if(n==0)
            return 0;
        int index = 1;
        for(int i=1; i<n;i++)
        {
            if(A[index-1]!=A[i])
                A[index++] = A[i];
        }
        return index;
    }
};

2 remove-duplicates-from-sorted-array-ii

題目鏈接:https://www.nowcoder.com/practice/567f420f12ed4069b7e1d1520719d409?tpId=46&tqId=29098&tPage=4&rp=4&ru=/ta/leetcode&qru=/ta/leetcode/question-ranking

class Solution {
public:
    int removeDuplicates(int A[], int n) {
         if(n<=2) return n;
        int index=2;//允許重複兩次,可以修改爲三次
        for(int i=2;i<n;i++)
        {
            if(A[i]!=A[index-2])//允許重複兩次,可以修改爲三次
            {A[index]=A[i];
            index++;
            }
        
        }
        return index;
    }
};

3 remove-duplicates-from-sorted-list

題目鏈接:https://www.nowcoder.com/practice/c087914fae584da886a0091e877f2c79?tpId=46&tqId=29096&tPage=4&rp=4&ru=/ta/leetcode&qru=/ta/leetcode/question-ranking

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) 
    {
        if(head == nullptr)
            return nullptr;
        ListNode* node = head;
        ListNode* last = head;

        while(last)
        {
            while(last->next != nullptr && last->val == last->next->val)
            {
                last = last->next;
            }
            node->next = last->next;
            last = last->next;
            node = last;
            
            

        }
        return head;
    }
};

4 remove-duplicates-from-sorted-list-ii

題目鏈接:https://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024?tpId=46&tqId=29095&tPage=4&rp=4&ru=%2Fta%2Fleetcode&qru=%2Fta%2Fleetcode%2Fquestion-ranking

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

if (head == NULL || head->next == NULL) return head;
        ListNode* head1 = new ListNode(INT_MIN);
        head1->next = head;
        ListNode* p = head1;
        ListNode* q = head1->next;
        while(q) {
            while(q->next && q->next->val == q->val) q = q->next;
            if(p->next != q) {
                //ListNode* tmp = q->next;
               // q->next = NULL; //將重複結點斷開
                p->next = q->next;
                q = q->next;
            }
            else {
                p = q;
                q = q->next;
            }
        }
        return head1->next;
    }
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章