lass Solution:
def getCommon(self, nums1: List[int], nums2: List[int]) -> int:
# 1. declare two pointers for nums1 and nums2
# 2. move pointer which points to smaller value left to right
# 3. if values are equal, return the value
# 4. return -1 if index is out of bound, or, no common element exists
p1 = p2 = 0
while p1 < len(nums1) and p2 < len(nums2):
v1, v2 = nums1[p1], nums2[p2]
if v1 > v2:
p2 += 1
elif v1 < v2:
p1 += 1
else:
return v1
return -1
Python
복사