当前位置: 首页 > news >正文

继续打卡day6

383. 赎金信 - 力扣(LeetCode)

class Solution {
public:bool canConstruct(string ransomNote, string magazine) {unordered_map<char, int> us;for(auto c: ransomNote){us[c]++; // 将字符串存储}for(auto c: magazine){if(us.count(c)){us[c]--;if(us[c] == 0) us.erase(c);}}return us. Empty();}
};

 15. 三数之和 - 力扣(LeetCode)

还是不会处理这里的去重逻辑

class Solution {
public:vector<vector<int>> threeSum(vector<int>& nums) {sort(nums.begin(), nums.end());vector<vector<int>> ans;auto n = nums.size();for(auto i = 0; i < n - 2; i++){  // 边界注意是n-2int left = i + 1, right = n -1;if(i > 0 && nums[i-1] == nums[i]) continue;while(left < right){int target = nums[i] + nums[left] + nums[right];if(target > 0){right--;}else if(target < 0){left++;} else{vector<int> res = {nums[i], nums[left], nums[right]};ans.push_back(res);while(left < right && nums[left] == nums[left+1]) left++;while(right > left && nums[right-1] == nums[right]) right--;left++;right--;}}}return ans;}
};

454. 四数相加 II - 力扣(LeetCode)

class Solution {
public:int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {unordered_map<int, int> ump;for(auto a: nums1){for(auto b: nums2){ump[a+b]++; // 统计可能出现的组数 , 次数}}int ans = 0;for(auto c : nums3){for(auto d : nums4){if(ump.count(-(c+d))){ans += ump[-c -d];}}}return ans;}
};

18. 四数之和 - 力扣(LeetCode)

class Solution {
public:vector<vector<int>> fourSum(vector<int>& nums, int target) {vector<vector<int>> res;int n = nums.size();if(n < 4) return res;sort(nums.begin(), nums.end());for(int i = 0; i < n - 3; i++){if(i > 0 && nums[i] == nums[i-1]) continue;for(int j = i+1; j < n - 2; j++){if(j > i + 1 && nums[j-1] == nums[j]) continue;long long sum2 = nums[i] + nums[j];// 双指针int left = j + 1, right = n - 1;while(left < right){long long sum4 = sum2 + nums[left] + nums[right];if(target > sum4){left++;}else if(target < sum4){right--;} else{ // 找到一个4元组合res.push_back({nums[i], nums[j], nums[left],nums[right]});while(left < right && nums[right-1] == nums[right]) right--;while(left < right&& nums[left+1] == nums[left]) left++;left++, right--;}}}}return res;}
};

总结:这次有的题目第二次写了还是不会,归根结底是对原理不清楚,只是在背题解一样,要多理解为什么这样写的原理。最难不过坚持!!!

http://www.dtcms.com/a/305415.html

相关文章:

  • SpringJDBC源码初探-DataSource类
  • 理解“无界队列”与“有界队列”及其适用场景
  • BigemapPro吸附功能 | 绘图共点共边,标绘从此无缝衔接!
  • 【Python】数据可视化之聚类图
  • 进阶向:Manus AI与多语言手写识别
  • 大模型量化004
  • 机器学习-贝叶斯函数(理解版)
  • Xmind 2025下载与保姆级安装教程
  • 数据库-索引
  • Python Day17 常用模块 和 加解密操作 及例题分析
  • window weblogic 解锁
  • Java 9 新特性解析
  • 《零基础入门AI:传统机器学习入门(从理论到Scikit-Learn实践)》
  • 36.Manacher 算法
  • 【n8n】如何跟着AI学习n8n【01】:定制AI老师
  • 【Linux】pthread学习笔记
  • scrapy框架新浪新闻
  • 使用JSON Schema 的 dependencies 实现 LLM 工具调用的参数约束
  • C 语言基础第16天:指针补充
  • 粒子群优化算法(Particle Swarm Optimization, PSO) 求解二维 Rastrigin 函数最小值问题
  • Mysql缓冲池和LRU
  • 关注 Yocto项目实战教程
  • PyCharm插件开发与定制指南:打造个性化开发环境
  • C++ 模板类型传递可行性检测指南
  • 3D打印喷头的基本结构
  • 区间DP求解策略详解
  • cmseasy靶机密码爆破通关教程
  • 第一章 RAG三问
  • flask使用celery通过数据库定时
  • 【专题十六】BFS 解决最短路径