Simple accepted python answer with explanation

  • 0

    My solution is simple, but a little stupid.
    First, find the same items in both lists, create a new list to store those items
    then, calculate the value(index sum) for each item
    finally, compare the value and return the correct items in a form a list

    class Solution(object):
        def findRestaurant(self, list1, list2):
            :type list1: List[str]
            :type list2: List[str]
            :rtype: List[str]
            common = []
            # for i in range(len(list1)):
            i = 0
            while i < len(list1):
                if list1[i] in list2:
            if len(common)==0 or len(common)==1:
                return common
            # elif list1.index(common[i]) i in range(len(common)) 
            common_sum = []
            for i in range(len(common)):
            # common_sum_sorted = sorted(common_sum)
            # for i in range(len(common)):
            if common_sum.count(common_sum[0])==1:
                return [common[0]]
            if common_sum.count(common_sum[0])>=1:
                return common[0:common_sum.count(common_sum[0])]

Log in to reply

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