算法
🔥 常见算法分类 + Java实现
1. 排序算法 (Sorting)
1.1 冒泡排序 (Bubble Sort)
1 | public static void bubbleSort(int[] arr) { |
1.2 选择排序 (Selection Sort)
1 | public static void selectionSort(int[] arr) { |
1.3 插入排序 (Insertion Sort)
1 | public static void insertionSort(int[] arr) { |
1.4 归并排序 (Merge Sort)
1 | public static void mergeSort(int[] arr, int l, int r) { |
1.5 快速排序 (Quick Sort)
1 | public static void quickSort(int[] arr, int l, int r) { |
2. 查找算法 (Searching)
2.1 顺序查找 (Linear Search)
1 | public static int linearSearch(int[] arr, int target) { |
2.2 二分查找 (Binary Search)(数组有序)
1 | public static int binarySearch(int[] arr, int target) { |
3. 双指针算法 (Two Pointers)
3.1 反转数组
1 | public static void reverse(int[] arr) { |
3.2 三数之和 (LeetCode 15)
1 | public static List<List<Integer>> threeSum(int[] nums) { |
4. 动态规划 (Dynamic Programming)
4.1 斐波那契数列
1 | public static int fib(int n) { |
4.2 爬楼梯 (LeetCode 70)
1 | public static int climbStairs(int n) { |
4.3 最长公共子序列 (LCS)
1 | public static int lcs(String s1, String s2) { |
5. 贪心算法 (Greedy)
5.1 跳跃游戏 (LeetCode 55)
1 | public static boolean canJump(int[] nums) { |
6. 分治算法 (Divide & Conquer)
6.1 归并排序 (见上)
6.2 快速排序 (见上)
7. 回溯算法 (Backtracking)
7.1 全排列 (LeetCode 46)
1 | public static List<List<Integer>> permute(int[] nums) { |
8. 图算法 (Graph Algorithms)
8.1 BFS(最短路径)
1 | public static void bfs(Map<Integer, List<Integer>> graph, int start) { |
8.2 DFS
1 | public static void dfs(Map<Integer, List<Integer>> graph, int start, Set<Integer> visited) { |
8.3 Dijkstra 最短路径
1 | public static int[] dijkstra(int[][] graph, int src) { |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 浮生若梦!
