算法基础三之链表、栈、队列、递归

栈队列算法与递归问题解析 本文涵盖多个与数据结构和算法相关的问题,包括使用栈和队列检测回文、阶乘递归的栈空间分析、汉诺塔移动步骤、递归定义(如降序列表、最大值、回文串、最大公约数)、链表操作、堆和哈希表的时间复杂度分析,以及深度优先搜索的非递归实现。 阅读详情

链表

单向链表

单向链表节点结构(可以实现成泛型)

public class Node<T> {
    public T value;
    public Node next;

    public Node(T value) {
        this.value = value;
    }
}

双向链表

public class DoubleNode {
    public int value;
    public DoubleNode pre;
    public DoubleNode next;

    public DoubleNode(int value) {
        this.value = value;
    }
}

单向链表和双向链表最简单的练习

链表相关的问题几乎都是coding问题

1)单链表和双链表怎么反转

package com.zh.class002;

public class ReverseList {
    public static void main(String[] args) {
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(3);
        Node node = reverseLinkedList(head);
        System.out.println(node.value);
        System.out.println(node.next.value);
        System.out.println(node.next.next.value);
    }
    public static Node reverseLinkedList(Node head) {
        Node pre = null;
        Node next = null;

        while (head != null) {
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }

    public static DoubleNode reverseDoubleLinkedList(DoubleNode head) {
        DoubleNode pre = null;
        DoubleNode next = null;

        while (head != null) {
            next = head.next;

            head.next = pre;
            head.pre = next;

            pre = head;
            head = next;
        }
        return pre;
    }
}

 

2)把给定值都删除

package com.zh.class002;

public class DeleteGivenValue {
    public static void main(String[] args) {
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(2);
        head.next.next.next = new Node(3);
        Node node = removeValue(head, 2);

        System.out.println(node);
    }
    public static Node removeValue(Node<Integer> head, int num) {
        // head来到第一个不需要删的位置
        while (head != null) {
            if (head.value == num) {
                head = head.next;
            } else {
                break;
            }
        }

        Node<Integer> pre = head;
        Node<Integer> cur = head;
        while (cur != null) {
            if (cur.value == num) {
                pre.next = cur.next;
            } else {
                pre = cur;
            }
            cur = cur.next;
        }
        return head;
    }
}

栈和队列

逻辑概念

栈:数据先进后出,犹如弹匣

队列:数据先进先出,好似队列

栈和队列的实际实现:

package com.zh.class002.stackandqueue;

import com.zh.class002.DoubleNode;

public class DoubleEndsQueue<T> {
    DoubleNode<T> head = null;
    DoubleNode<T> tail = null;
    public void addFromHead(T value) {
        DoubleNode<T> cur = new DoubleNode<>(value);
        if (head == null) {
            head = cur;
            tail = cur;
        } else {
            cur.next = head;
            head.pre = cur;
            head = cur;
        }
    }
    public void addFromBottom(T value) {
        DoubleNode<T> cur = new DoubleNode<>(value);
        if (head == null) {
            head = cur;
            tail = cur;
        } else {
            tail.next = cur;
            cur.pre = tail;
            tail = cur;
        }
    }

    public T popFromHead() {
        if (head == null) {
            return null;
        }
        DoubleNode<T> cur = head;
        if (head == tail) {
            head = null;
            tail = null;
        } else {
            head = head.next;
            cur.next = null;
            head.pre = null;
        }
        return cur.value;
    }

    public T popFromBottom() {
        if (head == null) {
            return null;
        }
        DoubleNode<T> cur = tail;
        if (head == tail) {
            head = null;
            tail = null;
        } else {
            tail = tail.pre;
            tail.next = null;
            cur.pre = null;
        }
        return cur.value;
    }

    public boolean isEmpty() {
        return head == null;
    }
}

双向链表实现

package com.zh.class002.stackandqueue;

public class MyStack<T> {
    private DoubleEndsQueue<T> queue;

    public MyStack() {
        queue = new DoubleEndsQueue();
    }

    public void push(T value) {
        queue.addFromHead(value);
    }

    public T pop() {
        return queue.popFromHead();
    }

    public boolean isEmpty() {
        return queue.isEmpty();
    }
}

 

