在整数数组nums中,找3个元素(a+b+c)和为0的三元组。
方法1:暴力解法:三重循环 O(N^3) //超时
方法2:排序+双指针法
先排序避免重复答案,降低复杂度变为twoSum,利用双指针找到所有解。
这里说的双指针法:a确定时,b+c 的值就确定为-a。 也就是随着b增加,c减少。那么b,c一共移动的次数是O(N)
在整数数组nums中,找3个元素(a+b+c)和为0的三元组。
方法1:暴力解法:三重循环 O(N^3) //超时
方法2:排序+双指针法
先排序避免重复答案,降低复杂度变为twoSum,利用双指针找到所有解。
这里说的双指针法:a确定时,b+c 的值就确定为-a。 也就是随着b增加,c减少。那么b,c一共移动的次数是O(N)
上一篇:1028 人口普查 (20 分)
下一篇:第6章 最短路径
思路:经过四数之和的洗礼,我想的是可以用循环确定a和b的数
15.三数之和文章目录15.三数之和一、题意二、思考过程哈希解法三、完整代码一、题意二、思考过程哈
举报文章
请选择举报类型
补充说明
0/200
上传截图
格式支持JPEG/PNG/JPG,图片不超过1.9M