C++ solution without using two pointers


  • 0
    S

    I found a very impressive solution in the youtube. Check this: https://www.youtube.com/watch?v=HuZJqRDOPo0

    class Solution {
    public:
        int findDuplicate(vector<int>& nums) {
            for ( int i = 0; i < nums.size(); i++ ) {
                int index = abs(nums[i]);
                if ( nums[index] > 0 ) {
                    nums[index] = -nums[index];
                } else return index;
            }
        }
    };
    

Log in to reply
 

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