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

《LeetCode 顺序刷题》81 - 90

81、[中等] 搜索旋转排序数组 Ⅱ数组二分查找class Solution { public: bool search(vectorint nums, int target) { int n nums.size(); if (n 0) { return false; } if (n 1) { return nums[0] target; } int l 0, r n - 1; while (l r) { int mid (l r) / 2; if (nums[mid] target) { return true; } if (nums[l] nums[mid] nums[mid] nums[r]) { l; --r; } else if (nums[l] nums[mid]) { if (nums[l] target target nums[mid]) { r mid - 1; } else { l mid 1; } } else { if (nums[mid] target target nums[n - 1]) { l mid 1; } else { r mid - 1; } } } return false; } };82、[中等] 删除排序链表中重复元素 Ⅱ链表class Solution { public: ListNode* deleteDuplicates(ListNode* head) { if (head nullptr) { return nullptr; } ListNode newhead; newhead.next head; ListNode* cur newhead; while (cur-next cur-next-next) { if (cur-next-val cur-next-next-val) { int x cur-next-val; while (cur-next cur-next-val x) { cur-next cur-next-next; } } else { cur cur-next; } } return newhead.next; } };83、[简单] 删除排序链表中的重复元素链表class Solution { public: ListNode* deleteDuplicates(ListNode* head) { if (head nullptr) { return head; } ListNode* cur head; while (cur-next) { ListNode* next cur-next; if (cur-val next-val) { cur-next next-next; delete next; } else { cur cur-next; } } return head; } };84、[困难] 柱状图中最大的矩形栈数组单调栈class Solution { public: int largestRectangleArea(vectorint heights) { int n heights.size(); vectorint left(n), right(n); stackint s; for (int i 0; i n; i) { while (!s.empty() heights[s.top()] heights[i]) { s.pop(); } left[i] s.empty() ? -1 : s.top(); s.push(i); } s stackint(); for (int i n - 1; i 0; --i) { while (!s.empty() heights[s.top()] heights[i]) { s.pop(); } right[i] s.empty() ? n : s.top(); s.push(i); } int ret 0; for (int i 0; i n; i) { ret max(ret, (right[i] - left[i] - 1) * heights[i]); } return ret; } };单调栈常数优化class Solution { public: int largestRectangleArea(vectorint heights) { int n heights.size(); vectorint left(n), right(n, n); stackint s; for (int i 0; i n; i) { while (!s.empty() heights[s.top()] heights[i]) { right[s.top()] i; s.pop(); } left[i] s.empty() ? -1 : s.top(); s.push(i); } int ret 0; for (int i 0; i n; i) { ret max(ret, (right[i] - left[i] - 1) * heights[i]); } return ret; } };86、[中等] 分隔链表链表class Solution { public: ListNode* partition(ListNode* head, int x) { ListNode shead, *cur1 shead; ListNode lhead, *cur2 lhead; ListNode* cur head; while (cur) { if (cur-val x) { cur1-next cur; cur1 cur1-next; } else { cur2-next cur; cur2 cur2-next; } cur cur-next; } cur1-next lhead.next; cur2-next nullptr; return shead.next; } };88、[简单] 合并两个有序数组排序数组双指针逆向双指针class Solution { public: void merge(vectorint nums1, int m, vectorint nums2, int n) { int end1 m - 1, end2 n - 1; int end m n - 1; while (end1 0 end2 0) { if (nums1[end1] nums2[end2]) { nums1[end--] nums1[end1--]; } else { nums1[end--] nums2[end2--]; } } while (end2 0) { nums1[end--] nums2[end2--]; } } };直接合并后排序class Solution { public: void merge(vectorint nums1, int m, vectorint nums2, int n) { for (int i 0; i n; i) { nums1[m i] nums2[i]; } sort(nums1.begin(), nums1.end()); } };90、[中等] 子集 Ⅱ迭代法实现子集枚举class Solution { private: vectorint path; vectorvectorint ret; public: vectorvectorint subsetsWithDup(vectorint nums) { sort(nums.begin(), nums.end()); int n nums.size(); for (int mask 0; mask (1 n); mask) { path.clear(); bool flag true; for (int i 0; i n; i) { if (mask (1 i)) { if (i 0 (mask (i - 1) 1) 0 nums[i] nums[i - 1]) { flag false; break; } path.push_back(nums[i]); } } if (flag) { ret.push_back(path); } } return ret; } };递归实现子集枚举class Solution { private: vectorint path; vectorvectorint ret; void dfs(bool choosePre, int cur, vectorint nums) { if (cur nums.size()) { ret.push_back(path); return; } dfs(false, cur 1, nums); if (!choosePre cur 0 nums[cur - 1] nums[cur]) { return; } path.push_back(nums[cur]); dfs(true, cur 1, nums); path.pop_back(); } public: vectorvectorint subsetsWithDup(vectorint nums) { sort(nums.begin(), nums.end()); dfs(false, 0, nums); return ret; } };
http://www.zskr.cn/news/1315329.html

