《LeetCode 顺序刷题》11 -20
11、[中等] 盛最多水的容器双指针cclass Solution { public: int maxArea(vectorint height) { int left 0; int right height.size() - 1; int ret 0; while (left right) { int v min(height[left], height[right]) * (right - left); ret max(ret, v); // 移动指针 if (height[left] height[right]) { left; } else { --right; } } return ret; } };JavaclassSolution{publicintmaxArea(int[]height){intleft0;// 左指针intrightheight.length-1;// 右指针intmaxArea0;// 最大面积while(leftright){// 计算当前容器的面积intcurrentAreaMath.min(height[left],height[right])*(right-left);// 更新最大面积maxAreaMath.max(maxArea,currentArea);// 移动指针if(height[left]height[right]){left;// 如果左边高度较小移动左指针}else{right--;// 如果右边的高度较小移动右指针}}returnmaxArea;}}12、[中等] 整数转罗马数字数学字符串模拟const pairint, string valueSymbols[] { {1000, M}, {900, CM}, {500, D}, {400, CD}, {100, C}, {90, XC}, {50, L}, {40, XL}, {10, X}, {9, IX}, {5, V}, {4, IV}, {1, I}, }; class Solution { public: string intToRoman(int num) { string roman; for (const auto [value, symbol] : valueSymbols) { while (num value) { num - value; roman symbol; } if (num 0) { break; } } return roman; } };硬编码数字const string thousands[] {, M, MM, MMM}; const string hundreds[] {, C, CC, CCC, CD, D, DC, DCC, DCCC, CM}; const string tens[] {, X, XX, XXX, XL, L, LX, LXX, LXXX, XC}; const string ones[] {, I, II, III, IV, V, VI, VII, VIII, IX}; class Solution { public: string intToRoman(int num) { return thousands[num / 1000] hundreds[num % 1000 / 100] tens[num % 100 / 10] ones[num % 10]; } };13、[简单] 罗马数字转整数哈希表数学字符串class Solution { private: unordered_mapchar, int symbolValue {{I, 1}, {V, 5}, {X, 10}, {L, 50}, {C, 100}, {D, 500}, {M, 1000}}; public: int romanToInt(string s) { int ret 0; int n s.size(); for (int i 0; i n; i) { int value symbolValue[s[i]]; if (i n - 1 value symbolValue[s[i 1]]) { ret - value; } else { ret value; } } return ret; } };14、[简单] 最长公共前缀字符串二分查找横向扫描class Solution { public: string longestCommonPrefix(vectorstring strs) { // 解法一两两比较 string ret strs[0]; for (int i 1; i strs.size(); i) { ret findCommon(ret, strs[i]); } return ret; } string findCommon(string s1, string s2) { int i 0; while (i min(s1.size(), s2.size()) s1[i] s2[i]) { i; } return s1.substr(0, i); } };纵向扫描class Solution { public: string longestCommonPrefix(vectorstring strs) { if (!strs.size()) { return ; } int length strs[0].size(); int count strs.size(); for (int i 0; i length; i) { char c strs[0][i]; for (int j 1; j count; j) { if (i strs[j].size() || strs[j][i] ! c) { return strs[0].substr(0, i); } } } return strs[0]; } };分治class Solution { public: string longestCommonPrefix(vectorstring strs) { if (!strs.size()) { return ; } else { return longestCommonPrefix(strs, 0, strs.size() - 1); } } string longestCommonPrefix(const vectorstring strs, int start, int end) { if (start end) { return strs[start]; } else { int mid (start end) / 2; string lcpLeft longestCommonPrefix(strs, start, mid); string lcpRight longestCommonPrefix(strs, mid 1, end); return commonPrefix(lcpLeft, lcpRight); } } string commonPrefix(const string lcpLeft, const string lcpRight) { int minLength min(lcpLeft.size(), lcpRight.size()); for (int i 0; i minLength; i) { if (lcpLeft[i] ! lcpRight[i]) { return lcpLeft.substr(0, i); } } return lcpLeft.substr(0, minLength); } };排序class Solution { public: string longestCommonPrefix(vectorstring strs) { if (strs.empty()) { return ; } sort(strs.begin(), strs.end()); string start strs.front(); string end strs.back(); int n min(start.size(), end.size()); int i 0; for (i 0; i n start[i] end[i]; i) ; return string(start, 0, i); } };15、[中等] 三数之和排序数组双指针class Solution { public: vectorvectorint threeSum(vectorint nums) { sort(nums.begin(), nums.end()); vectorvectorint ret; int n nums.size(); for (int i 0; i n; i) { if (nums[i] 0) { break; } int left i 1; int right n - 1; int target -nums[i]; while (left right) { int sum nums[left] nums[right]; if (sum target) { --right; } else if (sum target) { left; } else { ret.push_back({nums[i], nums[left], nums[right]}); left; --right; while (left right nums[left] nums[left - 1]) { left; } while(left right nums[right] nums[right 1]) { --right; } } } while (i n - 1 nums[i] nums[i 1]) { i; } } return ret; } };16、[中等] 最接近的三数之和排序数组双指针class Solution { public: int threeSumClosest(vectorint nums, int target) { sort(nums.begin(), nums.end()); int n nums.size(); int ret 1e7; // 根据差值的绝对值来更新答案 auto update [](int cur) { if (abs(cur - target) abs(ret - target)) { ret cur; } }; // 枚举 a for (int i 0; i nums.size(); i) { // 保证和上一次枚举的元素不相等 if (i 0 nums[i] nums[i - 1]) { continue; } // 使用双指针枚举 b 和 c int left i 1, right n - 1; while (left right) { int sum nums[i] nums[left] nums[right]; // 如果和为 target 直接返回答案 if (sum target) { return target; } update(sum); if (sum target) { // 如果和大于 target移动 c 对应的指针 right--; while (left right nums[right] nums[right 1]) { right--; } } else { // 如果和小于 target移动 b 对应的指针 left; while (left right nums[left] nums[left - 1]) { left; } } } } return ret; } };17、[中等] 电话号码的字母组合哈希表字符串回溯class Solution { private: string hash[10] {, , abc, def, ghi, jkl, mno, pqrs, tuv, wxyz}; string path; vectorstring ret; void dfs(string digits, int pos) { if (pos digits.size()) { ret.push_back(path); return; } for (const auto ch : hash[digits[pos] - 0]) { path.push_back(ch); dfs(digits, pos 1); path.pop_back(); } } public: vectorstring letterCombinations(string digits) { if (digits.empty()) { return {}; } dfs(digits, 0); return ret; } };18、[中等] 四数之和排序数组双指针class Solution { public: vectorvectorint fourSum(vectorint nums, int target) { int n nums.size(); if (n 4) { return {}; } vectorvectorint ret; sort(nums.begin(), nums.end()); for (int i 0; i n - 3; i) { for (int j i 1; j n - 2; j) { long long newtarget (long long)target - nums[i] - nums[j]; int left j 1, right n - 1; while (left right) { int sum nums[left] nums[right]; if (sum newtarget) { right--; } else if (sum newtarget) { left; } else { ret.push_back({nums[i], nums[j], nums[left], nums[right--]}); // 去重 while (left right nums[left] nums[left - 1]) { left; } while (left right nums[right] nums[right 1]) { right--; } } } while (j n - 2 nums[j] nums[j 1]) { j; } } while (i n - 3 nums[i] nums[i 1]) { i; } } return ret; } };19、[中等] 删除链表的倒数第 N 个节点栈链表双指针计算链表的长度class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode* newhead new ListNode(0, head); int count 0; ListNode* cur head; while (cur) { cur cur-next; count; } int gap count - n; cur newhead; while (gap--) cur cur-next; ListNode* del cur-next; cur-next del-next; cur newhead-next; delete del; delete newhead; return cur; } };栈class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode* newhead new ListNode(0, head); stackListNode* s; ListNode* cur newhead; while (cur) { s.push(cur); cur cur-next; } for (int i 0; i n; i) { s.pop(); } ListNode* prev s.top(); ListNode* del prev-next; prev-next del-next; cur newhead-next; delete del; delete newhead; return cur; } };双指针class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode newhead; newhead.next head; ListNode* slow newhead; ListNode* fast newhead; while (n--) { fast fast-next; } while (fast-next) { slow slow-next; fast fast-next; } ListNode* del slow-next; slow-next del-next; delete del; return newhead.next; } };20、[简单] 有效的括号栈哈希表字符串class Solution { public: bool isValid(string s) { int n s.size(); stackchar st; for (int i 0; i n; i) { if (s[i] ( || s[i] { || s[i] [) st.push(s[i]); else if (s[i] ) !st.empty() st.top() () st.pop(); else if (s[i] } !st.empty() st.top() {) st.pop(); else if (s[i] ] !st.empty() st.top() [) st.pop(); else return false; } return st.empty() true; } };class Solution { public: bool isValid(string s) { int n s.size(); if (n % 2 1) return false; unordered_mapchar, char pairs {{), (}, {], [}, {}, {}}; stackchar stk; for (const char ch : s) { if (pairs.count(ch)) { if (stk.empty() || stk.top() ! pairs[ch]) return false; stk.pop(); } else stk.push(ch); } return stk.empty(); } };

相关新闻

AI原生应用领域:AI工作流的实战应用案例

AI原生应用领域:AI工作流的实战应用案例

AI原生应用领域:AI工作流的实战应用案例关键词:AI原生应用、AI工作流、大模型、实战案例、流程自动化摘要:本文从“AI原生应用”的核心定义出发,结合“AI工作流”的具体实现逻辑,通过4大真实行业案例(内容生…

2026/5/17 5:08:28 阅读更多 →
移动开发:使用 RxJava 实现响应式 UI

移动开发:使用 RxJava 实现响应式 UI

移动开发:使用 RxJava 实现响应式 UI 关键词:RxJava、响应式编程、移动开发、UI组件、事件处理、异步操作、数据流管理 摘要:本文深入探讨如何通过 RxJava 在移动开发中构建响应式 UI,涵盖核心概念、架构原理、算法实现、实战案例…

2026/7/3 5:48:32 阅读更多 →
AI原生应用中的数据伦理:收集、使用与保护的平衡术

AI原生应用中的数据伦理:收集、使用与保护的平衡术

AI原生应用中的数据伦理:收集、使用与保护的平衡术关键词:AI原生应用、数据伦理、隐私保护、数据收集、算法公平性摘要:当你的智能音箱“听懂”你深夜的咳嗽并推荐止咳药,当购物APP“比你更懂你”地推送商品,这些AI原生…

2026/5/17 5:08:26 阅读更多 →

最新新闻

AI Agent Skills开发实战:代码审查与CI/CD集成

AI Agent Skills开发实战:代码审查与CI/CD集成

1. 项目概述:AI Agent Skills在开发中的实战价值第一次在项目中引入Agent Skills时,我正面临着一个典型的技术困境:团队需要处理大量重复性代码审查工作,但人工检查既耗时又容易遗漏细节。当时偶然发现Anthropic开源的Agent Skill…

2026/7/5 11:25:23 阅读更多 →
Unlimited-OCR长文档解析:R-SWA机制原理与生产部署指南

Unlimited-OCR长文档解析:R-SWA机制原理与生产部署指南

🚀 30款热门AI模型一站整合,DeepSeek/GLM/Qwen 随心用,限时 5 折。 👉 点击领海量免费额度 如果你正在处理一份几十页的PDF报告、一本扫描版电子书,或者一份复杂的学术论文,想把它们转换成可编辑、可搜索…

2026/7/5 11:23:22 阅读更多 →
遗传算法优化BP神经网络:从理论到实践(附Python源码)

遗传算法优化BP神经网络:从理论到实践(附Python源码)

1. 为什么需要遗传算法优化BP神经网络?BP神经网络作为最基础的前馈神经网络,在函数拟合、分类预测等任务中表现优异。但我在实际项目中发现,传统BP算法存在两个致命缺陷:一是初始权值随机生成,训练结果不稳定&#xff…

2026/7/5 11:23:22 阅读更多 →
Python实现NLP中文文本自动摘要系统详解

Python实现NLP中文文本自动摘要系统详解

1. 项目概述这个NLP中文自动生成文本摘要系统是一个基于Python开发的完整解决方案,包含源码、详细技术报告和系统讲解。它能够自动处理中文文本,生成简洁准确的摘要内容,适用于新闻聚合、论文综述、商业报告等多种场景。系统采用先进的自然语…

2026/7/5 11:21:22 阅读更多 →
2026年MacBook Neo用户转向Windows笔记本:AI PC选购与迁移全指南

2026年MacBook Neo用户转向Windows笔记本:AI PC选购与迁移全指南

🚀 30款热门AI模型一站整合,DeepSeek/GLM/Qwen 随心用,限时 5 折。 👉 点击领海量免费额度 如果你正在考虑入手一台 MacBook Neo,或者已经习惯了苹果生态,但又被 Windows 阵营近两年在 AI、性能和生态上…

2026/7/5 11:21:22 阅读更多 →
Python 实现最优化 6 大经典算法:梯度下降、牛顿法与罚函数法实战对比

Python 实现最优化 6 大经典算法:梯度下降、牛顿法与罚函数法实战对比

Python 实现最优化 6 大经典算法:梯度下降、牛顿法与罚函数法实战对比在机器学习和工程优化领域,最优化算法扮演着至关重要的角色。本文将深入探讨六种经典优化算法的 Python 实现,并通过 Rosenbrock 函数这一经典测试案例,对比分…

2026/7/5 11:19:22 阅读更多 →

日新闻

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容 【免费下载链接】BiliTools A cross-platform bilibili toolbox. 跨平台哔哩哔哩工具箱,支持下载视频、番剧等等各类资源 项目地址: https://gitcode.com/GitHub_Trending/bilit/BiliTools …

2026/7/5 0:03:34 阅读更多 →
威胁模型全解析:从新手入门到实战应用,助你构建安全产品!

威胁模型全解析:从新手入门到实战应用,助你构建安全产品!

威胁模型的陌生现状在忙碌疲惫的一天里,参与了关于混合后量子密码学的讨论,应付端点攻击找茬的人,还参与留言板讨论后,发现“威胁模型”对多数人仍是陌生概念,且多被当作时髦用语。有趣的相关画作有一幅由 Embyr 创作的…

2026/7/5 0:03:34 阅读更多 →
渗透测试入门指南:从零基础到实战环境搭建

渗透测试入门指南:从零基础到实战环境搭建

1. 从“看热闹”到“入门”:我理解的渗透测试到底是什么?每次看到新闻里说某个大公司的数据被“黑”了,或者某个网站被攻击导致服务瘫痪,你是不是和我一样,心里会冒出两个念头:一是“这黑客真厉害”&#x…

2026/7/5 0:07:38 阅读更多 →

周新闻

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容 【免费下载链接】BiliTools A cross-platform bilibili toolbox. 跨平台哔哩哔哩工具箱,支持下载视频、番剧等等各类资源 项目地址: https://gitcode.com/GitHub_Trending/bilit/BiliTools …

2026/7/5 0:03:34 阅读更多 →
威胁模型全解析:从新手入门到实战应用,助你构建安全产品!

威胁模型全解析:从新手入门到实战应用,助你构建安全产品!

威胁模型的陌生现状在忙碌疲惫的一天里,参与了关于混合后量子密码学的讨论,应付端点攻击找茬的人,还参与留言板讨论后,发现“威胁模型”对多数人仍是陌生概念,且多被当作时髦用语。有趣的相关画作有一幅由 Embyr 创作的…

2026/7/5 0:03:34 阅读更多 →
渗透测试入门指南:从零基础到实战环境搭建

渗透测试入门指南:从零基础到实战环境搭建

1. 从“看热闹”到“入门”:我理解的渗透测试到底是什么?每次看到新闻里说某个大公司的数据被“黑”了,或者某个网站被攻击导致服务瘫痪,你是不是和我一样,心里会冒出两个念头:一是“这黑客真厉害”&#x…

2026/7/5 0:07:38 阅读更多 →

月新闻