一些基本的数据结构算法

单链表的数据结构

class Node {
    
    
    public int value;
    public Node next;
    public Node(int value) {
    
    
        this.value = value;
    }
}

双链表数据结构

public class DoubleNode {
    
    
	public int value;
	public DoubleNode last;
	public DoubleNode next;
	public DoubleNode(int data) {
    
    
		value = data;
	}
}

1链表反转

public static Node reverseNode(Node head) {
    
    
        Node pre = null;
        Node next = null;
        while(head != null) {
    
    
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }

2删除链表中指定的值

 public static Node deleteNode (Node head, int value) {
    
    
        // 1、找到新的头结点
        while (head != null) {
    
    
            if (head.value != value) {
    
    
                break;
            }
            head = head.next;
        }
        Node pre = head;
        Node cur = head;
        while (cur != null) {
    
    
            if (cur.value == value) {
    
    
                pre.next = cur.next;
            } else {
    
    
                pre = cur;
            }
            cur = cur.next;
        }
        return head;
    }

3用数组实现队列

在这里插入图片描述

class MyQueue {
    
    
    static class Queue {
    
    
        private int[] arr;
        private int pushi; // begin
        private int polli; // end
        private int size;
        private final int limit;

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

        public void push(int value) {
    
    
            if (size == limit) {
    
    
                return;
            }
            size++;
            arr[pushi] = value;
            nextIndex(pushi);
        }

        public int poll() {
    
    
            if (size == 0) {
    
    
                return -1;
            }
            size--;
            int ans = arr[polli];
            nextIndex(polli);
            return ans;
        }

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

        private int nextIndex(int i) {
    
    
            return i < limit - 1 ? i + 1 : 0;
        }
    }

}

4实现一个特殊的栈:基本功能 + 返回栈中最小元素

  1. pop、 push、 getMin操作的时间复杂度都是0(1)。
    2)设计的栈类型可以使用现成的栈结构。
    在这里插入图片描述

5用栈实现队列 && 用队列实现栈

在这里插入图片描述

class TwoStackQueue {
    
    
    public Stack<Integer> stackPush;
    public Stack<Integer> stackPop;

    public TwoStackQueue() {
    
    
        stackPush = new Stack<>();
        stackPop = new Stack<>();
    }

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

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

    public int poll() {
    
    
        if (stackPop.empty() && stackPush.empty()) {
    
    
            return -1;
        }
        pushToPop();
        return stackPop.pop();
    }

    public int peek() {
    
    
        if (stackPop.empty() && stackPush.empty()) {
    
    
            return -1;
        }
        pushToPop();
        return stackPop.peek();
    }
}

在这里插入图片描述

class TwoQueueStack<T> {
    
    
    public Queue<T> queue;
    public Queue<T> helper;
    public TwoQueueStack() {
    
    
        queue = new LinkedList<>();
        helper = new LinkedList<>();
    }
    public void push(T value) {
    
    
        queue.offer(value);
    }
    public T poll() {
    
    
        while (queue.size() > 1) {
    
    
            helper.offer(queue.poll());
        }
        T ans = queue.poll();
        Queue<T> tmp = queue;
        queue = helper;
        helper = tmp;
        return ans;
    }
    public boolean isEmpty() {
    
    
        return queue.isEmpty();
    }
}

递归算法

在这里插入图片描述
子问题规模一致(Master公式):T(N) = a x T(N/b) + O(1)
1)log以b为底的a次方 < d —> O(N的d次方)
2)log以b为底的a次方 > d —> O(N的(log以b为底的a次方)的方)
3)log以b为底的a次方 = d —> O(N的d次方乘以logN)

时间复杂度公式:T(N) = 2 x T(N/2) + O(1)

public static 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 + ((L + R) >> 1);
	int leftMax = process(arr, L, mid);
	int rightMax = process(arr, mid + 1, R);
	return Math.max(leftMax, rightMax);
}

猜你喜欢

转载自blog.csdn.net/zs18753479279/article/details/119512225