C++ summary of ALL Duplicated Related Problem Set

Problem 26 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.*

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int n = nums.size();
        if (n <= 1) return n;
        //pre : record the end index of the different number array
        //cur : record the current checking index
        int pre = 0, cur = 0;
        while (cur < n) {
            if (nums[cur] == nums[pre]) ++cur;
            else {
                nums[pre+1] = nums[cur];
                pre++;
                cur++;
            }
        }
        return pre + 1;
    }
};

Problem 80 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice?

Here is a general implementation to solve the problem like this that the duplicates are allowed at most k times

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int k = 2;
        int n = nums.size();
        if (n <= k) return n;
        int pre = 1, cur = 1, count = 1;
        while (cur < n) {
            if (nums[cur] != nums[cur-1]) {
                count = 1;
                nums[pre++] = nums[cur];
            }
            else {
                //only record the duplicate numbers for k times at most 
                if (count < k) {
                    nums[pre++] = nums[cur];
                    count++;
                }
            }
            cur++;
        }
        return pre;
    }
};

Problem 83 --- Given a sorted linked list, delete all duplicates such that each element appear only once.

class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if (!head || !head->next) return head;
        ListNode* cur = head;
        while (cur && cur->next) {
            //duplicate value, we delete the next node 
            if(cur->val == cur->next->val) {
                ListNode* temp = cur->next;
                cur->next = cur->next->next;
                delete temp;
            }
            else {
                cur = cur->next;
            }
        }
        return head;
    }
};

Problem 83 ----- Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Given 1->2->3->3->4->4->5, return 1->2->5.

class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if (!head || !head->next) return head;
        ListNode* start = new ListNode(0);
        start->next = head;
        //pre record all the unique value node 
        ListNode* pre = start;
        while (pre->next) {
            ListNode* cur = pre->next;
            //skip all the duplicate value node 
            while (cur->next && cur->next->val == cur->val) {
                ListNode* temp = cur;
                cur = cur->next;
                delete temp;
            }
            //cur point to the duplicate value , we skip all the duplicate value 
            if (cur != pre->next) pre->next = cur->next;
            //no duplicate value, just move forward 
            else pre = pre->next;
        }
        return start->next;
    }
};

Problem 27 ----- Remove Element Given an array and a value, remove all instances of that value in place and return the new length.

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

Contain Duplicates 1 ----- Problem 217 ----- Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        for (int i = 1; i < nums.size(); ++i) {
            if (nums[i] == nums[i - 1]) return true;
        }
        return false;
    }
};

Contain Duplicates 2 ------- Problem Set 219 ------ Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.

class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        unordered_map<int, int> m;
        for (int i = 0; i < nums.size(); ++i) {
            if (m.find(nums[i]) != m.end() && i - m[nums[i]] <= k) return true;
            else m[nums[i]] = i;
        }
        return false;
    }
};

Contain Duplicates 3 ------------- Problem Set 220 Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k.

class Solution {
public:
    bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
        multiset<long long> bst;
        for (int i = 0; i < nums.size(); i++) {
            if (bst.size() == k + 1) bst.erase(bst.find(nums[i - k - 1]));
            auto lb = bst.lower_bound(nums[i] - t);
            if (lb != bst.end() && (*lb - nums[i] <= t)) return true;
            bst.insert(nums[i]);
        }
        return false;
    }
};

Problem 287 ------- Find the Duplicate Number ---- Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.

class Solution {
public:
    int findDuplicate(vector<int>& nums) {
        int low = 1;
        int high = nums.size();
        int mid = 0, count = 0;
        while (low < high) {
            mid = (low + high) / 2;
            count = 0;
            for(auto num : nums) 
                if (num <= mid)  count++;
            if (count <= mid)
                low = mid + 1;
            else 
                high = mid;
        }
        return low;
    }
};

Problem 316 ----- Remove Duplicate Letters ---------- Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.

class Solution {
public:
    string removeDuplicateLetters(string s) {
        int m[256] = {0}, visited[256] = {0};
        string res = "0";
        //1st pass to get the frequency of each word
        for (auto a : s) ++m[a];
        for (auto a : s) {
            --m[a];
            if (visited[a]) continue;
            while (a < res.back() && m[res.back()]) {
                visited[res.back()] = 0;
                res.pop_back();
            }
            res += a;
            visited[a] = 1;
        }
        return res.substr(1);
    }
};
Comments (0)