Why my code in C++ can't pass the [2, 2, 1] test


  • 0
    Y

    my code is written in C++ and i don't know why it can't pass the [2, 2, 1] test

    the expected result is 1 and mine is 2

    class Solution {
    
    public:
    
    int singleNumber(int A[], int n) {
        
        if(sizeof(A)/sizeof(A[0])==1) return A[0];
        
        vector<int> array(A[0], A[n-1]);
        
        while(true)
        {
            int size=array.size();
            
            vector<int>::iterator pointer=array.begin()+1;
            
            for(vector<int>::iterator tail=array.end(); pointer!=tail; pointer++)
            {
                if(*array.begin()==*pointer)
                {
                    array.erase(array.begin());
                    array.erase(pointer);
                    break;
                }
                
            }
            
            if(array.size()==1) return *array.begin();
            
            if(array.size()==size) return *array.begin();
            
        }   
    }
    

    };


Log in to reply
 

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