自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(53)
  • 收藏
  • 关注

原创 链表的操作

反转 Reverse Linked List 单链表反转 easy Reverse Linked List II 反转链表的一部分 middle Reverse Nodes in k-Group 每k个节点 反转一次 Reorder List 涉及链表反转 middle Rotate List 从右向左翻转k个node 合并有序链表 Merge Two Sorted Lists 合并两个有序链表 easy

2020-05-26 21:18:33 114

原创 42. Trapping Rain Water

public int trap(int[] height) { int ans = 0; Deque<Integer> stack = new ArrayDeque<>(); for (int i = 0; i < height.length; i++) { while (!stack.isEmpty() && (height[i] > height[stack.peek()]))

2020-05-26 20:52:10 88

原创 150. Evaluate Reverse Polish Notation

public int evalRPN(String[] tokens) { Stack<Integer> stack = new Stack<>(); for (String s : tokens) { if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) { int right = stack.pop(); int left = stack.pop(); if (s

2020-05-23 21:14:36 99

原创 147. Insertion Sort List

public ListNode insertionSortList(ListNode head) { ListNode dummyHead = new ListNode(Integer.MIN_VALUE); ListNode prev = dummyHead; ListNode curr = head; while (curr != null) { ListNode then = curr.next;

2020-05-23 20:19:55 85

原创 146. LRU Cache

class LRUCache { private LinkedHashMap<Integer, Integer> map; public LRUCache(int capacity) { map = new LinkedHashMap<Integer, Integer>(capacity, 0.75f, true){ @Override protected boolean removeEldestEn

2020-05-22 15:42:56 84

原创 145. Binary Tree Postorder Traversal

public List<Integer> postorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<>(); postorderTraversalSub(root, res); return res; } private void postorderTraversalSub(TreeNode node, List<In

2020-05-21 23:04:12 78

原创 144. Binary Tree Preorder Traversal

public List<Integer> preorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<>(); preorderTraversalSub(root, res); return res; } private void preorderTraversalSub(TreeNode node, List<Integ

2020-05-21 22:52:01 81

原创 148. Sort List

public ListNode sortList(ListNode head) { if (head == null || head.next == null) return head; ListNode slow = head; ListNode fast = head; // while (fast != null && fast.next != null) { // 会在sortList(head);

2020-05-15 17:59:51 78

原创 82. Remove Duplicates from Sorted List II

public ListNode deleteDuplicates(ListNode head) { if (head == null || head.next == null) return head; ListNode dummyHead = new ListNode(head.val-1); dummyHead.next = head; ListNode prev = dummyHead;

2020-05-15 16:29:20 82

原创 143. Reorder List

if (head == null || head.next == null) return; ListNode p1 = head; ListNode p2 = head; while (p2.next != null && p2.next.next != null) { p1 = p1.next; p2 = p2.next.next; } ListNode mid = p1; ListNode prev = mid; ListNode curr = mid.n

2020-05-15 13:31:36 90

原创 142. Linked List Cycle II

public ListNode detectCycle(ListNode head) { ListNode slow = head; ListNode fast = head; boolean hasCycle = false; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.ne

2020-05-15 10:55:40 68

原创 141. Linked List Cycle

public boolean hasCycle(ListNode head) { ListNode slow = head; ListNode fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; if (slow == fast)

2020-05-15 10:30:04 108

原创 138. Copy List with Random Pointer

public Node copyRandomList(Node head) { if (head == null) return head; Node ptr = head; while (ptr != null) { Node newNode = new Node(ptr.val); newNode.next = ptr.next; ptr.next = newNode;

2020-05-14 16:33:40 68

原创 java boolean

在 Java 虚拟机规范中,boolean 类型则被映射成 int 类型。具体来说,“true”被映射为整数 1,而“false”被映射为整数 0。这个编码规则约束了 Java 字节码的具体实现。 也就是说boolean占4个字节

2020-05-14 14:16:56 113

原创 51. N-Queens

public List<List<String>> solveNQueens(int n) { char[][] board = new char[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ...

2020-05-14 10:24:50 100

原创 拓扑排序

public int[] findOrder(int numCourses, int[][] prerequisites) { int[][] adjacent = new int[numCourses][numCourses]; int[] indegree = new int[numCourses]; for (int i = 0; i < prerequisites.length; i++) { int from = pr

2020-05-12 12:17:32 90

原创 207. Course Schedule

public boolean canFinish(int numCourses, int[][] prerequisites) { int[][] adjacent = new int[numCourses][numCourses]; int[] indegree = new int[numCourses]; for (int i = 0; i < prerequisites.length; i++) { int ready =

2020-05-12 12:16:44 89

原创 210. Course Schedule II

public int[] findOrder(int numCourses, int[][] prerequisites) { int[][] adjacent = new int[numCourses][numCourses]; int[] indegree = new int[numCourses]; for (int i = 0; i < prerequisites.length; i++) { int from = pr

2020-05-12 12:14:08 81

原创 133. Clone Graph

private Map<Integer, Node> map = new HashMap<>(); public Node cloneGraph(Node node) { return clone(node); } private Node clone(Node node) { if (node == null) return null; if (map.containsKe

2020-05-12 09:36:44 86

原创 130. Surrounded Regions

class QuickUnionUF { private int[] parent; private int[] size; private int count; public QuickUnionUF(int n) { if (n < 0) throw new IllegalArgumentException(); parent = new int[n]; size = new int[n];

2020-05-11 21:32:52 61

原创 并查集

class QuickUnionUF { private int[] parent; private int[] size; private int count; public QuickUnionUF(int n) { if (n < 0) throw new IllegalArgumentException(); parent = new int[n]; size = new int[n];

2020-05-11 21:31:43 84

原创 129. Sum Root to Leaf Numbers

public int sumNumbers(TreeNode root) { if (root == null) return 0; return sumNumbersSub(root, 0); } private int sumNumbersSub(TreeNode node, int val) { if (node == null) return 0; if (node.left == null &&

2020-05-11 20:19:55 60

原创 128. Longest Consecutive Sequence

public int longestConsecutive(int[] num) { int res = 0; HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int n : num) { if (!map.containsKey(n)) { int left = (map.containsKey(n - 1)) ? map.ge

2020-05-11 18:01:46 63

原创 125. Valid Palindrome

public boolean isPalindrome(String s) { if (s == null || s.trim().length() <= 1) return true; s = s.toLowerCase(); for (int i = 0, j = s.length()-1; i < j;) { if (!isLetterOrDigit(s.charAt(i)

2020-05-11 17:27:57 97

原创 124. Binary Tree Maximum Path Sum

int maxVal; public int maxPathSum(TreeNode root) { maxVal = Integer.MIN_VALUE; maxPathSub(root, root); return maxVal; } private int maxPathSub(TreeNode node, TreeNode root) { if (node == null) return 0;

2020-05-11 17:07:44 61

原创 123. Best Time to Buy and Sell Stock III

public int maxProfit(int[] prices) { if(prices == null || prices.length == 0) return 0; int totalK = 2; int[][] dp = new int[totalK+1][prices.length]; for(int k = 1;k<=totalK;k++){//profit = 0 when k = 0 for(i

2020-05-11 16:44:32 84

原创 122. Best Time to Buy and Sell Stock II

public int maxProfit(int[] prices) { if (prices.length == 0) return 0; int maxVal = 0; for (int i = 0; i < prices.length-1; i++) { if (prices[i+1] > prices[i]) { maxVal += prices[i+1] -

2020-05-11 13:38:54 66

原创 120. Triangle

public int minimumTotal(List<List<Integer>> t) { if (t.size() == 0) return 0; int n = t.size(); if (n == 0) return 0; if (n == 1) return t.get(0).get(0); int[] dp = new int[n]; for (int i = 0;

2020-05-11 10:37:15 70

原创 119. Pascal's Triangle II

public List<Integer> getRow(int rowIndex) { List<Integer> row = new ArrayList<>(); for (int i = 0; i <= rowIndex; i++) { row.add(0, 1); for (int j = 1; j < row.size()-1;

2020-05-11 10:21:08 61

原创 118. Pascal‘s Triangle

public List<List<Integer>> generate(int numRows) { List<List<Integer>> res = new ArrayList<>(); List<Integer> row = new ArrayList<>(); for (int i = 0; i < numRows; i++) {

2020-05-11 10:15:36 65

原创 缓存与缓冲区

缓存可以提高低速设备的访问速度,或者减少复杂耗时的计算带来的性能问题。理论上说,我们可以通过缓存解决所有关于“慢”的问题,比如从磁盘随机读取数据慢,从数据库查询数据慢,只是不同的场景消耗的存储成本不同。 缓冲区则是一块临时存储数据的区域,这些数据后面会被传输到其他设备上。缓冲区更像“消息队列篇”中即将提到的消息队列,用以弥补高速设备和低速设备通信时的速度差。比如,我们将数据写入磁盘时并不是直接刷盘,而是写到一块缓冲区里面,内核会标识这个缓冲区为脏。当经过一定时间或者脏缓冲区比例到达一定阈值时,由单独的线程.

2020-05-09 15:54:45 275

原创 116. Populating Next Right Pointers in Each Node

public Node connect(Node root) { if (root == null) return root; Node prev = root; Node curr = null; while (prev.left != null) { curr = prev; while (curr != null) { curr.left.next = c

2020-05-09 14:57:07 68

原创 114. Flatten Binary Tree to Linked List

TreeNode prev = null; public void flatten(TreeNode root) { if (root == null) return; flatten(root.right); flatten(root.left); root.right = prev; root.left = null; prev = root;

2020-05-09 14:54:15 96

原创 113. Path Sum II

public List<List<Integer>> pathSum(TreeNode root, int sum) { List<List<Integer>> res = new ArrayList<>(); pathSumSub(res, new ArrayList<>(), root, sum); return res; } private vo

2020-05-09 14:14:39 64

原创 112. Path Sum

public boolean hasPathSum(TreeNode root, int sum) { if (root == null) return false; if (root.left == null && root.right == null && sum - root.val == 0) return true; return hasPathSum(root.l

2020-05-09 13:46:06 63

原创 111. Minimum Depth of Binary Tree

public int minDepth(TreeNode root) { if (root == null) return 0; Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root); int level = 0; while (!queue.isEmpty()) { level++; int size = queue.size(); for (int i =

2020-05-09 13:35:34 61

原创 110. Balanced Binary Tree

public boolean isBalanced(TreeNode root) { if (root == null) return true; int left = getHeight(root.left); int right = getHeight(root.right); if (Math.abs(left - right) > 1) return false;

2020-05-09 12:40:47 102

原创 109. Convert Sorted List to Binary Search Tree

public TreeNode sortedListToBST(ListNode head) { if (head == null) return null; return toBST(head, null); } private TreeNode toBST(ListNode head, ListNode tail) { if (head == tail) return null; .

2020-05-09 12:30:25 64

原创 108. Convert Sorted Array to Binary Search Tree

public TreeNode sortedArrayToBST(int[] nums) { if (nums.length == 0) return null; TreeNode root = buildBST(nums, 0, nums.length-1); return root; } private TreeNode buildBST(int[] nums, int lo, int hi) {

2020-05-09 10:45:36 55

原创 104. Maximum Depth of Binary Tree

public int maxDepth(TreeNode root) { if (root == null) return 0; int left = maxDepth(root.left); int right = maxDepth(root.right); return Math.max(left, right) + 1; }

2020-05-09 09:42:15 52

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除