Promote the best solution so far


  • 0
    L

    https://leetcode.com/discuss/103248/java-o-min-n1-n2-space-o-n1-n2-time-one-hashset

    3 reasons for me to consider above solution is the best solution, better than the solution that received the most vostes so far.

    Reason 1: space more efficient and time complexity is as good

    Reason 2: putting fewer elelements in hahset will reduce the chance of collisions happening

    Reason 3: I like one line of codes in the above solution. It further reduces the chance of collisions happening

         if (set.remove(num)) {
                list.add(num);
          }
    

    although search time complexity of hashset/hashtable/map is O(1), that is for the average case. In the worst case where collisions happen, the search time complexity could be as bad as O(n). The above codes not only satisfy our search needs but also remove unneeded elements which reduce the chance of collisions happening. I have not seen other solutions take care of collisions better than this solution so far.

    Please vote this solution up and let it receive more deserved votes.


Log in to reply
 

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