相关文章:

  • Linux内核PCIe热插拔驱动开发实战:从IDT芯片到稳定运行
  • 2026年知名的小区道闸/智能道闸/赣州人行道闸/公园道闸品牌厂家推荐 - 品牌宣传支持者
  • 龙芯2K3000赋能轨道交通AFC系统:国产化工控平台实战全解析
  • 张量分解与神经网络训练加速的硬件挑战
  • 2026年大体重外卖骑手电动车坐垫/小牛电动车坐垫精选厂家推荐 - 品牌宣传支持者
  • 2026年比较好的实验室/恒温恒湿实验室服务型公司推荐 - 品牌宣传支持者
  • 告别直播平台封禁!用OBS+Smart_rtmpd在局域网内搭建私人游戏直播流(保姆级配置)
  • 2026年比较好的呼市工业管道疏通清淤售后无忧公司 - 行业平台推荐
  • Cadence IC617新手避坑指南:在CentOS7上从零搭建TSMC 65nm工艺库并跑通第一个NMOS仿真
  • 2026年质量好的桩基注浆阀/沧州预埋式注浆阀/桩端注浆阀/单向逆止注浆阀多家厂家对比分析 - 品牌宣传支持者
  • 从零到精通:手把手教你设计生产级Skill,附3个实战案例
  • 拆个汽车配件里的压电陶瓷片,用示波器和面包板实测它的‘发电’与‘震动’能力
  • 告别重复劳动:用这个Maya Mel脚本插件,5分钟搞定Arnold材质批量调节
  • HLK-V20语音模块的智能家居实战:如何用STM32控制灯、电机并连接ESP8266上云
  • Analog Discovery 2:口袋实验室如何用FPGA重塑硬件调试体验
  • 别再乱写RS485协议了!基于STM32F103C8T6,聊聊工业通讯中帧结构的那些坑
  • 2026年海绵不容易塌的浙江减震电动车坐垫/耐老化电动车坐垫主流厂家对比评测 - 行业平台推荐
  • SAP-ABAP:ABAP开发中 DELETE ADJACENT DUPLICATES 去重语句详解:作用、用法与避坑指南
  • Django 从 0 到 1 打造完整电商平台:电商项目需求分析与数据库设计
  • 手把手教你为树莓派CM4或Jetson Nano扩展4G/5G模块:基于Mini PCI-e接口的完整硬件连接与驱动配置指南
  • ARM SVE2非临时存储指令STNT1原理与应用
  • 2026年热门的双鸭山监控设备回收/海康监控设备回收综合评价公司 - 行业平台推荐
  • Molflow仿真结果怎么看?Texture、Profile、Counter Facet全解析,选对方法效率翻倍
  • 【声纳技术手册】3 三维水声传播的快速计算:从海底山脉到水平折射
  • 番茄小说下载器终极指南:5种格式+Web界面打造个人数字图书馆
  • 前后端分离项目避坑指南:为什么你的网关CORS配置了还是报跨域错误?
  • 3篇6章5节:基于 stat_slab () 函数的高血压临床数据可视化
  • 单传感器肌电假肢:DTW算法实现92%识别准确率
  • 别再乱画了!GD32/STM32复位与唤醒按键电路设计,90%新手会踩的坑
  • 内存中心计算:突破存储墙与DRAM可靠性挑战