package com.zh.class002.stackandqueue;

public class MyQueue<T> {
    private DoubleEndsQueue<T> queue;

    public MyQueue() {
        queue = new DoubleEndsQueue<T>();
    }

    public void push(T value) {
        queue.addFromHead(value);
    }

    public T poll() {
        return queue.popFromBottom();
    }

    public boolean isEmpty() {
        return queue.isEmpty();
    }
}

 

数组实现

package com.zh.class002.stackandqueue;

public class MyArrayStack {
    public static void main(String[] args) {
        MyArrayStack myArrayStack = new MyArrayStack(3);
        myArrayStack.push(1);
        myArrayStack.push(2);
        myArrayStack.push(3);
        System.out.println(myArrayStack.pop());
        myArrayStack.push(4);
        System.out.println(myArrayStack.pop());

    }
    private int[] arr;
    int index = 0;
    private final int limit;

    public MyArrayStack(int limit) {
        arr = new int[limit];
        this.limit = limit;
    }

    public void push(int value) {
        if (index == limit) {
            throw new RuntimeException("栈满了,不能再加了");
        }
        arr[index ++] = value;
    }

    public int pop() {
        if (index == 0) {
            throw new RuntimeException("栈空了,不能再拿了");
        }
        return arr[--index];
    }

    public boolean isEmpty() {
        return index == 0;
    }
}

 

package com.zh.class002.stackandqueue;

public class MyArrayQueue {
    private int[] arr;
    private int pushi;
    private int polli;
    private int size;
    private final int limit;

    public MyArrayQueue(int limit) {
        arr = new int[limit];
        pushi = 0;
        polli = 0;
        size = 0;
        this.limit = limit;
    }

    public void push(int value) {
        if (size == limit) {
            throw new RuntimeException("栈满了,不能再加了");
        }
        size++;
        arr[pushi] = value;
        pushi = nextIndex(pushi);
    }

    public int pop() {
        if (size == 0) {
            throw new RuntimeException("栈空了,不能再拿了");
        }
        size--;
        int ans = arr[polli];
        polli = nextIndex(polli);
        return ans;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    // 如果现在的下标是i,返回下一个位置
    private int nextIndex(int i) {
        return i < limit - 1 ? i + 1 : 0;
    }
}

栈和队列的常见面试题

实现一个特殊的栈,在基本功能的基础上,再实现返回栈中最小元素的功能。

1)pop、push、getMin操作的时间复杂度都是O(1)。

2)设计的栈类型可以使用现成的栈结构。

实现一:双栈实现,一个栈存数据,另一个栈存最小值

package com.zh.class002;

import java.util.Stack;

public class GetMinStack {
    public static void main(String[] args) {
        GetMinStack getMinStack = new GetMinStack();
        getMinStack.push(1);
        getMinStack.push(2);
        getMinStack.push(3);
        System.out.println(getMinStack.getmin());
        getMinStack.pop();
        System.out.println(getMinStack.getmin());
        getMinStack.pop();
        System.out.println(getMinStack.getmin());
    }
    Stack<Integer> dataStack;
    Stack<Integer> minStack;

    public GetMinStack() {
        this.dataStack = new Stack<>();
        this.minStack = new Stack<>();
    }

    public void push(int newNum) {
        if (minStack.isEmpty()) {
            minStack.push(newNum);
        } else if (newNum < minStack.peek()) {
            minStack.push(newNum);
        } else {
            int newMin = minStack.peek();
            minStack.push(newMin);
        }
        dataStack.push(newNum);
    }

    public int pop() {
        if (this.dataStack.isEmpty()) {
            throw new RuntimeException("Your stack is empty.");
        }
        this.minStack.pop();
        return this.dataStack.pop();
    }

    public int getmin() {
        if (this.minStack.isEmpty()) {
            throw new RuntimeException("Your stack is empty.");
        }
        return this.minStack.peek();
    }
}

面试题二:

1)如何用栈结构实现队列结构

2)如何用队列结构是实现栈结构

栈实现队列

package com.zh.class002;

import java.util.Stack;

