四元组相加获得target

4Sum

  • 给定一个数组,选择四个元素相加,结果为target,找出所有符合的四元组。

  • Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note: The solution set must not contain duplicate triplets.

example 1

For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.A solution set is:
[[-1,  0, 0, 1],[-2, -1, 1, 2],[-2,  0, 0, 2]
]

思路

  1. 思路参照三元组相加获得target

  2. 多一层循环即可,注意边界检测即可。

代码

class Solution(object):def fourSum(self, nums, target):""":type nums: List[int]:type target: int:rtype: List[List[int]]"""nums.sort()ret = []for i in range(len(nums) - 3):if i > 0 and nums[i] == nums[i - 1]:continuefor j in range (i+1, len(nums) - 2):if j > i + 1 and nums[j] == nums[j - 1]:continuehead, tail = j+1, len(nums) - 1while head < tail:if nums[i] + nums[j] + nums[head] + nums[tail] == target:ret.append([nums[i], nums[j], nums[head], nums[tail]])head += 1tail -= 1while head < tail and nums[head] == nums[head - 1]:head += 1while head < tail and nums[tail] == nums[tail + 1]:tail -= 1elif nums[i] + nums[j] + nums[head] + nums[tail] > target:tail -= 1else:head += 1return ret

本题以及其它leetcode题目代码github地址: github地址

四元组相加获得target相关推荐

  1. 三元组相加获得target

    三元组相加获得target 3Sum 给定一个数组,选择三个元素相加,结果为target,找出所有符合的三元组 Given an array S of n integers, are there el ...

  2. (补)算法训练第七天|力扣454.四数相加II ,383. 赎金信,15. 三数之和,18. 四数之和

    代码随想录算法训练营第七天|力扣454.四数相加II ,383. 赎金信,15. 三数之和,18. 四数之和 454.四数相加II 题目链接:四数相加II 参考:https://programmerc ...

  3. 算法-查找数组两个数据相加等于目标值

    给定一个整数数组nums和一个整数目标值target,请你在该数组中找出和为目标值target的那两个整数,并返回他们的数组下标. 示例1: 输入:nums=[2,7,11,15] target =9 ...

  4. 代码随想录算法训练营第七天|454.四数相加II、383. 赎金信、15. 三数之和、18. 四数之和

    今日学习的文章和视频链接 454文章链接: link 454视频讲解链接: link 383文章链接: link 383视频暂无讲解 15文章链接: link 15视频讲解链接: link 18文章链 ...

  5. 字节跳动客户开发_实习|字节跳动 客户端实习生 1-5面 面经

    作者:Simple零 链接:https://www.nowcoder.com/discuss/366054?from=zhnkw 来源:牛客网 目前已经拿到实习offer,牛客上各位牛友的面经真的帮助 ...

  6. leetcode_two sum()

    最近觉得自己很渣,所以在这里立下flag,从今天开始每天刷一道leetcode上的题目,并且在这里进行代码整理,闲话少说,进入正题: 一.Two sum 题目要求: 我的答案: vector<i ...

  7. 剑指 offer 编程题 C++ 版总结(下)

    解题思路:让两个指针分别指向两个链表,当遍历到最末尾的结点后,指针指向另一个链表的头结点.例如:A 和 B 开始的时候分别指向 4 和 5.两个指针分别遍历链表,当 A 遍历到末尾结点的 5 时,下一 ...

  8. LeetCode题库第1题 两数之和

    两数之和 LeetCode题库第1题 两数之和 看到题,我就想到了暴力法: public int[] force(int[] nums,int target) {for (int i = 0; i & ...

  9. c语言中dfs用pos做参数,LeetCode算法练习——深度优先搜索 DFS(2)

    更多干货就在我的个人博客 BlackBlog.tech 欢迎关注! 也可以关注我的csdn博客:黑哥的博客 谢谢大家! 我们继续LeetCode之旅. 做了一段时间的LeetCode,感觉还是不错的. ...

最新文章

  1. ROS之py文件权限
  2. 关于bds2006里面的indy 问题!!!!!!
  3. python 统计使用技巧
  4. 诗歌rials 之RJS的tips
  5. react hooks使用_我如何使用React Hooks在约100行代码中构建异步表单验证库
  6. php 社区,社区(phpmysql)一
  7. 锁失效_关于bigtable中chubby锁失效时的一点思考
  8. 游戏开发之多态及虚函数(C++基础)
  9. myeclipse打开JSP电脑很卡,CPU使用率90%以上
  10. jquery addClass,removeClass 设置或删除类
  11. Javascript的块级作用域
  12. 操作 神通数据库_神通数据库命令行
  13. linux ubuntu下网络调试助手(GUI)工具
  14. 在 QNAP(威联通)NAS 上自动查找和删除 重复文件 的方法
  15. html 链接长宽,CSS实现长宽比的几种方案【转载】
  16. 微信小程序 - 实现手机号登录--授权并获取手机号保存至本地
  17. 共享单车背后还隐藏着多少惊天秘密?
  18. 健康课程小程序开发,传播正确养生方法,拥抱健康生活
  19. Java引用包的方法
  20. 工具学习——有哪些好用的学术翻译工具

热门文章

  1. 任何时候都不要轻易满仓
  2. 欲学机器学习必先掌握Shell,AI工程师自制教程,获Reddit网友400+点赞 | PDF+视频...
  3. CNN更新换代!性能提升算力减半,还即插即用
  4. 2019ASC世界大学生超算竞赛预赛结果出炉:20校晋级,北航第一
  5. 中国学霸本科生提出AI新算法:速度比肩Adam,性能媲美SGD,ICLR领域主席赞不绝口...
  6. digiKam 6.1.0 发布,相片管理工具
  7. AJAX,JSON,GSON
  8. 将Hibernate中的枚举转换为自定义数值
  9. 模板引擎 Velocity
  10. Android 连接SQLite