104. 二叉树的最大深度

public int maxDepth(TreeNode root) {
if (root == null)
return 0;
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
142. 环形链表 II

public ListNode detectCycle(ListNode head) {
if (head == null || head.next == null)
return null
ListNode fast = head, slow = head
while (fast != null && fast.next != null) {
fast = fast.next.next
slow = slow.next
if (fast == slow) {
break
}
}
if (fast != slow)
return null
fast = head
while (fast != slow) {
fast = fast.next
slow = slow.next
}
return slow
}
160. 相交链表

public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) {
return null
}
ListNode ptrA = headA
ListNode ptrB = headB
while (ptrA != ptrB) {
ptrA = ptrA == null ? headB : ptrA.next
ptrB = ptrB == null ? headA : ptrB.next
}
return ptrA
}
543. 二叉树的直径

class Solution {
int length = 0
public int diameterOfBinaryTree(TreeNode root) {
dfsLength(root)
return length
}
private int dfsLength(TreeNode root) {
if(root == null){
return 0
}
int lHeight = dfsLength(root.left)
int rHeight = dfsLength(root.right)
length = Math.max(length,lHeight+rHeight)
return Math.max(lHeight,rHeight)+1
}
}
344. 反转字符串

class Solution {
public void reverseString(char[] s) {
if(s == null || s.length == 1){
return
}
int l = 0 ,r = s.length-1
while(l < r){
swap(l , r , s)
l++
r--
}
}
public void swap(int l ,int r , char[] s){
char temp = s[l]
s[l] = s[r]
s[r] = temp
}
}
144. 二叉树的前序遍历

public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>()
if (root == null) return result
Stack<TreeNode> stack = new Stack()
stack.add(root)
while (!stack.isEmpty()) {
TreeNode cur = stack.pop()
result.add(cur.val)
if (cur.right != null)
stack.add(cur.right)
if (cur.left != null)
stack.add(cur.left)
}
return result
}
146. LRU缓存机制

class LRUCache {
public static final int NOTFOUND = -1;
int capacity = 0;
LinkedHashMap<Integer, Integer> map = new LinkedHashMap<>();
public LRUCache(int capacity) {
this.capacity = capacity;
}
public int get(int key) {
int value = NOTFOUND;
if (!map.containsKey(key))
return value;
value = map.get(key);
map.remove(key);
map.put(key, value);
return value;
}
public void put(int key, int value) {
if (map.containsKey(key)) {
map.remove(key);
} else if (map.size() == capacity) {
map.remove(map.keySet().iterator().next());
}
map.put(key, value);
}
}
232. 用栈实现队列

class MyQueue {
private Stack<Integer> in = new Stack<>();
private Stack<Integer> out = new Stack<>();
public void push(int x) {
in.push(x);
}
public int pop() {
in2out();
return out.pop();
}
public int peek() {
in2out();
return out.peek();
}
private void in2out() {
if (out.isEmpty()) {
while (!in.isEmpty()) {
out.push(in.pop());
}
}
}
public boolean empty() {
return in.isEmpty() && out.isEmpty();
}
}
25. K 个一组翻转链表

public ListNode reverseKGroup(ListNode head, int k) {
if (head == null || k == 0 || k == 1) {
return head
}
int length = 0
ListNode dummyNode = new ListNode(-1)
ListNode prev = dummyNode, cur = head, next = null
dummyNode.next = head
while (head != null) {
head = head.next
length++
}
for (int i = 0
for (int j = 1
// 内循环中 prev 未变过 cur 未变过
// 只是每次循环 将 cur 后的node 提到 prev后 d123 d213 d321
next = cur.next
cur.next = next.next
next.next = prev.next
prev.next = next
}
prev = cur
cur = prev.next
}
return dummyNode.next
}
21. 合并两个有序链表

public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) return l2
if (l2 == null) return l1
if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2)
return l1
} else {
l2.next = mergeTwoLists(l1, l2.next)
return l2
}
}
206. 反转链表

public ListNode reverseList(ListNode head) {
if (head == null)
return head
ListNode dummyNode = new ListNode(-1)
while (head != null) {
ListNode next = head.next
head.next = dummyNode.next
dummyNode.next = head
head = next
}
return dummyNode.next
}
236. 二叉树的最近公共祖先

public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == p || root == q || root == null )
return root
TreeNode leftResult = lowestCommonAncestor(root.left,p,q)
TreeNode rightResult = lowestCommonAncestor(root.right,p,q)
return leftResult==null ? rightResult :rightResult==null?leftResult:root
}
215. 数组中的第K个最大元素

public int findKthLargest(int[] nums, int k) {
if (nums == null)
return 0;
PriorityQueue<Integer> queue = new PriorityQueue<>(k);
for (int ele : nums) {
if (queue.size() < k) {
queue.add(ele);
continue;
}
if (queue.peek() < ele) {
queue.poll();
queue.add(ele);
}
}
return queue.peek();
}
53. 最大子序和.

public int maxSubArray(int[] nums) {
if (nums.length == 0) {
return 0
}
if (nums.length == 1) {
return nums[0]
}
// dp[i] --> 以 i 结尾的最大长度
int[] dp = new int[nums.length]
int maxValue
dp[0] = maxValue = nums[0]
for (int i = 1
dp[i] = Math.max(nums[i], dp[i - 1] + nums[i])
if (dp[i] > maxValue) {
maxValue = dp[i]
}
}
return maxValue
}
103. 二叉树的锯齿形层次遍历

public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
if (root == null)
return new ArrayList()
List<List<Integer>> result = new ArrayList<>()
int flag = 0
Deque<TreeNode> queue = new LinkedList<>()
queue.add(root)
while (!queue.isEmpty()) {
int size = queue.size()
List<Integer> path = new ArrayList()
for (int i = 0
TreeNode node = queue.poll()
if (node.left != null)
queue.add(node.left)
if (node.right != null)
queue.add(node.right)
path.add(node.val)
}
if (flag % 2 == 1) {
Collections.reverse(path)
}
flag++
if (path.size() != 0) {
result.add(path)
}
}
return result
}