public class TwoStacksImplementQueue {
    public static void main(String[] args) {
        TwoStacksImplementQueue twoStacksImplementQueue = new TwoStacksImplementQueue();
        twoStacksImplementQueue.add(1);
        twoStacksImplementQueue.add(2);
        twoStacksImplementQueue.add(3);
        System.out.println(twoStacksImplementQueue.poll());
        System.out.println(twoStacksImplementQueue.poll());
        twoStacksImplementQueue.add(5);
        System.out.println(twoStacksImplementQueue.poll());
        System.out.println(twoStacksImplementQueue.poll());

    }
    Stack<Integer> stackPush;
    Stack<Integer> stackPop;

    public TwoStacksImplementQueue() {
        this.stackPush = new Stack<>();
        this.stackPop = new Stack<>();
    }

    public void add(int pushInt) {
        stackPush.push(pushInt);
        pushToPop();
    }

    private void pushToPop() {
        if (stackPop.isEmpty()) {
            while (!stackPush.isEmpty()) {
                stackPop.push(stackPush.pop());
            }
        }
    }

    public int poll() {
        if (stackPop.isEmpty() && stackPush.isEmpty()) {
            throw new RuntimeException("Queue is empty!");
        }
        pushToPop();
        return stackPop.pop();
    }

    public int peek() {
        if (stackPop.isEmpty() && stackPush.isEmpty()) {
            throw new RuntimeException("Queue is empty!");
        }
        pushToPop();
        return stackPop.peek();
    }
}

队列实现栈

package com.zh.class002;

import java.util.LinkedList;
import java.util.Queue;

public class TwoQueueImplementStack<T> {
    public Queue<T> queue;
    public Queue<T> help;

    public TwoQueueImplementStack() {
        queue = new LinkedList<>();
        help = new LinkedList<>();
    }

    public void push(T value) {
        queue.offer(value);
    }

    public T poll() {
        while (queue.size() > 1) {
            help.offer(queue.poll());
        }
        T ans = queue.poll();
        Queue<T> tmp = queue;
        queue = help;
        help = tmp;
        return ans;
    }

    public T peek() {
        while (queue.size() > 1) {
            help.offer(queue.poll());
        }
        T ans = queue.poll();
        help.offer(ans);
        Queue<T> tmp = queue;
        queue = help;
        help = tmp;
        return ans;
    }

    public boolean isEmpty() {
        return queue.isEmpty();
    }
}

递归:

例子:

求数组arr[L...R]中的最大值,怎么用递归方法实现

1)将[L...R]范围分成左右两半。左:[L...Mid],右[Mid+1...R]

2)左部分求最大值,右部分求最大值

3)[L...R]范围上的最大值,是max{左部分最大值,右部分最大值}

注意:2)是个递归过程,当范围上只有一个数,就可以不用再递归了

package com.zh.class002;

public class GetMax {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9,10};
        System.out.println(GetMax.getMax(arr));
    }
    public static int getMax(int[] arr) {
        return process(arr, 0, arr.length - 1);
    }

    public static int process(int[] arr, int l, int r) {
        if (l == r) {
            return arr[l];
        }
        int mid = l + (r - l) / 2;
        int leftMax = process(arr, l, mid);
        int rightMax = process(arr, mid + 1, r);
        return Math.max(leftMax, rightMax);
    }
}

对于符合下面公式的递归,时间复杂度是可以确认的

T(N)=aT(\frac{N}{b}) + O(N^{d})

其中a,b,d都是常数

对于上面的递归,假设L-R上有N个数

子问题的规模是N/2,调用了两次

因此a=2(调用次数)

b=2(子问题规模)

除此外,其他的复杂度都是常数次,因此d=0

结果如下:

 

上面复杂度确认a = 2, b = 2, d=0;因此复杂度为第一种,复杂度为O(N)

 

 

 

 

 

 

 

 

 

