news 2026/7/6 4:05:26

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
网上的若干算法都太复杂了,现提出包氏算法如下:
  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
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/6 4:03:20

LangChain FewShotPromptTemplate少样本应用实战

里有个容易踩的坑&#xff1a;创建 FewShotPromptTemplate 的时候&#xff0c;examples 和 example_selector 这两个参数是互斥的&#xff0c;必须填其中一个&#xff0c;不然代码直接报错。绝大多数情况下&#xff0c;我们直接用 examples 参数把准备好的示例数据传进去就行。…

作者头像 李华
网站建设 2026/7/6 4:03:04

硬件版【Cursor】?aily blockly IDE尝鲜封神,实战硬伤尽显

步骤 1&#xff1a;安装启动下载好安装包&#xff0c;一路 “下一步”&#xff0c;几分钟就装好了。打开 IDE 的瞬间&#xff0c;我直接被清爽的界面戳中了&#xff1a;左边是全平台开发板列表&#xff0c;Arduino、ESP32、STM32、树莓派 Pico… 主流板子基本全覆盖。中间是图形…

作者头像 李华
网站建设 2026/7/6 3:53:43

商用轨道插座怎么选更划算 各品牌性价比盘点帮你避坑少花冤枉钱

开过咖啡店、装过联合办公、做过商业展厅的朋友都懂&#xff0c;配电布局绝对是装修前期最容易踩的坑&#xff1a;插座布少了&#xff0c;后期加设备要拖插排乱不说&#xff0c;还容易过载跳闸&#xff1b;布多了&#xff0c;闲置的插座丑还浪费钱&#xff0c;换个业态还要砸墙…

作者头像 李华
网站建设 2026/7/6 3:49:20

Windows Mobile下访问Sqlite的Native C++封装

qlite几乎成立移动设备开发领域数据存储方面的事实标准。Sqlite已经广泛被使用到Andriod&#xff0c;iPhone&#xff0c;WebOS以及Symbian等平台了&#xff0c;本文讲述在Windows Mobile平台下如何使用Native C访问Sqlite&#xff0c;同时讲述一个封装类的实现和使用。 Sqlite源…

作者头像 李华