454.四数相加II

题目链接

给你四个整数数组 nums1nums2nums3nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足:

  • 0 <= i, j, k, l < n
  • nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
class Solution {public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {Map<Integer, Integer> countAB = new HashMap<>();for (int i : nums1) {for (int j : nums2) {countAB.put(i + j, countAB.getOrDefault(i + j, 0) + 1);}}int ans = 0;for (int k : nums3) {for (int l : nums4) {ans += countAB.getOrDefault(-k - l, 0);}}return ans;}
}

383. 赎金信

题目链接

给你两个字符串:ransomNotemagazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。

如果可以,返回 true ;否则返回 false

magazine 中的每个字符只能在 ransomNote 中使用一次。

提示:

  • 1 <= ransomNote.length, magazine.length <= 105
  • ransomNotemagazine 由小写英文字母组成
class Solution {public boolean canConstruct(String ransomNote, String magazine) {if (ransomNote.length() > magazine.length()) {return false;}int[] temp = new int[26];for (char c : magazine.toCharArray()) {temp[c - 'a']++;}for (char c : ransomNote.toCharArray()) {temp[c - 'a']--;if (temp[c - 'a'] < 0) {return false;}}return true;}
}

15. 三数之和

题目链接

给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != ji != kj != k ,同时还满足 nums[i] + nums[j] + nums[k] == 0 。请

你返回所有和为 0 且不重复的三元组。

注意:答案中不可以包含重复的三元组。

class Solution {public List<List<Integer>> threeSum(int[] nums) {List<List<Integer>> ans = new ArrayList<>();Arrays.sort(nums);int length = nums.length;for (int first = 0; first < length; first++) {//去重if (first > 0 && nums[first] == nums[first - 1]) {continue;}int third = length - 1;for (int second = first + 1; second < length; second++) {//去重if (second > first + 1 && nums[second] == nums[second - 1]) {continue;}while (second < third && nums[first] + nums[second] + nums[third] > 0) {third--;}if (second==third){break;}if(nums[first] + nums[second] + nums[third]==0){ans.add(Arrays.asList(nums[first],nums[second],nums[third]));}}}return ans;}
}

18. 四数之和

题目链接

给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] (若两个四元组元素一一对应,则认为两个四元组重复):

  • 0 <= a, b, c, d < n
  • abcd 互不相同
  • nums[a] + nums[b] + nums[c] + nums[d] == target

你可以按 任意顺序 返回答案 。

代码类似三数之和

class Solution {public List<List<Integer>> fourSum(int[] nums, int target) {List<List<Integer>> ans = new ArrayList<>();if (nums == null || nums.length < 4) {return ans;}Arrays.sort(nums);int length = nums.length;for (int first = 0; first < length - 3; first++) {if (first > 0 && nums[first] == nums[first - 1]) {continue;}if ((long)nums[first] + nums[first + 1] + nums[first + 2] + nums[first + 3] > target) {break;}if ((long)nums[first] + nums[length - 3] + nums[length - 2] + nums[length - 1] < target) {continue;}for (int second = first + 1; second < length - 2; second++) {if (second > first + 1 && nums[second] == nums[second - 1]) {continue;}if ((long)nums[first] + nums[second] + nums[second + 1] + nums[second + 2] > target) {break;}if ((long)nums[first] + nums[second] + nums[length - 2] + nums[length - 1] < target) {continue;}int fourth = length - 1;for (int third = second+1; third < length - 1; third++) {if (third > second + 1 && nums[third] == nums[third - 1]) {continue;}while (third < fourth && (long)nums[first] + nums[second] + nums[third] + nums[fourth] > target) {fourth--;}if (third == fourth) {break;}if ((long)nums[first] + nums[second] + nums[third] + nums[fourth] == target) {ans.add(Arrays.asList(nums[first], nums[second], nums[third], nums[fourth]));}}}}return ans;}
}

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

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

    代码随想录算法训练营day6| 454.四数相加II 383.赎金信 15.三数之和 18.四数之和 LeetCode 454 四数相加II 题目链接: 454.四数相加II class Soluti ...

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

    一.454.四数相加II 力扣 思路:第一眼还没反应过来,真是缺练.在四个数组中分别寻找,可以先把前两个数组的和先存入map中,再计算后两个数组元素的和,看一下相反数在map中出现没有,出现过就res ...

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

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

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

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

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

    454. 四数相加 II 题解及想法 通过两个for循环先遍历a和b,key放a和b两数之和,value 放a和b两数之和出现的次数,再通过两个for循环遍历c和d,如果0-(c+d) 在map中出现 ...

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

    Leetcode 454.四数相加II 思路分析: 本题直观的想法是采取暴力法,四数相加就用四层for循环.虽然能得到结果,但时间复杂度为o(n4),当数组长度较大时,Leetcode便提示超时.该方 ...

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

    454.四数相加Ⅱ.383.赎金信.15.三数之和四数之和 四数相加 对于四数相加,我们可以定义一个map用来记录nums1与nums2的和对应次数,再遍历nums3与nums4,如果存在c与d使得a ...

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

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

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

    一.参考资料 四数相加II 题目链接/文章讲解/视频讲解:https://programmercarl.com/0454.%E5%9B%9B%E6%95%B0%E7%9B%B8%E5%8A%A0II. ...

最新文章

  1. H5中canvas和svg绘图方式介绍
  2. .NET Core 3.1正式发布,还不赶快升级!
  3. 15、sql编程基本语法介绍
  4. C语言指针变量的运算
  5. 宁静——一种心灵的奢望
  6. 说说 Python TestCase 中的断言方法
  7. 百度地图行政区划遮罩+描点+信息窗demo
  8. 伦敦银短线交易_MOM指标
  9. 在终端输入“python”或者”pip“时出现python’不是内部或外部命令,也不是可运行的程序 或批处理文件
  10. 模型的骨骼动画技术讲解
  11. 华为LiteOS操作系统中的LOS_DL_LIST_ENTRY怎么理解?
  12. esc键 qt 退出菜单_qt之esc键
  13. Android系统SD卡各类文件夹名称
  14. 详解物联网常用协议:IIC和RS485通信协议
  15. Ymir初级使用教程
  16. python读取特定单词_在文本python中搜索特定单词
  17. LCD(五)Backlight背光子系统
  18. 独角兽项目 7 - 从环境到测试
  19. php解析bt,PHP基于闭包思想实现的BT(torrent)文件解析工具实例详解
  20. Microchip PIC24F dsPIC33E flash 自擦写的尿性小结

热门文章

  1. HTML 修改时间日期,文件属性创建时间修改时间
  2. 【深度学习】Weight Normalization: 一种简单的加速深度网络训练的重参数方法
  3. 博文写作、知识学习的技巧
  4. 文案优化技巧,批量文案改写工具
  5. raid6 p和q计算方法
  6. 【python】datetime类型转换
  7. 下拉列表刷新,分页加载
  8. Run-Time Check Failure #2 问题
  9. [转]Windows Server 2008 对 CPU 及 RAM 的支持规格
  10. 【ARM-Linux开发】Rico Board DIY系列实验教程 Day 2——搭建Boa服务器