左神算法课笔记(二):链表、栈和队列、递归Master公式、哈希表、有序表 单向链表 双向链表 单链表、双链表最简单的面试题 1、单链表和双链表如何反转 package class02; import java.util.ArrayList; public class Code01_ReverseList { public static class Node { public int value; public Node next; public Node(int data) { value = data; } } public stat 阅读详情

相关推荐

手把手搭建你的第一个OCT仿真模型:用Python和光学仿真库重现A-SCAN信号

本文详细介绍了如何使用Python和光学仿真库搭建OCT仿真模型,重现A-SCAN信号。从环境配置、干涉仪建模到信号处理全流程,手把手教你实现光学相干层析成像的核心技术。通过代码示例和性能优化技巧,帮助开发者深入理解OCT原理并应用于医疗诊断等领域。

weixin_42562079的博客 241

算法与数据结构复习 第三章 栈与队列(详解)

文章目录第三章 栈与队列书面作业一、判断题二、单选题三、程序填空四、函数题 第三章 栈与队列 书面作业 一、判断题 1、在对不带头结点的链队列作出队操作时,不会改变头指针的值。 (F) 2、循环队列执行出队操作时会引起大量元素的移动。 (F) 3、不论是入队列操作还是入栈操作,在顺序存储结构上都需要考虑"溢出"情况。 (T) 4、“Circular Queue” is defined to be ...

好学者的博客 1万+

lstm如何输入多维序列_深度学习也可以做时间序列异常诊断

最近在看的一篇论文里,用到了GRU、VAE等技术来做多维时间序列异常诊断。那么今天首先来聊一聊深度学习的基础知识,然后简单说一下论文。以下基础知识部分的内容,主要来源于李宏毅老师的课程。有兴趣的可以去看李老师的课程哦https://www.bilibili.com/video/BV1JE411g7XF?p=1 1、GRU(Gate Recurrent Unit):GRU是循环神经网络(Recurr...

weixin_30899789的博客 2833

《Data Structure And Algorithm Analysis In C++》读书笔记三

Chapter 3 Lists, Stacks, and Queues Topics: *Introduce the concept of Abstract Data Types(ADTs). *Show how to efficiently perform operations on lists. *Introduce the stack ADT and its use in im...

sesiria的博客 1221

C语言编写Picard迭代和牛顿迭代法

为了完成学校计算方法的作业,花了两个多星期将所有实验报告编写出来,看到同桌找代码的辛苦,优快云也没有这类代码,我只是一时兴起,想分享我写的代码与大家共飨,有错误还请多多指正,代码都不难,也是为了让优快云帮我保存下,我辛辛苦苦做出来的,倘若能给别人带来一丝一毫的帮助,我都万分荣幸。 1:什么是Picard迭代 先弄一个满足f(x) = 0的函数将其转化为x= 正在上传… 重新上传 取消 ...

mohenxiang的博客 4699

最强AI插件Augment,适配idea、vscode,对标cursor!

Augment是一款新兴的AI辅助编码工具,能够无缝集成到IDEA和VS Code等主流IDE中,直接对标专业AI编码工具Cursor。其核心优势包括多IDE适配、无缝集成、代码理解与生成以及对话式编程体验。Augment通过插件形式保留原IDE功能和快捷键习惯,降低学习成本,同时提供深度理解项目上下文和生成高质量代码的能力。此外,Augment还具备审查变更、检查点、代理与代理自动等核心功能,支持从简单配置更改到复杂功能实现的各种开发任务。Augment代表了AI辅助编程工具的新方向,通过增强开发者熟悉的

todoitbo的博客 1万+

数据结构与算法(线性结构篇)-队列

一 队列的应用场景 二 数组模拟队列 三 数组模拟环形队列分析 四 数组模拟环形队列实现

qq_43671749的博客 438

15、SQL Server查询优化与CPU负载管理

本文深入探讨了SQL Server查询优化与CPU负载管理的关键技术,包括强制连接类型的使用、键查找问题的解决、索引设计的最佳实践、高CPU负载的排查与优化,以及并行性的利弊与调整策略。通过优化查询逻辑、合理设计索引、监控与管理CPU资源,可以显著提升SQL Server的性能和稳定性。适用于数据库开发人员和DBA进行性能调优参考。

pz8901234的博客 77

单链表反转及取中间值

定义一个单链表 public class ListNode { public Integer value; public ListNode next; public ListNode(Integer value) { this.value = value; } }

远方的少年 190

Hermes Agent 常见报错排查与问题解决

Hermes Agent 常见报错排查指南 摘要 本文全面梳理了 Hermes Agent 使用过程中的10类常见报错,按阶段划分为安装期、运行期认证类、后端基础设施类和长任务执行类四大类,并提供了系统化的排查流程: 安装期问题:包括命令未找到和Python版本不兼容,主要检查PATH环境和Python版本是否符合要求 认证类问题:涉及API Key认证失败和速率限制,需要验证密钥配置和配额状态 基础设施问题:包含Docker报错、MCP连接失败和OAuth令牌过期,需检查相关服务状态 执行类问题:主要是上

简简单单Onlinezuozuo 1345

安装西门子TIA Portal V15.1提示先决条件不足如何处理?

安装TIA Portal STEP 7 Professional WinCC Professional V15.1提示先决条件不足,需要如何才能够满足安装条件呢? 1.硬件要求: 安装 STEP 7 Basic / Professional V15.1 的计算机必须至少满足以下需求: 处理器: Core i5-6440EQ 3.4 GHz 或者相当 内存: 16GB或者更多(对于大型项目,为32GB) 硬盘:SSD,配备至少50GB的存储空间 图形分辨率:最小 1920 x 1080 显示器:15.6"

Robot_PLC_HMI_Automation 2万+

2024年算法基础三之链表、栈、队列、递归(1),由浅入深

其他的内容都可以按照路线图里面整理出来的知识点逐一去熟悉,学习,消化,不建议你去看书学习,最好是多看一些视频,把不懂地方反复看,学习了一节视频内容第二天一定要去复习,并总结成思维导图,形成树状知识网络结构,方便日后复习。这里还有一份很不错的《Java基础核心总结笔记》,特意跟大家分享出来目录:部分内容截图:本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录需要这份系统化的资料的朋友,可以点击这里获取} else {

pckzzy119的博客 296

yolo 图像检测、关键点检测单摄像头测量物体尺寸长度大小

new_distance/real_size (待求物体像素距离与实际距离比值)= distance / metric_length (参照物像素距离与实际距离比值)然后运行size_demo进行要测量的物体进行关节点检测距离;metric_length是上面耳机选取的两个点的实际物理距离,大概6cm;这里用的windows。通过关键点检测识别物体关键点的像素距离,然后参考比例尺计算实际距离。2)关键点算法这里用的现有的进行测试,实际场景需要自己训练。这里拿这模型推理出的关键点坐标进行后续长度计算。

