Python code, 3 lines using set


  • 22
    class Solution(object):
    def intersection(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        nums1=set(nums1)
        nums2=set(nums2)
        return list(nums1&nums2)

  • 15
    R

    make it 1 :)

    def intersection(self, nums1, nums2):
            return list(set(nums1) & set(nums2))

  • 0
    C

    this is a good answer, I don't know why some one vote down.


  • 0
    Z

    class Solution(object):
    def intersection(self, nums1, nums2):

        i = []
    
        for k in nums1:
            if k in nums2 and k not in i:
                i.append(k)
        
        return i

  • 11
    H

    @cheffyu because this answer just used the python implementation of intersection, it is same as

    return  set(nums2).intersection(nums2)
    

    If you do this during interview, it would definitely not impress the interviewer.


  • 1
    L

    @cheffyu
    I think the reason is that this is not an algorithm solution. The "intersection" operation of two arrays ("lists" here in Python) should be IMPLEMENTED, rather then USED. The problems here we are working on are under the topic of "Algorithm", and this solution just uses the built-in (it's good, but it's not the wanted one). If Python can use all its built-in and libraries, many hard problems here is not a real problem.


  • 0

    Make it 0 line :)

    intersection = lambda *p: list(set(p[1]) & set(p[2]))

  • 0
    A
    This post is deleted!

  • 0
    R

    @hui19 I do not understand why it would be a problem -

    1. It is the most efficient implementation possible (in Python)
    2. It shows that you know how to use sets.
    3. It is the most Pythonic solution and lets the interviewer know that you know what you are doing.

    If asked otherwise, you can always write a trivial algorithm, but it won't have the above advantages :)


  • 0
    L

    @rocode This online judge exists for helping people preparing algorithm interview, not for code golf. Of course, you can do it in a code golf style for fun. For languages like Python, which has a large standard library, you can often find ways to solve algorithm questions with standard library methods. However, the interviewers are more interested in how do you implement the algorithm in details (you don't need to write a python program in the style of C, but you need to show more implementation details than "the most Pythonic solution"), not how can you find a better way to combine standard library methods. For short, the interviewers want to find programmers who read and understand Algorithm books, not programmers who read and understand Python Cookbook.


  • 0
    R

    @lanjiann Might be true in some cases, but in most cases, I do not think that matters. You need to have a good balance of Algorithms knowledge and a good grasp over the language of your choice. That is what I was trying to communicate in my answer. If you can implement using more efficient functions in Python (that is, you know what is to be done), you can almost always implement it normally with the details, just that it will take more time. So I do not think that is a problem.
    Still, we live in a free world and are free to make our own choices. So, make your choice depending on the situation is what I would say!


Log in to reply
 

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