数据结构
2026/1/15大约 2 分钟
数据结构
数组
特点
- 连续内存存储
- 随机访问 O(1)
- 插入删除 O(n)
动态数组(ArrayList)
// 扩容机制
private void grow(int minCapacity) {
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1); // 1.5 倍
elementData = Arrays.copyOf(elementData, newCapacity);
}链表
单链表
class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
}
}
// 反转链表
public ListNode reverse(ListNode head) {
ListNode prev = null, curr = head;
while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}双向链表
class DoublyListNode {
int val;
DoublyListNode prev, next;
}栈
特点
- 后进先出(LIFO)
- push/pop O(1)
应用
- 括号匹配
- 表达式求值
- 函数调用栈
// 有效括号
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for (char c : s.toCharArray()) {
if (c == '(') stack.push(')');
else if (c == '[') stack.push(']');
else if (c == '{') stack.push('}');
else if (stack.isEmpty() || stack.pop() != c) return false;
}
return stack.isEmpty();
}队列
特点
- 先进先出(FIFO)
- enqueue/dequeue O(1)
应用
- BFS
- 任务调度
- 消息队列
// 用栈实现队列
class MyQueue {
Stack<Integer> in = new Stack<>();
Stack<Integer> out = new Stack<>();
public void push(int x) {
in.push(x);
}
public int pop() {
if (out.isEmpty()) {
while (!in.isEmpty()) {
out.push(in.pop());
}
}
return out.pop();
}
}哈希表
特点
- 查找/插入/删除 O(1)
- 空间换时间
冲突解决
- 链地址法
- 开放地址法
// 两数之和
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[]{map.get(complement), i};
}
map.put(nums[i], i);
}
return new int[0];
}二叉树
遍历
// 前序遍历
public void preorder(TreeNode root) {
if (root == null) return;
visit(root);
preorder(root.left);
preorder(root.right);
}
// 中序遍历
public void inorder(TreeNode root) {
if (root == null) return;
inorder(root.left);
visit(root);
inorder(root.right);
}
// 后序遍历
public void postorder(TreeNode root) {
if (root == null) return;
postorder(root.left);
postorder(root.right);
visit(root);
}
// 层序遍历
public void levelOrder(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
visit(node);
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
}二叉搜索树
// 查找
public TreeNode search(TreeNode root, int val) {
if (root == null || root.val == val) return root;
return val < root.val ? search(root.left, val) : search(root.right, val);
}
// 插入
public TreeNode insert(TreeNode root, int val) {
if (root == null) return new TreeNode(val);
if (val < root.val) root.left = insert(root.left, val);
else root.right = insert(root.right, val);
return root;
}堆
特点
- 完全二叉树
- 最大堆/最小堆
- 插入/删除 O(logn)
应用
- 优先队列
- TopK 问题
- 堆排序
// TopK 最小的 K 个数
public int[] topK(int[] arr, int k) {
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
for (int num : arr) {
maxHeap.offer(num);
if (maxHeap.size() > k) maxHeap.poll();
}
return maxHeap.stream().mapToInt(i -> i).toArray();
}