weixin_42357472的博客 2525

算法修炼入门指南:从数据结构到基础算法的二十二层体系构建

数据结构与算法是计算机科学的核心基础,它们共同构成了高效解决复杂问题的理论框架。数据结构如数组、链表、栈、队列和哈希表,负责数据的组织与存储,而算法思想如递归、分治、排序和查找,则定义了处理数据的逻辑步骤。掌握这些基础能显著提升代码效率与系统性能,是进行系统设计、优化和通过技术面试的基石。在实际开发中,无论是构建高并发服务、实现快速检索,还是优化数据处理流程,都离不开扎实的算法内功。本文以“练气二十二层”为隐喻,系统梳理了从变量、流程控制到二叉树、堆等核心内容的渐进式学习路径,并融入递归、动态规划等高频技术

weixin_30621711的博客 680

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

21天攻克剑指Offer:从零基础到算法高手的终极指南

还在为算法面试感到焦虑吗?面对海量的算法题目不知从何下手?LeetCode-Book项目为你提供了一套完整的剑指Offer刷题解决方案!这个开源项目包含了《剑指Offer》、《图解算法数据结构》、《Krahets笔面试精选88题》三大核心资源的Python、Java、C++解题代码,帮助你系统化掌握算法面试必备技能。 ## 📊 为什么选择LeetCode-Book? 在当今竞争激烈的技术面试

gitblog_00090的博客 347

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

数据结构与算法之美总结(数组、链表、栈、队列、递归、排序及二分)

数据结构与算法之美总结(数组、链表、栈、队列、递归、排序及二分)

凡的博客 1万+

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

栈、队列、链表

队列(queue)是一种在一端进行插入,另一端进行删除的受限的线性表。队列是一种先进先出(First In First Out)的线性表,允许进入的一端成为队尾(rear),允许删除的一端称为队头(front)。2、队列常见基本操作创:InitQueue(&Q)判空:IsEmpty(Q)增:EnQueue(&Q,p)删: DeQueue(&Q,p)查:GetHead(&Q,x)由于队列是受限的线性表,并不是所有针对线性表的操作都合适。像是从队列中读取某个中间的元素。

