链表反转
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
}
快速排序
public class QuickSort {
public static void quickSort(int[] arr, int low, int high) {
int i, j, base;
if (low > high) {
return;
}
i = low;
j = high;
base = arr[low];
while (i < j) {
//先看右边,依次往左递减
while (base <= arr[j] && i < j) {
j--;
}
//再看左边,依次往右递增
while (base >= arr[i] && i < j) {
i++;
}
//如果满足条件则交换
if (i < j) {
int t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
//最后将基准为与i和j相等位置的数字交换
arr[low] = arr[i];
arr[i] = base;
//递归调用左半数组
quickSort(arr, low, j - 1);
//递归调用右半数组
quickSort(arr, j + 1, high);
}
public static void main(String[] args) {
int[] arr = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
quickSort(arr, 0, arr.length - 1);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
public class A3_MergeTwoSortList {
public ListNode Merge(ListNode l1, ListNode l2) {
ListNode newNode = new ListNode(0);
ListNode cur = newNode;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
cur.next = l1;
l1 = l1.next;
} else {
cur.next = l2;
l2 = l2.next;
}
cur = cur.next;
}
cur.next = l1 != null ? l1 : l2;
return newNode.next;
}
}
搜索旋转数组
public int search(int[] nums, int target) {
int lo = 0;
int hi = nums.length - 1;
int mid = 0;
while (lo <= hi) {
mid = lo + (hi - lo) / 2;
if (nums[mid] == target) {
return mid;
}
// 先根据 nums[mid] 与 nums[lo] 的关系判断 mid 是在左段还是右段
if (nums[mid] >= nums[lo]) {
// 再判断 target 是在 mid 的左边还是右边,从而调整左右边界 lo 和 hi
if (target >= nums[lo] && target < nums[mid]) {
hi = mid - 1;
} else {
lo = mid + 1;
}
} else {
if (target > nums[mid] && target <= nums[hi]) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
}
return -1;
}
public class A2_LruCache {
Map<Integer, Integer> map;
Deque<Integer> list = new LinkedList<>();
int size = 0;
int capacity;
public A2_LruCache(int capacity) {
this.capacity = capacity;
map = new HashMap<>(capacity);
}
public int get(int key) {
if (map.containsKey(key)) {
list.remove(key);
list.addFirst(key);
return map.get(key);
} else {
return -1;
}
}
public void set(int key, int value) {
if (size >= capacity) {
int del = list.removeLast();
map.remove(del);
size--;
}
if (map.containsKey(key)) {
list.remove(key);
}
list.addFirst(key);
map.put(key, value);
size++;
}
}
public class A055_MergeCommonList {
static class Interval {
public int start;
public int end;
public Interval() {
this.start = 0;
this.end = 0;
}
public Interval(int s, int e) {
this.start = s;
this.end = e;
}
}
public ArrayList<Interval> merge(ArrayList<Interval> list) {
ArrayList<Interval> mergeList = new ArrayList<>();
Collections.sort(list, (a, b) -> a.start - b.start);
int len = list.size();
int index = 0;
while (index < len) {
int start = list.get(index).start;
int end = list.get(index).end;
//循环比较后一个列表的起始值是否小于等于当前的右边值
//右边值等于前后列表右边值的最大值
while (index < len - 1 && list.get(index + 1).start <= end) {
end = Math.max(end, list.get(index + 1).end);
index++;
}
mergeList.add(new Interval(start, end));
index++;
}
return mergeList;
}
}
* @desc 二叉树根节点到叶子节点的所有路径和
* <p>
* 给定一个二叉树的根节点root,该树的节点值都在数字\ 0-9 0−9 之间,每一条从根节点到叶子节点的路径都可以用一个数字表示。
* 1.该题路径定义为从树的根结点开始往下一直到叶子结点所经过的结点
* 2.叶子节点是指没有子节点的节点
* 3.路径只能从父节点到子节点,不能从子节点到父节点
* 4.总节点数目为n
* <p>
* 这题说的是每条从根节点到叶子结点的路径都代表一个数字,然后再把这些数字加起来即可。
* 遍历一棵树从根节点到叶子结点的所有路径,最容易想到的是DFS,所以这题使用DFS是最容易解决的。
* 如果对二叉树的DFS不熟悉的话,可以看下373,数据结构-6,树
* <p>
* 解决方式就是从根节点往下走的时候,那么当前节点的值就是父节点的值*10+当前节点的值。
* 默认根节点的父节点的值是0,如果到达叶子结点,就用一个全局的变量把叶子结点的值加起来。
* 这里就以示例2为例来画个图看一下
*/
public class A052_TreeSumNumbers {
public int sumNumbers(TreeNode root) {
//如果根节点是空,直接返回0即可
if (root == null) {
return 0;
}
//两个栈,一个存储的是节点,一个存储的是节点对应的值
Stack<TreeNode> nodeStack = new Stack<>();
Stack<Integer> valueStack = new Stack<>();
//把根节点和根节点的值分别压入两个栈中
nodeStack.add(root);
valueStack.add(root.val);
//全局的,统计所有路径的和
int sum = 0;
while (!nodeStack.isEmpty()) {
//当前节点和当前节点的值同时出栈
TreeNode node = nodeStack.pop();
int value = valueStack.pop();
if (node.left == null && node.right == null) {
//如果当前节点是叶子结点,说明找到了一条路径,把这条路径的值加入到全局变量res中
sum += value;
} else {
//如果不是叶子节点就执行下面的操作
if (node.right != null) {
//把子节点和子节点的值分别加入到栈中,这里子节点的值就是父节点的值*10+当前节点的值
nodeStack.push(node.right);
valueStack.push(value * 10 + node.right.val);
}
if (node.left != null) {
nodeStack.push(node.left);
valueStack.push(value * 10 + node.left.val);
}
}
}
return sum;
}
}
**
* 给定一个二叉树,找出其最大深度。
* <p>
* 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
* <p>
* 说明: 叶子节点是指没有子节点的节点。
* <p>
* 示例:
* 给定二叉树 [3,9,20,null,null,15,7]
*/
public class TreeMaxDepth {
public static int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int ans = 0;
while (!queue.isEmpty()) {
int size = queue.size();
while (size > 0) {
TreeNode node = queue.poll();
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
size--;
}
ans++;
}
return ans;
}
/**
* 方法二:递归
* @param root
* @return
*/
public int maxDepth2(TreeNode root) {
if (root == null) {
return 0;
} else {
int leftHeight = maxDepth(root.left);
int rightHeight = maxDepth(root.right);
return Math.max(leftHeight, rightHeight) + 1;
}
}
经典算法实现

本文涵盖链表反转、快速排序、两排序链表合并等算法实现,深入解析搜索旋转数组及二叉树路径求和等问题。
2万+

被折叠的 条评论
为什么被折叠?



