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

网站建设的语言与工具seo自动发布外链工具

网站建设的语言与工具,seo自动发布外链工具,深圳中装建设集团,wordpress.重装一、栈的核心算法与应用场景 栈的先进后出特性使其在以下算法中表现优异: 括号匹配:校验表达式合法性。表达式求值:中缀转后缀,逆波兰表达式求值。深度优先搜索(DFS):模拟递归调用。单调栈&am…

一、栈的核心算法与应用场景

栈的先进后出特性使其在以下算法中表现优异:

  1. 括号匹配:校验表达式合法性。
  2. 表达式求值:中缀转后缀,逆波兰表达式求值。
  3. 深度优先搜索(DFS):模拟递归调用。
  4. 单调栈:解决区间最值问题。
  5. 函数调用栈:模拟程序执行流程。

二、括号匹配算法

1. 问题描述

给定一个包含()[]{}的字符串,判断其是否合法。

2. 实现代码
#include <stack>
#include <string>
#include <unordered_map>bool isValidParentheses(const std::string& s) {std::stack<char> stack;std::unordered_map<char, char> mapping = {{')', '('},{']', '['},{'}', '{'}};for (char ch : s) {if (mapping.count(ch)) { // 右括号if (stack.empty() || stack.top() != mapping[ch]) {return false;}stack.pop();} else { // 左括号stack.push(ch);}}return stack.empty();
}
3. 关键点
  • 使用哈希表存储括号映射关系。
  • 栈为空时遇到右括号直接返回false
  • 最终栈为空才表示匹配成功。

三、表达式求值算法

1. 中缀转后缀(逆波兰表达式)
#include <stack>
#include <string>
#include <vector>
#include <cctype>std::vector<std::string> infixToPostfix(const std::string& expr) {std::vector<std::string> output;std::stack<char> operators;std::unordered_map<char, int> precedence = {{'+', 1}, {'-', 1},{'*', 2}, {'/', 2},{'^', 3}};for (size_t i = 0; i < expr.size(); ++i) {char ch = expr[i];if (isdigit(ch)) { // 数字直接输出std::string num;while (i < expr.size() && isdigit(expr[i])) {num += expr[i++];}output.push_back(num);--i;} else if (ch == '(') { // 左括号入栈operators.push(ch);} else if (ch == ')') { // 右括号弹出至左括号while (!operators.empty() && operators.top() != '(') {output.push_back(std::string(1, operators.top()));operators.pop();}operators.pop(); // 弹出左括号} else { // 运算符while (!operators.empty() && precedence[ch] <= precedence[operators.top()]) {output.push_back(std::string(1, operators.top()));operators.pop();}operators.push(ch);}}// 弹出剩余运算符while (!operators.empty()) {output.push_back(std::string(1, operators.top()));operators.pop();}return output;
}
2. 逆波兰表达式求值
#include <stack>
#include <vector>
#include <string>int evalRPN(const std::vector<std::string>& tokens) {std::stack<int> stack;for (const std::string& token : tokens) {if (token == "+" || token == "-" || token == "*" || token == "/") {int b = stack.top(); stack.pop();int a = stack.top(); stack.pop();if (token == "+") stack.push(a + b);else if (token == "-") stack.push(a - b);else if (token == "*") stack.push(a * b);else stack.push(a / b);} else {stack.push(std::stoi(token));}}return stack.top();
}

四、单调栈算法

1. 问题描述

给定一个数组,找到每个元素的下一个更大元素(Next Greater Element)。

2. 实现代码
#include <stack>
#include <vector>std::vector<int> nextGreaterElements(const std::vector<int>& nums) {std::vector<int> result(nums.size(), -1);std::stack<int> stack;for (int i = 0; i < nums.size(); ++i) {while (!stack.empty() && nums[stack.top()] < nums[i]) {result[stack.top()] = nums[i];stack.pop();}stack.push(i);}return result;
}
3. 关键点
  • 栈中存储数组下标,便于更新结果。
  • 时间复杂度为O(n),空间复杂度为O(n)。

五、深度优先搜索(DFS)与栈

1. 递归DFS
void dfsRecursive(Node* node) {if (!node) return;// 处理当前节点for (auto child : node->children) {dfsRecursive(child);}
}
2. 迭代DFS(使用栈)
void dfsIterative(Node* root) {if (!root) return;std::stack<Node*> stack;stack.push(root);while (!stack.empty()) {Node* curr = stack.top();stack.pop();// 处理当前节点for (auto it = curr->children.rbegin(); it != curr->children.rend(); ++it) {stack.push(*it); // 子节点逆序压栈}}
}

六、函数调用栈模拟

1. 问题描述

模拟函数调用栈的行为,实现一个简单的解释器。

2. 实现代码
#include <stack>
#include <string>
#include <iostream>void executeFunction(const std::string& name) {std::cout << "Entering function: " << name << std::endl;// 模拟函数执行std::cout << "Exiting function: " << name << std::endl;
}void simulateCallStack() {std::stack<std::string> callStack;callStack.push("main");executeFunction(callStack.top());callStack.push("func1");executeFunction(callStack.top());callStack.push("func2");executeFunction(callStack.top());while (!callStack.empty()) {callStack.pop();if (!callStack.empty()) {std::cout << "Returning to function: " << callStack.top() << std::endl;}}
}

七、总结

栈作为一种基础数据结构,在算法设计中具有广泛的应用。通过深入理解栈的特性和应用场景,可以高效解决括号匹配、表达式求值、单调栈、DFS等问题。同时,栈在系统级编程(如调用栈)中也扮演着重要角色。掌握栈的实现和应用,是提升算法能力和编程水平的关键。

http://www.dtcms.com/wzjs/232295.html

相关文章:

  • 网站设计的公司怎么样免费建站的网站
  • 域名备案步骤长沙靠谱seo优化价格
  • 公司企业网站建设目的推广小程序
  • 如何借用别人网站做模板镇江网站关键字优化
  • 黑河建设网站百度手机助手免费下载
  • ui做网站流程营销培训机构哪家最专业
  • 西安建筑网站建设学大教育培训机构电话
  • 网站做的好不好看什么网站优化的关键词
  • 合肥专业做网站的公司有哪些新闻头条今日新闻
  • 个人做盈利网站免费建站工具
  • 在社保网站做调动seo网站分析报告
  • 一个完整的工程项目流程优化设计电子版在哪找
  • 江苏网站建设机构搜索引擎优化技术都有哪些
  • 网站设计需要会什么优化大师的优化项目有哪7个
  • 各种广告图片大全青岛seo网站推广
  • 网站建设好的公司关键词seo排名优化如何
  • 企业网站管理系统怎么修改密码成都企业seo
  • 网站建设类的论文题目网站排名顾问
  • 政府网站集约化建设专题代运营网店公司
  • 在外汇局网站做登记报告每天三分钟新闻天下事
  • behance是什么网站百度人工在线客服
  • 关掉自己做的网站央视新闻最新消息今天
  • 品牌网站建设 t磐石网络网络舆情分析报告模板
  • 湖滨区建设局网站新区seo整站优化公司
  • 腾讯云网站搭建深圳搜狗seo
  • 建设一个同城购物网站快速排名服务平台
  • 网站架构优化开源crm系统
  • 网站开发的五个阶段百度一下你就知道了
  • 贵州网站优化网络优化工程师为什么都说坑人
  • 专业做蛋糕视频网站百度爱采购优化软件