数据结构与算法之美
王争
前 Google 工程师
283752 人已学习
新⼈⾸单¥68
登录后,你可以任选4讲全文学习
课程目录
已完结/共 81 讲
基础篇 (38讲)
数据结构与算法之美
15
15
1.0x
00:00/00:00
登录|注册

春节7天练 | Day 2:栈、队列和递归

中文版:
英文版:
中文版:
英文版:
中文版:
英文版:
中文版:
英文版:
中文版:
英文版:
中文版:
英文版:
Climbing Stairs(爬楼梯)
Sliding Window Maximum(滑动窗口最大值)
Design Circular Deque(设计一个双端队列)
Evaluate Reverse Polish Notatio(逆波兰表达式求值)
Longest Valid Parentheses(最长有效的括号)
Valid Parentheses(有效的括号)
编程实现一组数据集合的全排列
编程实现求阶乘n!
编程实现斐波那契数列求值f(n)=f(n-1)+f(n-2)
实现一个循环队列
用链表实现一个链式队列
用数组实现一个顺序队列
编程模拟实现一个浏览器的前进、后退功能
用链表实现一个链式栈
用数组实现一个顺序栈
递归
队列
递归
队列
对应的LeetCode练习题(@Smallfly 整理)
关于栈、队列和递归的几个必知必会的代码实现
春节七天练第二篇,答题收获春节福利

该思维导图由 AI 生成,仅供参考

你好,我是王争。初二好!
为了帮你巩固所学,真正掌握数据结构和算法,我整理了数据结构和算法中,必知必会的 30 个代码实现,分 7 天发布出来,供你复习巩固所用。今天是第二篇。
和昨天一样,你可以花一点时间,来完成测验。测验完成后,你可以根据结果,回到相应章节,有针对性地进行复习。

关于栈、队列和递归的几个必知必会的代码实现

用数组实现一个顺序栈
用链表实现一个链式栈
编程模拟实现一个浏览器的前进、后退功能

队列

用数组实现一个顺序队列
用链表实现一个链式队列
实现一个循环队列

递归

编程实现斐波那契数列求值 f(n)=f(n-1)+f(n-2)
编程实现求阶乘 n!
编程实现一组数据集合的全排列

对应的 LeetCode 练习题(@Smallfly 整理)

Valid Parentheses(有效的括号)
Longest Valid Parentheses(最长有效的括号)
确认放弃笔记?
放弃后所记笔记将不保留。
新功能上线,你的历史笔记已初始化为私密笔记,是否一键批量公开?
批量公开的笔记不会为你同步至部落
公开
同步至部落
取消
完成
0/2000
荧光笔
直线
曲线
笔记
复制
AI
  • 深入了解
  • 翻译
    • 英语
    • 中文简体
    • 中文繁体
    • 法语
    • 德语
    • 日语
    • 韩语
    • 俄语
    • 西班牙语
    • 阿拉伯语
  • 解释
  • 总结

数据结构和算法是计算机科学中非常重要的基础知识,对于程序员来说,掌握这些知识是至关重要的。本文介绍了一系列关于栈、队列和递归的代码实现,以及对应的LeetCode练习题。作者王争整理了30个必知必会的代码实现,分7天发布,供读者复习巩固所用。本篇文章主要涵盖了栈、队列和递归的相关内容,包括用数组和链表实现栈和队列,模拟浏览器的前进、后退功能,实现循环队列,以及编程实现斐波那契数列求值、求阶乘和全排列等。此外,还提供了对应的LeetCode练习题,如有效的括号、逆波兰表达式求值、设计双端队列、滑动窗口最大值和爬楼梯等。通过这些练习,读者可以巩固所学知识,并应用到实际问题中。这篇文章为读者提供了一个系统的复习和练习的机会,有助于加深对数据结构和算法的理解和掌握。

仅可试看部分内容,如需阅读全部内容,请付费购买文章所属专栏
《数据结构与算法之美》
新⼈⾸单¥68
立即购买
登录 后留言

