Simple, beat 99%, good to know.

  • 0

    Complexity is O(n) Time and O(1) space - Negate values at position, mark it as negative, when it negates to same position and its value is negative, push it to the result :) Simple, good luck and best wishes to everyone

    class Solution {
        vector<int> findDuplicates(vector<int>& nums) {
            vector<int> res;
            for (int i = 0; i < nums.size(); i++){
                int m = abs(nums[i]) - 1;
                if (nums[m] > 0) {
                    nums[m] = - nums[m];
                else 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.