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

网上的若干算法都太复杂了,现提出包氏算法如下:

  1. 先for循环把arr1中的元素入栈,并在每次遍历时,检索arr2中可以pop的元素。如果循环结束,而stack中还有元素,就说明arr2序列不是pop序列。

    static bool JudgeSequenceIsPossible(int[] arr1, int[] arr2)
    {
    Stack stack = new Stack();

    for (int i = 0, j = 0; i < arr1.Length; i++)
    {
    stack.Push(arr1[i]);

    while (stack.Count > 0 && (int)stack.Peek() == arr2[j])
    {
    stack.Pop();
    j++;
    }
    }

    return stack.Count == 0;
    }

    6.递归反转一个栈,要求不得重新申请一个同样的栈,空间复杂度o(1)

    算法思想:汉诺塔的思想,非常复杂,玩过九连环的人都想得通的

    static void ReverseStack(ref Stack stack) { if (stack.Count == 0) return; object top = stack.Pop(); ReverseStack(ref stack); if (stack.Count == 0) { stack.Push(top); return; } object top2 = stack.Pop(); ReverseStack(ref stack); stack.Push(top); ReverseStack(ref stack); stack.Push(top2); }

    7.给栈排个序

    本题目是上一题目的延伸

    static void Sort(ref Stack stack) { if (stack.Count == 0) return; object top = stack.Pop(); Sort(ref stack); if (stack.Count == 0) { stack.Push(top); return; } object top2 = stack.Pop(); if ((int)top > (int)top2) { stack.Push(top); Sort(ref stack); stack.Push(top2); } else { stack.Push(top2); Sort(ref stack); stack.Push(top); } }

    8..如何用一个数组实现两个栈

    继续我所提倡的抠门儿思想,也不枉我和青菜脸相交一场。

    网上流传着两种方法:

    方法1 采用交叉索引的方法

    一号栈所占数组索引为0, 2, 4, 6, 8......(K*2)
    二号栈所占数组索引为1,3,5,7,9 ......(K*2 + 1)

    算法实现如下:

    public class NewStack { object[] arr; int top1; int top2; public NewStack(int capticy) { arr = new object[capticy]; top1 = -1; top2 = -2; } public void Push(int type, object element) { if (type == 1) { if (top1 + 2 >= arr.Length) throw new Exception("The stack is full"); else { top1 += 2; arr[top1] = element; } } else //type==2 { if (top2 + 2 >= arr.Length) throw new Exception("The stack is full"); else { top2 += 2; arr[top2] = element; } } } public object Pop(int type) { object obj = null; if (type == 1) { if (top1 == -1) throw new Exception("The stack is empty"); else { obj = arr[top1]; arr[top1] = null; top1 -= 2; } } else //type == 2 { if (top2 == -2) throw new Exception("The stack is empty"); else { obj = arr[top2]; arr[top2] = null; top2 -= 2; } } return obj; } public object Peek(int type) { if (type == 1) { if (top1 == -1) throw new Exception("The stack is empty"); return arr[top1]; } else //type == 2 { if (top2 == -2) throw new Exception("The stack is empty"); return arr[top2]; } } }

    方法2:

    第一个栈A:从最左向右增长
    第二个栈B:从最右向左增长

    代码实现如下:

    public class NewStack { object[] arr; int top1; int top2; public NewStack(int capticy) { arr = new object[capticy]; top1 = 0; top2 = capticy; } public void Push(int type, object element) { if (top1 == top2) throw new Exception("The stack is full"); if (type == 1) { arr[top1] = element; top1++; } else //type==2 { top2--; arr[top2] = element; } } public object Pop(int type) { object obj = null; if (type == 1) { if (top1 == 0) throw new Exception("The stack is empty"); else { top1--; obj = arr[top1]; arr[top1] = null; } } else //type == 2 { if (top2 == arr.Length) throw new Exception("The stack is empty"); else
http://www.cnnetsun.cn/news/3175539.html

相关文章:

  • LangChain FewShotPromptTemplate少样本应用实战
  • 硬件版【Cursor】?aily blockly IDE尝鲜封神,实战硬伤尽显
  • 【Bug已解决】Claude Desktop 报错 Virtual Machine Platform not available 解决方案
  • 基于scRNA解析HNSCC肿瘤免疫微环境中Tfh、Th17细胞浸润的预后价值
  • 商用轨道插座怎么选更划算 各品牌性价比盘点帮你避坑少花冤枉钱
  • Windows Mobile下访问Sqlite的Native C++封装
  • Unity URP卡通渲染着色器:从原理到实践的完整指南
  • 3步掌握AMD Ryzen SDT调试工具:专业级CPU性能调优完整指南
  • NHibernate Issues之1904/1905:相同属性的Domain与Join查询/子查询
  • 智能办公本X2:端侧AI驱动的手写语音协同工作流
  • 大语言模型解码策略与低资源部署技术详解
  • NHibernate实例分享:Northwind Mapping
  • 2026年全铜卫浴五金洁具厂商口碑情况汇总
  • Vben精讲:06-Vben环境变量配置
  • MoeKoe Music终极指南:如何用开源免费客户端享受VIP音乐体验
  • Python自动化测试·Selenium操控元素的方法
  • 从Qwen-AgentWorld看大模型智能体如何操作真实系统:架构、挑战与工程实践
  • Cline 配置 Claude Sonnet 5 实战指南:思考深度调优与切换 Fable 5 的时机
  • 恶劣天气数据集 极端天气数据集 雨天道路数据集 雾天道路数据集 雪天马路恶劣环境图像目标检测数据集-道路障碍物识别数据集-数据集第10119期
  • 三千米浮空飞艇视频接入,广域立体视频孪生全域侦监技术解读 野外复杂地形动态重建 · 演训场景视频孪生目标三维重构完整体系
  • 毕业生必备7款AI写作辅助网站,一站式搞定选题初稿与降AI率
  • GHelper:华硕笔记本开源高效控制工具的专业替代方案
  • 成都茶台定制推荐
  • YOLOv10模型改进-Neck改进-第74篇:YOLOv10改进策略【Neck】| FPN-DCN可变形卷积
  • 连锁超市收银系统选什么?千店实测与商拓深度测评
  • 【湍流】基于matlab对涡粘性和雷诺应力模型FVM实现湍流通道流【含Matlab源码 15687期】含报告
  • 如何选择合适的面试机构?
  • 一文吃透 Transformers 模型加载:from_pretrained 参数大全与推理全流程解析
  • 磁吸易装 + 稳定传输,打造高效机房 U 位资产管理方案
  • 像素地理锚定标准化模型 Pixel2Geo引擎构建视频孪生空间基准体系 虚实双向指令交互通道 视频孪生联动前端感知设备智能处置技术详解