classSolution: deffourSum(self, nums: List[int], target: int) -> List[List[int]]: N = len(nums) nums_asc = sorted(nums) ans = [] for a inrange(0, N-3): # 判断是否与前一个值相等 if a>0and nums_asc[a]==nums_asc[a-1]: continue for b inrange(a+1, N-2): if b>a+1and nums_asc[b]==nums_asc[b-1]: continue c, d = b+1, N-1 while(c<d): val = nums_asc[a]+nums_asc[b]+nums_asc[c]+nums_asc[d] if val > target: d-=1 elif val < target: c+=1 else: ans.append([nums_asc[a], nums_asc[b], nums_asc[c], nums_asc[d]]) # 跳过相等的元素 while(c<d and nums_asc[c]==nums_asc[c+1]): c+=1 while(c<d and nums_asc[d]==nums_asc[d-1]): d-=1 c += 1 d -= 1 return ans