m0_71266194的博客 1578

常见数据结构和算法实现(排序/查找/数组/链表/栈/队列/树/递归/海量数据处理/图/位图/Java版数据结构)

数据结构和算法 入门《大话数据结构 程杰》《算法图解》《数据结构与算法分析:Java语言描述》(大学课本 伪代码)《剑指offer》《编程珠玑》(对大数据量处理的算法)《编程之美》(超级难)《算法导论》(很厚很无聊)《算法第四版》(推荐 本书没有动态规划) LeetCode 王争google 《算法帝国》《数学之美》《算法之美》(闲暇阅读) https://github.com/wan...

Jet_qi的博客 8853

郝斌——数据结构笔记(数组、链表、栈、队列)(递归)

一、预备知识 使用教材:严蔚敏,吴伟民编写的《数据结构》 但是书中的算法都是伪算法(不是程序),都是解题的思路 具体的程序由高一凡主编的书里面有。 黄国瑜写的数据结构也可以。 模块一: 线性结构 连续存储(数组) 离散存储(链表) 线性结构的两种常用应用之一 (栈) 线性结构的两种常用应用之一 (队列) 专题:递归 (1)1+2+3+4+5+… 100 的和 (2)求阶乘 (3)汉诺塔 (4)走迷宫 模块二:非线性结构 树 图 模块三:查找和排序 折半查找 排序:冒泡、插入、选择、快速

vincent3678的博客 1982

golang--算法--栈&队列&递归

关于栈、队列和递归的几个必知必会的代码实现 栈 用数组实现一个顺序栈 用链表实现一个链式栈 编程模拟实现一个浏览器的前进、后退功能 队列 用数组实现一个顺序队列 用链表实现一个链式队列 实现一个循环队列 递归 编程实现斐波那契数列求值 f(n)=f(n-1)+f(n-2) 编程实现求阶乘 n! 编程实现一组数据集合的全排列 1. 用数组实现一个顺序栈代码: package ...

天天water的专栏 4414

【算法与数据结构】链表、哈希表、栈和队列、二叉树

数据结构是组织和存储数据的方式,它定义了数据元素之间的关系和操作。数据结构可以分为线性结构(如数组、链表、队列、栈等)和非线性结构(如树、图、哈希表等),不同的数据结构适用于不同的应用场景,能够提供高效的数据操作方法。

杨荧的优快云博客 1842

数据结构与算法(三):栈与队列

上一篇《数据结构与算法(二):线性表》中介绍了数据结构中线性表的两种不同实现——顺序表与链表。这一篇主要介绍线性表中比较特殊的两种数据结构——栈与队列。首先必须明确一点,栈和队列都是线性表,它们中的元素都具有线性关系,即前驱后继关系。

Java+GO+JS全栈工程师-鹤冲天的博客:编程辅导~商务合作~技术交流 6594

算法基础02-链表、栈、队列、递归、哈希表、顺序表

文章目录1 链表、栈、队列、递归、哈希1.1 链表1.1.1 单向链表1.1.2 双向链表1.1.3 单双链表简单练习1.2 栈、队列1.3 栈、队列常见面试题1.4 递归1.4.1 递归行为的时间复杂度1.5 哈希表HashMap、HashSet1.6 顺序表 TreeMap、TreeSet 1 链表、栈、队列、递归、哈希 1.1 链表 1.1.1 单向链表 单向链表的节点结构(可以实现成泛型) : public class Node { public int value;

weixin_42503072的博客 466

【左程云算法全讲2】链表、栈、队列、递归、哈希表和有序表

系列综述:💞目的:本系列是个人整理为了秋招面试的,整理期间苛求每个知识点,平衡理解简易度与深入程度。🥰来源:材料主要源于进行的,每个知识点的修正和深入主要参考各平台大佬的文章,其中也可能含有少量的个人实验自证。🤭结语:如果有帮到你的地方,就和!!🌈。

qq_43840665的博客 514
上一篇: springCloud学习三之Eureka健康检查
下一篇: Python输出函数print
给自己加加油
博客等级 码龄8年 302粉丝 110原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值