• 算法组-常见的数据结构基础


    一、单向链表和双向链表最简单练习

            1)单链表和双向链表如何反转

            2)  删除指定值

            结点类型

    1. //单链表结点
    2. public static class Node{
    3. private int val;
    4. private Node next;
    5. public Node(int data){
    6. this.val = val;
    7. }
    8. }
    9. //双向链表结点
    10. public static class DoubleNode{
    11. private int value;
    12. private DoubleNode last;
    13. private DoubleNode next;
    14. public DoubleNode(int data){
    15. this.value = value;
    16. }
    17. }

            反转单链表

    1. /**
    2. * 反转单链表
    3. * 一定需要返回值(就是反转后的结点)
    4. */
    5. public static Node reverseLinkedList(Node head){
    6. Node pre = null;
    7. Node next = null;
    8. while (head!=null){
    9. next = head.next;
    10. head.next = pre;
    11. pre = head;
    12. head = next;
    13. }
    14. return pre;
    15. }

    删除单链指定值

    1. /**
    2. * 删除指定值,还得设置返回值,因为有可能删除了头部结点,所以需要返回Node
    3. */
    4. public static Node removeValue(Node head,int value){
    5. //head来到第一个不需要删除的位置作为头部
    6. while (head!=null){
    7. if (head.val != value){
    8. break;
    9. }
    10. head = head.next;
    11. }
    12. // 1 ) head == null
    13. // 2 ) head != null
    14. Node pre = head;
    15. Node cur = head;
    16. //从头开始往后遍历,如果值相同的话就将指针移动,否则下移,知道结束为止
    17. while (cur!=null){
    18. if (cur.val == value){
    19. pre.next = cur.next;
    20. }else {
    21. pre = cur;
    22. }
    23. cur = cur.next;
    24. }
    25. return head;
    26. }

    二、栈和队列 

            1)双向链表实现

            2)数组实现

    双向列表实现队列

    数组实现队列

            核心思想:定义多个变量,例如end,begin,size等待,end:默认为0,每次新加入一个元素就自增1,begin:默认为0,表示每取出一个元素的话,就在原来值的小标取出然后自增1.size:就只需要判断加入的数量就行。相当于解耦!

    1. package com.laoyang.体系班;
    2. /**
    3. * @author:Kevin
    4. * @create: 2023-10-15 18:06
    5. * @Description: 数组实现队列
    6. */
    7. public class day3_Array_Queen {
    8. public static class MyQueue{
    9. private int[] arr;
    10. private int pushi; //end:表示最新加进来的下标
    11. private int polli; //begin:表示最开始取处置后的小标
    12. private int size; // 表示已经存入的值的数量
    13. private final int limit;
    14. public MyQueue(int limit){
    15. arr = new int[limit];
    16. pushi = 0;
    17. polli = 0;
    18. this.limit = limit;
    19. }
    20. public void push(int value){
    21. if (limit == size){
    22. throw new RuntimeException("队列满了,不能再加了");
    23. }
    24. size++;
    25. arr[pushi] = value;
    26. //如果pushi没到底部就+1,到底部就置为0
    27. pushi = nextIndex(pushi);
    28. }
    29. //如果pushi没到底部就+1,到底部就置为0
    30. private int nextIndex(int i) {
    31. return i < limit-1 ? i+1 : 0;
    32. }
    33. public int pop(){
    34. if (size == 0){
    35. throw new RuntimeException("队列为空");
    36. }
    37. size--;
    38. int ans = arr[polli];
    39. polli = nextIndex(polli);
    40. return ans;
    41. }
    42. }
    43. }

    三、栈和队列常见面试题

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

            要求复杂度都为O(1)

            实现思路:实现两个栈,一个数据栈(正常栈),一个最小栈,

            第一次压入7:压入两个栈中

            第二次压入5:数据栈直接入栈,如果当前数比最小栈的栈顶要小,直接入栈

            第三次压入8:数据站直接入栈,如果当前数不比最小栈栈顶小,重复压入最小栈栈顶5

            以此类推~,同时两个栈,入栈是同步的。

            2. 如何用栈实现队列

            实现思路:

            思路一:定义两个栈,一个push栈,一个pop栈,刚开始入栈时进入push栈中,如果需要往出取的话,就将push栈的值进入pop栈中,然后将pop栈的数据返回给用户。

            前提条件:

                    1)必须一次性倒完。

                    2)如果pop栈没有拿完,不能倒数据

    1. package com.laoyang.体系班;
    2. import java.util.Stack;
    3. /**
    4. * @author:Kevin
    5. * @create: 2023-10-16 10:43
    6. * @Description: 栈实现队列
    7. */
    8. public class day3_zhan_Quene {
    9. public static class TwoStacksQueue {
    10. public Stack stackPush;
    11. public Stack stackPop;
    12. public TwoStacksQueue() {
    13. stackPop = new Stack();
    14. stackPush = new Stack();
    15. }
    16. //push栈向pop栈到数据
    17. private void pushToPop(){
    18. if (stackPop.isEmpty()){
    19. while (!stackPush.isEmpty()){
    20. stackPop.push(stackPush.pop());
    21. }
    22. }
    23. }
    24. public void add(int pushint){
    25. stackPush.push(pushint);
    26. pushToPop();
    27. }
    28. public int poll(){
    29. if (stackPush.isEmpty()&&stackPop.isEmpty()){
    30. throw new RuntimeException("队列为空");
    31. }
    32. pushToPop();
    33. return stackPop.pop();
    34. }
    35. }
    36. public static void main(String[] args) {
    37. TwoStacksQueue queue = new TwoStacksQueue();
    38. queue.add(1);
    39. queue.add(2);
    40. queue.add(3);
    41. System.out.println(queue.poll());
    42. }
    43. }

            

            3.如何用队列实现栈

            实现思路:

                    两个队列,互相倒数据,首先将插入的数据放入队列一,如果需要出一个数据,就将其他数据放入队列二,然后出栈这个数据,如果又有数据出,就将这个数据留下来,其他数据又放入队列一,来回互相倒。

            

    1. class MyStack {
    2. Queue queue1;
    3. Queue queue2;
    4. public MyStack() {
    5. queue1 = new LinkedList();
    6. queue2 = new LinkedList();
    7. }
    8. public void push(int x) {
    9. queue2.offer(x);
    10. while(!queue1.isEmpty()){
    11. queue2.offer(queue1.poll());
    12. }
    13. Queue temp = queue1;
    14. queue1 = queue2;
    15. queue2 = temp;
    16. }
    17. public int pop() {
    18. return queue1.poll();
    19. }
    20. public int top() {
    21. return queue1.peek();
    22. }
    23. public boolean empty() {
    24. return queue1.isEmpty();
    25. }
    26. }

    四、递归

            1. 数组最大值

    1. package com.laoyang.体系班;
    2. /**
    3. * @author:Kevin
    4. * @create: 2023-10-16 17:19
    5. * @Description: 递归
    6. */
    7. public class day3_digui {
    8. public static void main(String[] args) {
    9. System.out.println(getMax(new int[]{1, 2, 3, 4, 5}));
    10. }
    11. public static int getMax(int[] arr){
    12. return arrMax(arr,0,arr.length-1);
    13. }
    14. public static int arrMax(int[] arr,int L,int R){
    15. if (L==R){
    16. return arr[L];
    17. }
    18. int mid = L + ((R-L) >> 1);
    19. int leftMax = arrMax(arr,L,mid);
    20. int rightMax = arrMax(arr,mid+1,R);
    21. return Math.max(leftMax,rightMax);
    22. }
    23. }

    五、哈希表(Hashmap)与有序表 (TreeMap)

  • 相关阅读:
    Keras深度学习实战(17)——使用U-Net架构进行图像分割
    2016-2023全国MEM国家A类线趋势图:浙大MEM要高多少?
    前端小案例-图片存放在远端服务器
    C++设计模式---模板方法模式
    刚来的00后真的卷,听说工作还没两年,跳到我们公司直接起薪20k...
    lambda表达式
    【C语言】typedef
    常见C++开源库-几何算法库-Boost.Geometry-Clipper2-布尔运算库-支持开放式多段线-基础几何对象-详解教程
    dBm dBi dBd dB dBc解释
    9. Vue3中如何将虚拟节点渲染成真实节点
  • 原文地址:https://blog.csdn.net/qq_67801847/article/details/133845316