C++ using another idea instead of swapping


  • 1
    S

    idea: take advantage of sign bit in integer

    class Solution {
    public:
        vector<int> findDuplicates(vector<int>& nums) {
            vector<int> ret;
            int mark = 0x80000000;  
            int mask = 0x7fffffff; 
            for (int i = 0; i < nums.size(); ++i){
                int num = nums[i] & mask;
                if (nums[num - 1] & mark)
                    ret.push_back(num);
                nums[num - 1] |= mark;
            }
            return ret;
        }
    };
    

Log in to reply
 

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