Find All duplicates in an array in O(n) time and O(1) space


  • 0
    T

    '''
    class Solution {
    public:
    vector<int> findDuplicates(vector<int>& nums) {
    int len = nums.size();
    vector<int>res;
    for (int i = 0; i < len; i++) {
    int m = abs(nums[i]) - 1;
    nums[m] = - nums[m];
    if (nums[m] > 0) {
    res.push_back(m+1);
    }
    }
    return res;
    }
    };
    ''''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.