- 博客(78)
- 收藏
- 关注
原创 LeetCode-Algorithms-[Easy]面试题40. 最小的k个数
面试题40. 最小的k个数方法一:直接排序 public int[] getLeastNumbers_2(int[] arr, int k) { if (k == 0 || arr.length == 0) { return new int[0]; } Arrays.sort(arr); return Arrays.copyOfRange(arr, 0, k);...
2020-04-23 10:01:02 336
原创 LeetCode-Algorithms-[Easy][摩尔投票法]169. 多数元素
169. 多数元素官方题解 public int majorityElement(int[] nums) { int num = nums[0]; int vote = 1; for (int i = 1; i < nums.length; ++i) { if (vote == 0) { num = nums[i]; } if (nu...
2020-04-20 11:05:29 210
原创 LeetCode-Algorithms-[Easy][摩尔投票法]面试题39. 数组中出现次数超过一半的数字
面试题39. 数组中出现次数超过一半的数字摩尔投票法详解 public int majorityElement(int[] nums) { int num = nums[0]; int vote = 1; for (int i = 1; i < nums.length; ++i) { if (vote == 0) { num = nums[i]; ...
2020-04-20 11:03:48 244
原创 LeetCode-Algorithms-[Mid]面试题38. 字符串的排列
面试题38. 字符串的排列 private char[] c; private HashSet<String> set; private int length; public String[] permutation(String s) { initial(s); permute(0); String[] res = new String[set.size...
2020-04-20 10:13:45 261
原创 LeetCode-Algorithms-[Hard]面试题37. 序列化二叉树
面试题37. 序列化二叉树 // Encodes a tree to a single string. public String serialize(TreeNode root) { if (root == null) { return "[]"; } StringBuilder sb = new StringBuilder("["); Queue...
2020-04-20 09:42:39 223
原创 LeetCode-Algorithms-[Hard]297. 二叉树的序列化与反序列化
297. 二叉树的序列化与反序列化 // Encodes a tree to a single string. public String serialize(TreeNode root) { if (root == null) { return "[]"; } StringBuilder sb = new StringBuilder("["); Q...
2020-04-20 09:40:20 219
原创 LeetCode-Algorithms-[Mid][双百解法]面试题36. 二叉搜索树与双向链表
面试题36. 二叉搜索树与双向链表其实就是考察二叉排序树的中序遍历是顺序这一知识点 private Node res; private Node preNode; public Node treeToDoublyList(Node root) { initial(); inOrder(root); return this.res; } private vo...
2020-04-19 14:20:07 217
原创 LeetCode-Algorithms-[Mid][双百解法]面试题35. 复杂链表的复制
面试题35. 复杂链表的复制\1.基础的生成单链表的一个流程。首先生成原始的链表,此阶段只关注.next指针域,并用一个Map记录下对应关系2.生成.random的指针域,用Map记录的对应关系去指示 public Node copyRandomList(Node head) { if (head == null) { return null; } Node ...
2020-04-19 11:28:38 264
原创 LeetCode-Algorithms-[Mid][双百解法]138. 复制带随机指针的链表
138. 复制带随机指针的链表 public Node copyRandomList(Node head) { if (head == null) { return null; } Node resNode = new Node(head.val); Node preNode = resNode; Node headNode = h...
2020-04-19 11:24:30 237
原创 LeetCode-Algorithms-[Mid]113. 路径总和 II
113. 路径总和 II private List<List<Integer>> res; private List<Integer> list; public List<List<Integer>> pathSum(TreeNode root, int sum) { initial(); getPathSum(r...
2020-04-19 10:18:04 178
原创 LeetCode-Algorithms-[Mid]面试题34. 二叉树中和为某一值的路径
面试题34. 二叉树中和为某一值的路径第一版代码:不回溯的做法 private List<List<Integer>> res; int sum; public List<List<Integer>> pathSum(TreeNode root, int sum) { initial(sum); getSumPath(ro...
2020-04-19 10:14:31 204
原创 LeetCode-Algorithms-[Mid]面试题32 - III. 从上到下打印二叉树 III
面试题32 - III. 从上到下打印二叉树 III public List<List<Integer>> levelOrder(TreeNode root) { if (root == null) { return new LinkedList<List<Integer>>(); } Queue<TreeNode&...
2020-04-18 16:05:04 192
原创 LeetCode-Algorithms-[Mid]102. 二叉树的层序遍历
102. 二叉树的层序遍历 public List<List<Integer>> levelOrder(TreeNode root) { if (root == null) { return new LinkedList<List<Integer>>(); } Queue<TreeNode> q = ne...
2020-04-18 09:53:06 169
原创 LeetCode-Algorithms-[Easy]面试题32 - II. 从上到下打印二叉树 II
面试题32 - II. 从上到下打印二叉树 II public List<List<Integer>> levelOrder(TreeNode root) { if (root == null) { return new LinkedList<List<Integer>>(); } Queue<TreeNode> ...
2020-04-18 09:49:14 197
原创 LeetCode-Algorithms-[Mid][树的层次遍历]面试题32 - I. 从上到下打印二叉树
面试题32 - I. 从上到下打印二叉树 public int[] levelOrder(TreeNode root) { if (root == null) { return new int[0]; } Queue<TreeNode> q = new LinkedList<TreeNode>(); Queue<Integer> in...
2020-04-18 08:28:55 195
原创 LeetCode-Algorithms-[Mid]946. 验证栈序列
946. 验证栈序列 public boolean validateStackSequences(int[] pushed, int[] popped) { Stack<Integer> stack = new Stack<Integer>(); int j = 0, n = pushed.length, m = popped.length; f...
2020-04-17 10:13:48 141
原创 LeetCode-Algorithms-[Mid]面试题31. 栈的压入、弹出序列
面试题31. 栈的压入、弹出序列 public boolean validateStackSequences(int[] pushed, int[] popped) { Stack<Integer> stack = new Stack<Integer>(); int j = 0, n = pushed.length, m = popped.length;...
2020-04-17 10:12:49 222
原创 LeetCode-Algorithms-[Easy]155. 最小栈
155. 最小栈 class MinStack { Stack<Integer> s1; Stack<Integer> s2; /** initialize your data structure here. */ public MinStack() { this.s1 = new Stack<Integer>(); th...
2020-04-17 09:41:35 192
原创 LeetCode-Algorithms-[Easy]面试题30. 包含min函数的栈
面试题30. 包含min函数的栈 class MinStack { Stack<Integer> s1; Stack<Integer> s2; /** initialize your data structure here. */ public MinStack() { this.s1 = new Stack<Integer>(...
2020-04-17 09:38:11 202
原创 LeetCode-Algorithms-[Mid]54. 螺旋矩阵
54. 螺旋矩阵public class No54 { private boolean[][] isPrint; private int[][] matrix; private List<Integer> res; private int n; private int m; private int i; private int j; private int c...
2020-04-16 11:06:30 175
原创 LeetCode-Algorithms-[Easy]面试题29. 顺时针打印矩阵
面试题29. 顺时针打印矩阵public class Interview29 { private boolean[][] isPrint; private int[][] matrix; private int[] res; private int n; private int m; private int i; private int j; private int co...
2020-04-16 10:55:54 277
原创 LeetCode-Algorithms-[Easy][树]面试题28. 对称的二叉树
面试题28. 对称的二叉树 public boolean isSymmetric(TreeNode root) { if (root == null) { return true; } return isSym(root.left, root.right); } private boolean isSym(TreeNode leftNode, TreeNode...
2020-04-16 09:43:02 186
原创 LeetCode-Algorithms-[Easy][树基础题]面试题27. 二叉树的镜像
面试题27. 二叉树的镜像 public TreeNode mirrorTree(TreeNode root) { if (root == null) { return null; } TreeNode left = mirrorTree(root.left); TreeNode right = mirrorTree(root.right); TreeNod...
2020-04-16 08:46:48 196
原创 LeetCode-Algorithms-[Mid]面试题26. 树的子结构
面试题26. 树的子结构 public boolean isSubStructure(TreeNode A, TreeNode B) { if (!(A != null && B != null)) { return false; } if (isSub(A, B)) { return true; } if (isSubStructure(A....
2020-04-15 20:51:36 134
原创 LeetCode-Algorithms-[Easy][链表基础题]面试题24. 反转链表
面试题24. 反转链表 public ListNode reverseList(ListNode head) { ListNode crrNode = head; ListNode preNode = null; while (crrNode != null) { ListNode nextNode = crrNode.next; crrNode.next = pre...
2020-04-15 14:06:05 184
原创 LeetCode-Algorithms-[Easy][链表基础题]面试题18. 删除链表的节点
面试题18. 删除链表的节点链表基础题 public ListNode deleteNode(ListNode head, int val) { ListNode temp = head; ListNode pre = null; while (temp != null && temp.val != val) { pre = temp; temp ...
2020-04-14 19:56:14 182
原创 LeetCode-Algorithms-[Easy][水题]面试题17. 打印从1到最大的n位数
面试题17. 打印从1到最大的n位数双百解法 public int[] printNumbers(int n) { final int[] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999, Integer.MAX_VALUE }; int maxNum = sizeTable[...
2020-04-14 18:57:01 188
原创 LeetCode-Algorithms-[Mid]面试题16. 数值的整数次方
面试题16. 数值的整数次方 public double myPow(double x, int n) { if (n == 0) { return 1; } else if (n == 1) { return x; } else if (n == -1) { return 1 / x; } if (n % 2 == 0) { double d =...
2020-04-14 18:49:22 198
原创 LeetCode-Algorithms-[Easy]面试题15. 二进制中1的个数
面试题15. 二进制中1的个数 public int hammingWeight(int n) { int count = 0; while (n != 0) { count += n & 1; n = n >>> 1; } return count; } public int hammingWeight_2(int n) { ...
2020-04-14 15:30:56 185
转载 LeetCode-Algorithms-[Mid]面试题14- II. 剪绳子 II
面试题14- II. 剪绳子 IIJava贪心 思路讲解 public int cuttingRope(int n) { if (n == 3) return 2; if (n == 2) return 1; int mod = 1000000007; long res = 1; while (n > 4) { res *= 3; res...
2020-04-14 15:03:54 184
转载 LeetCode-Algorithms-[Mid]面试题14- I. 剪绳子
面试题14- I. 剪绳子双百详细题解花里胡哨搞了半天。。。结果发现最优解是一个数学问题,而不是一个计算机问题class Solution { public int cuttingRope(int n) { if(n <= 3) return n - 1; int a = n / 3, b = n % 3; if(b ==...
2020-04-14 14:25:32 221
原创 LeetCode-Algorithms-[Mid][DFS]面试题13. 机器人的运动范围
面试题13. 机器人的运动范围 private boolean[][] visited; private int m; private int n; private int k; public int movingCount(int m, int n, int k) { initial(m, n, k); return DFS(0, 0); } private voi...
2020-04-14 11:41:02 197
原创 LeetCode-Algorithms-[Mid][DFS]面试题12. 矩阵中的路径
面试题12. 矩阵中的路径追求易读代码 String word; char[][] board; boolean[][] visited; int n; int m; int l; public boolean exist(char[][] board, String word) { initial(board, word); for (int i = 0; i &...
2020-04-14 10:53:20 227
原创 LeetCode-Algorithms-[Easy]面试题11. 旋转数组的最小数字
面试题11. 旋转数组的最小数字最优解是二分法,题解内有第二种是线性查找的解法第三种,基于恢复原始序列的思想。典型的三次翻转思路 public int minArray(int[] numbers) { int index = 0; int n = numbers.length; while (index < n - 1 && numbers[i...
2020-04-14 09:13:18 194
原创 LeetCode-Algorithms-[Easy]面试题09. 用两个栈实现队列
面试题09. 用两个栈实现队列 class CQueue { private Stack<Integer> s1; private Stack<Integer> s2; public CQueue() { s1 = new Stack<Integer>(); s2 = new Stack<Integer>(); }...
2020-04-13 11:15:27 183
原创 LeetCode-Algorithms-[Mid]面试题07. 重建二叉树
面试题07. 重建二叉树public class Interview07 { private HashMap<Integer, Integer> map; private int n; private int[] pre; public TreeNode buildTree(int[] preorder, int[] inorder) { n = inorder.l...
2020-04-13 10:46:39 190
原创 LeetCode-Algorithms-[Easy]1271. 十六进制魔术数字
1271. 十六进制魔术数字 public String toHexspeak(String num) { long intNum = Long.parseLong(num); String hex = Long.toHexString(intNum).toUpperCase(); hex = hex.replace('0', 'O'); hex = hex.replac...
2020-04-13 10:05:55 233
原创 LeetCode-Algorithms-[Easy]1243. 数组变换
1243. 数组变换 public List<Integer> transformArray(int[] arr) { boolean isChange = false; int n = arr.length; int[] temp = new int[n]; initialTemp(temp, arr, n); while (true) { i...
2020-04-13 10:00:40 209
原创 LeetCode-Algorithms-[Easy]1228. 等差数列中缺失的数字
1228. 等差数列中缺失的数字 public int missingNumber(int[] arr) { int diff1 = arr[1] - arr[0]; int diff2 = arr[2] - arr[1]; int diff = diff1; if (Math.abs(diff1) > Math.abs(diff2)) { diff = di...
2020-04-13 09:47:23 273
原创 LeetCode-Algorithms-[Easy]1213. 三个有序数组的交集
1213. 三个有序数组的交集 public List<Integer> arraysIntersection(int[] arr1, int[] arr2, int[] arr3) { List<Integer> res = new LinkedList<Integer>(); int i = 0, j = 0, z = 0; int l...
2020-04-12 15:48:07 205
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人