C 3ms solution without while/loop.


  • 0
    S
    int comp( const int*l, const int*r )
    {
        if( l[0]!=l[1] )
            return -1;
        if( r[0]!=r[1] )
            return 1;
        return 0;
    }
    
    
    int singleNonDuplicate(int* nums, int numsSize) {
        qsort( nums, (numsSize+1)/2, sizeof(nums[0])+sizeof(nums[0]), (int (*)(const void*,const void*)) comp );
        
        return nums[0];
    }
    

  • 0

    @strangenaiad Awesome~


  • 0
    S

    @ypochien

    for example
    [1,1,2,3,3]
    [1,1,2,3,3,4,4]
    [1,1,2,3,3,3,3,4,4]

    We cat treat it as int pair:
    [1,1], [2,3], [3,]
    [1,1], [2,3], [3,4], [4, ]
    [1,1], [2,3], [3,3], [3,4], [4, ]

    The answer will always be the 1st (and also smallest) in different pair
    Thus, qsort can simply work fine and quickly.

    This solution is not a regular one, but it works just fine on this very condition.


  • 0
    R
    #include<bits/stdc++.h>
    using namespace std;
    
    int main()
    {
        int a[2]={0},res=0;
          while(cin>>a[0] && cin>>a[1])
           {
                  if(a[0]!=a[1])
                       {res=a[0];}
    
           }
           cout<<res;
        return 0;
    }
    

    cannot i do this because it seems pretty simple??


Log in to reply
 

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