tarmogoyf

recursion python

Nov 22nd, 2023
826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. class Solution(object):
  2.     def intersect(self, nums1, nums2):
  3.         """
  4.        :type nums1: List[int]
  5.        :type nums2: List[int]
  6.        :rtype: List[int]
  7.        """
  8.         map = {}
  9.         if len(nums2) > len(nums1):
  10.             return Solution.intersect(self, nums2, nums1)
  11.         for num in nums1:    
  12.             map[num] = map.get(num, 0) + 1
  13.         i = 0
  14.         for num in nums2:
  15.             if map.get(num) > 0:
  16.                 nums1[i] = num
  17.                 map[num]= map.get(num) - 1
  18.                 i += 1
  19.         return nums1[0 : i]        
  20.        
Advertisement
Add Comment
Please, Sign In to add comment