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

深圳住房和建设管理局官方网站深圳网站制作价格

深圳住房和建设管理局官方网站,深圳网站制作价格,有经验的南昌网站建设,羽毛球赛事视频一、栈的核心算法与应用场景 栈的先进后出特性使其在以下算法中表现优异: 括号匹配:校验表达式合法性。表达式求值:中缀转后缀,逆波兰表达式求值。深度优先搜索(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/590522.html

相关文章:

  • 网站设计师 要求中国企业500强榜单发布
  • 个人优秀网站网站建设:上海珍岛
  • 旅游网站建设模块威胁网站检测平台建设
  • 铜仁公司做网站南联网站建设推广
  • 能够做数据地图的网站怎么做淘宝客网站赚钱吗
  • 品牌宣传网站制作如何给异地网站做镜像
  • 自己做网站难网站开发与软件开发区别
  • 企业网站必须实名认证现在网站的外部链接怎么做
  • 建设外贸类网站西宁城西区建设局网站
  • 用jsp做一网站的流程图朝阳公共资源交易中心
  • 专业网站建设是哪家便宜wordpress文章字体大小
  • 网站建设对企业的好处wordpress 模板引入文件
  • 阿里云网站 模板建设wordpress网站分享到朋友圈
  • 如何在linux服务器上架设网站专业郑州网站建设
  • 网站描文本链接怎么做网站进度条做多大
  • 宁波网站建设优化ip反查域名网站
  • 有什么网站是做企业型的上海代理注册公司
  • 东莞网站优化是什么企业网站 建设策划书
  • 广州免费建站排行视频拍摄合同
  • 亚马逊站外推广网站百度蜘蛛
  • 微信公众号模板素材网站网络推广工作
  • 宝山区建设用地事务所网站南京溧水城市建设集团网站
  • 做网站预付款是多少绍兴做网站的公司
  • 小程序开发文档说明优化师证书
  • 美团做团购网站舆情网站
  • 二手网站建设青岛网页设计公司
  • 威海 网站开发有没有专门做教育培训的网站
  • 包头网站建设项目建设目标怎么写
  • 商务网站开发技术15个国内互动网站设计欣赏
  • 小米商城网站设计论文做网站插背景图片如何变大