Is that count " without using extra memory" ?


  • 0
    A
        public int singleNumber(int[] A) {
        HashSet<String> hash = new HashSet<String>();
        for(int i = 0 ; i<A.length ; i ++ )
        {
            if(hash.contains(A[i]+""))
            {
                hash.remove(A[i]+"");
                hash.add(A[i]+" ");
            }else if (hash.contains(A[i]+" "))
            {
                hash.remove(A[i]+" ");
            }else
            {
                hash.add(A[i]+"");
            }
        }
        for(String s : hash)
        {
            if(s.contains(" "))
            	s = s.substring(0, s.length()-1);
            return Integer.valueOf(s);
        }
        return 0 ;
    }

  • 0
    S

    No, this uses extra memory. Specifically, the number of entries in hash is the number of entries of A, so it uses O(n) space. No extra memory means the memory used does not depend on the size of A.


Log in to reply
 

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