程序设计面试题目精讲及技巧.docxVIP

  • 1
  • 0
  • 约6.27千字
  • 约 17页
  • 2026-03-07 发布于福建
  • 举报

第PAGE页共NUMPAGES页

2026年程序设计面试题目精讲及技巧

一、编程实现题(共5题,每题10分)

1.题目(算法设计):

给定一个包含重复整数的数组,返回所有不重复的三元组,使得三元组中的数字相加等于给定的目标值。要求时间复杂度优于O(n2)。

示例输入:

nums=[-1,0,1,2,-1,-4],target=0

示例输出:

[[-1,-1,2],[-1,0,1]]

答案与解析:

python

defthreeSum(nums,target):

nums.sort()

n=len(nums)

res=[]

foriinrange(n):

ifi0andnums[i]==nums[i-1]:

continue

left,right=i+1,n-1

whileleftright:

total=nums[i]+nums[left]+nums[right]

iftotal==target:

res.append([nums[i],nums[left],nums[right]])

whileleftrightandnums[left]==nums[left+1]:

left+=1

whileleftrightandnums[right]

文档评论(0)

1亿VIP精品文档

相关文档