全部留言(58)

  • 最新
  • 精选
  • Abner
    java实现一个循环队列 代码如下: package queue; public class CircularQueue { private String[] data; private int size; private int head; private int tail; public CircularQueue(int capacity) { data = new String[capacity]; size = capacity; head = 0; tail = 0; } public boolean enqueue(String item) { if ((tail + 1) % size == head) { return false; } data[tail] = item; tail = (tail + 1) % size; return true; } public String dequeue() { if (head == tail) { return null; } String value = data[head]; head = (head + 1) % size; return value; } public void printAll() { if (0 == size) { return ; } for (int i = head;i % size != tail;i++) { System.out.print(data[i] + " "); } System.out.println(); } public static void main(String[] args) { CircularQueue circularQueue = new CircularQueue(5); circularQueue.enqueue("hello1"); circularQueue.enqueue("hello2"); circularQueue.enqueue("hello3"); circularQueue.enqueue("hello4"); circularQueue.dequeue(); circularQueue.printAll(); } }

    编辑回复: 感谢您参与春节七天练的活动,为了表彰你在活动中的优秀表现,赠送您99元专栏通用阅码,我们会在3个工作日之内完成礼品发放,如有问题请咨询小明同学,微信geektime002。

    2019-02-12
  • 神盾局闹别扭
    全排列实现: void Dopermute(char *pstr, char *pBegin) { if (*pBegin == '\0') printf("%s\n", pstr); for (char *pCur = pBegin; *pCur != '\0'; pCur++) { char temp = *pBegin; *pBegin = *pCur; *pCur = temp; Dopermute_v2(pstr, pBegin + 1); temp = *pBegin; *pBegin = *pCur; *pCur = temp; } } void Permute(char* pstr) { if (pstr == nullptr) return; Dopermute(pstr, pstr); }

    编辑回复: 感谢您参与春节七天练的活动,为了表彰你在活动中的优秀表现,赠送您99元专栏通用阅码,我们会在3个工作日之内完成礼品发放,如有问题请咨询小明同学,微信geektime002。

    2019-02-09
  • molybdenum
    老师新年好 这是我的作业 https://blog.csdn.net/github_38313296/article/details/86819684

    作者回复: 👍

    2019-02-09
  • 菜菜
    求斐波那契数列,当然最经典的算法就是递归,但是递归的效率非常低,因为中间过车会计算大量重复的子节点。在《剑指Offer》一书中,提到了一个自下而上计算的方法。我们知道f(0)=0,f(1)=1,再计算f(2),f(3)一直到f(n)。这样,时间复杂度就是O(n)。

    编辑回复: 感谢您参与春节七天练的活动,为了表彰你在活动中的优秀表现,赠送您10元无门槛优惠券,我们会在3个工作日之内完成礼品发放,如有问题请咨询小明同学,微信geektime002。

    2019-02-06
  • 李皮皮皮皮皮
    基础数据结构和算法是基石,灵活运用是解题的关键。栈,队列这些数据结构说到底就是给顺序表添加约束,更便于解决某一类问题。学习中培养算法的设计思想是非常关键的。而且思想是可以通用的。之前读《暗时间》一书,收获颇深。书中介绍之正推反推我在做程序题时竟出奇的好用。
    2019-02-05
    1
    11
  • Abner
    java用数组实现一个顺序栈 代码如下: package stack; public class ArrayStack { private String[] data; private int count; private int size; public ArrayStack(int n) { this.data = new String[n]; this.count = 0; this.size = n; } public boolean push(String value) { if (count == size) { return false; } else { data[count] = value; count++; return true; } } public String pop() { if (count == 0) { return null; } else { count--; return data[count]; } } }
    2019-02-11
    3
  • Abner
    java用递归实现斐波那契数列 代码如下: package recursion; public class Fib { public long calFib(long n) { if (n == 0 || n == 1) { return 1; } else { return calFib(n - 1) + calFib(n - 2); } } public static void main(String[] args) { Fib fib = new Fib(); long result = fib.calFib(5); System.out.println(result); } }
    2019-02-11
    2
  • Abner
    java用递归实现求解n! 代码如下: package recursion; public class Fac { public long calFac(long n) { if (n == 0) { return 1; } return calFac(n - 1) * n; } public static void main(String[] args) { Fac fac = new Fac(); long result = fac.calFac(10); System.out.println(result); } }
    2019-02-11
    2
  • kai
    1. 编程实现斐波那契数列求值 f(n)=f(n-1)+f(n-2) public class Fibonacci { public static int fib(int n) { if (n <= 0) { return 0; } if (n == 1) { return 1; } return fib(n-1) + fib(n-2); } } 2. Climbing Stairs(爬楼梯) public class ClimbStairs { public int climbFloor(int n) { if (n == 1 || n == 2) { return n; } return climbFloor(n - 1) + climbFloor(n - 2); } public int climbFloorIter(int n) { if (n == 1 || n == 2) { return n; } int jump1 = 1; int jump2 = 2; int jumpN = 0; for (int i = 3; i <= n; i++) { jumpN = jump1 + jump2; jump1 = jump2; jump2 = jumpN; } return jumpN; } } 3. Sliding Window Maximum(滑动窗口最大值) import java.util.ArrayList; import java.util.LinkedList; public class MaxNumOfSlidingWindow { public ArrayList<Integer> maxInWindows(int [] num, int size) { ArrayList<Integer> res = new ArrayList<>(); if (num == null || num.length <= 0 || size <= 0 || size > num.length) { return res; } LinkedList<Integer> qMax = new LinkedList<>(); // 双端队列:左端更新max,右端添加数据 int left = 0; for (int right = 0; right < num.length; right++) { // 更新右端数据 while (!qMax.isEmpty() && num[qMax.peekLast()] <= num[right]) { qMax.pollLast(); } qMax.addLast(right); // 更新max:如果max的索引不在窗口内,则更新 if (qMax.peekFirst() == right - size) { qMax.pollFirst(); } // 待窗口达到size,输出max if (right >= size-1) { res.add(num[qMax.peekFirst()]); left++; } } return res; } }
    2019-02-11
    2
  • Abner
    java用链表实现一个链式栈 代码如下: package stack; public class LinkedStack { private Node top = null; public static class Node { private String data; private Node next; public Node(String data, Node next) { this.data = data; this.next = next; } public String getData() { return data; } } public void push(String item) { Node newNode = new Node(item, null); if (top == null) { top = newNode; } else { newNode.next = top; top = newNode; } } public String pop() { if (top == null) { return null; } String value = top.data; top = top.next; return value; } public void printAll() { Node pNode = top; while (pNode != null) { System.out.print(pNode.data + " "); pNode = pNode.next; } System.out.println(); } public static void main(String[] args) { LinkedStack linkedStack = new LinkedStack(); linkedStack.push("haha"); linkedStack.push("nihao"); linkedStack.printAll(); } }
    2019-02-12
    1
收起评论
大纲
固定大纲
关于栈、队列和递归的几个必知必会的代码实现
队列
递归
对应的 LeetCode 练习题(@Smallfly 整理)
显示
设置
留言
58
收藏
沉浸
阅读
分享
手机端
快捷键
回顶部