Double Pointer/Index C++ 40ms Solution


  • 0
    P
    class Solution {
    public:
        bool containsDuplicate(vector<int>& nums) {
            int j = nums.size() - 1;
            if(j < 1) return 0;
            int i = 0;
            unordered_map<int, int> table;
            while(i < j) {
                if(table[nums[i++]]++)
                    return 1;
    
                if(table[nums[j--]]++)
                    return 1;
            }      
            return 0;
        }
    };
    

    Combining the if statements with an 'or' conditional adds an extra 4 ms.


Log in to reply
 

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