给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] :0 <= a, b, c, d < n
a、b、c 和 d 互不相同
nums[a] + nums[b] + nums[c] + nums[d] == target
你可以按 任意顺序 返回答案 。示例 1:输入:nums = [1,0,-1,0,-2,2], target = 0
输出:[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
示例 2:输入:nums = [2,2,2,2,2], target = 8
输出:[[2,2,2,2]]链接:https://leetcode-cn.com/problems/4sumclass Solution:def fourSum(self, nums: List[int], target: int) -> List[List[int]]:result = []if not nums or len(nums) < 4:return resultnums.sort()length = len(nums)for i in range(length-3):if i > 0 and nums[i]==nums[i-1]:continueif nums[i] + nums[length-3] + nums[length-2] + nums[length-1] < target:continueif nums[i] + nums[i+1] + nums[i+2] + nums[i+3] > target:breakfor j in range(i+1,length-2):if j > i+1 and nums[j]==nums[j-1]:continueif nums[i] + nums[j] + nums[length-1] + nums[length-2] < target:continueif nums[i] + nums[j] + nums[j+1] + nums[j+2] > target:breakleft, right = j + 1, length - 1while left < right:total = nums[i] + nums[j] + nums[left] + nums[right] if total == target:result.append([nums[i],nums[j],nums[left],nums[right]])left += 1while left < right and nums[left] == nums[left - 1]:left += 1right -= 1while left < right and nums[right] == nums[right + 1]:right -= 1elif total < target:left += 1else:right -= 1return result

Leetcode 18. 四数之和 (每日一题 20211011)相关推荐

  1. 4. Leetcode 18. 四数之和 (数组-双向双指针)

    给你一个由 n 个整数组成的数组 nums ,和一个目标值 target .请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] ( ...

  2. [Leetcode 18]四数之和 4 Sum

    [题目] Given an array nums of n integers and an integer target, are there elements a, b, c, and d in n ...

  3. LeetCode 18. 四数之和 思考分析(双指针解)

    目录 需要注意的几点 1.去除剪枝操作 2.去重操作的细节 code以及效果: 题目 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b, ...

  4. leetcode 18. 四数之和(双指针)

    给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...

  5. leetcode 18. 四数之和 (C++)

    给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...

  6. leetcode 18. 四数之和

    题目 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出 ...

  7. LeetCode 18 四数之和

    https://leetcode-cn.com/problems/4sum/ 解决方案 class Solution {public List<List<Integer>> f ...

  8. 代码随想录算法训练营第07天 | LeetCode 454.四数相加2,383. 赎金信,15. 三数之和,18. 四数之和,总结

    LeetCode [454. 四数相加 II] 题目:给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足 ...

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

    一.Leetcode 454. 四数相加 II 相当于两数相加.但是呢很巧妙的是,卡哥在遍历CD数组时把查哈希表的方法融入了进去.学习一下. 二.Leetcode 383. 赎金信 更简单了,主要是审 ...

最新文章

  1. 计算两个日期之间有多少天多少小时多少分钟
  2. 电子书下载|2020 年云原生年货小红书来啦!
  3. winform 后台线程更新UI
  4. python 查找指定文件_python实现在目录中查找指定文件的方法
  5. Kibana部署及配置(四)
  6. android黑科技系列——爆破一款应用的签名验证问题
  7. iOS方法类:CGAffineTransform
  8. [干货来袭]C#7.0新特性(VS2017可用)
  9. 苹果屏幕上的小圆点_苹果或明年部署miniLED屏幕 最早用在Macbook上
  10. 【渝粤教育】国家开放大学2018年春季 3924T★汽车电器设备构造与检修 参考试题
  11. AppWeb服务后台登陆及配置的方法解答
  12. 天锐绿盾防泄密软件6.0新版本功能已优化!!!
  13. APU工业控制领域应用
  14. 阿里巴巴四十大盗计算机病毒,泄密 阿里巴巴和四十大盗 全文 - 故事365
  15. 怎样恢复电脑丢失的文件?
  16. Exe文件开机启动,隐藏运行窗口运行
  17. Cesium基础-表面面积量算(依地形量算、依模型表面量算)
  18. 区块链系列 - 以太坊简介
  19. 关于 RTOS 的选择
  20. java安装教程win7_Tomcat服务器安装配置教程(win7)

热门文章

  1. ubuntu下搭建一个数据化处理的开发环境
  2. dyld: Library not loaded: @rpath/Alamofire.framework/Alamofire
  3. 大数据精准营销:买水培栀子花该推送啥
  4. UVA-10954 Add All
  5. 奥迪坚呼叫中心在电话营销领域必须要了解的几大优势
  6. Unix command to find CPU Utilization
  7. 消费者关注的 Win8 问题汇总(中)
  8. CollaDec 之前的三个SharePoint工具开源发布
  9. NAND FLASH读写速度计算方法详解
  10. PAT甲级1064 Complete Binary Search Tree (30分):[C++题解]完全二叉搜索树BST