![](https://img-blog.csdnimg.cn/20201014180756780.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode刷题记录
as1490047935
这个作者很懒,什么都没留下…
展开
-
238. 除自身以外数组的乘积
class Solution { public int[] productExceptSelf(int[] nums) { int length = nums.length; int[] answer = new int[length]; // answer[i] 表示索引 i 左侧所有元素的乘积 // 因为索引为 '0' 的元素左侧没有元素, 所以 answer[0] = 1 answer[0] = 1;转载 2021-08-24 16:58:05 · 61 阅读 · 0 评论 -
236. 二叉树的最近公共祖先
class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root == null || root == p || root == q) return root; TreeNode left = lowestCommonAncestor(root.left, p, q); TreeNode right = lowes转载 2021-08-24 16:56:33 · 53 阅读 · 0 评论 -
234. 回文链表
public boolean isPalindrome(ListNode head) { ListNode fast = head, slow = head; //通过快慢指针找到中点 while (fast != null && fast.next != null) { fast = fast.next.next; slow = slow.next; } //如果fast不为空,说明链表的长度是奇数个 if (转载 2021-08-24 16:43:03 · 51 阅读 · 0 评论 -
221. 最大正方形
题解class Solution { public int maximalSquare(char[][] matrix) { int m = matrix.length , n = matrix[0].length , max = 0; int [][] dp = new int[m][n]; for(int i = 0 ; i < m ; i++){ for(int j = 0; j < n ;j++){ ..转载 2021-08-22 17:12:21 · 74 阅读 · 0 评论 -
215. 数组中的第K个最大元素
选择排序 //selectSort 每次将当前元素替换为后面最小的元素 public static void selectSort(int[] a){ int N = a.length; for (int i = 0; i < N; i++) { int min = i; for (int j = i+1; j < N; j++) { if(a[j]<a[min]).转载 2021-08-22 16:45:43 · 68 阅读 · 0 评论 -
208. 实现 Trie (前缀树)
题解比较详细的图解class TrieNode { // 节点 boolean isWord; TrieNode[] children = new TrieNode[26]; }class Trie { TrieNode root; // 根节点 public Trie() { root = new TrieNode(); // 构造字典树,就是先构造出一个空的根节点 } //【向字典树插入单词w..转载 2021-08-22 16:07:08 · 58 阅读 · 0 评论 -
课程表
方法一:入度表(广度优先遍历)class Solution { public boolean canFinish(int numCourses, int[][] prerequisites) { int[] indegrees = new int[numCourses]; List<List<Integer>> adjacency = new ArrayList<>(); Queue<Integer>.转载 2021-04-27 16:26:49 · 84 阅读 · 0 评论 -
课程表
class Solution { public boolean canFinish(int numCourses, int[][] prerequisites) { int[] indegrees = new int[numCourses]; List<List<Integer>> adjacency = new ArrayList<>(); Queue<Integer> queue = new Lin.转载 2021-04-27 15:55:50 · 74 阅读 · 0 评论 -
反转链表
方法一:迭代class Solution { public ListNode reverseList(ListNode head) { ListNode prev = null; ListNode curr = head; while (curr != null) { ListNode next = curr.next; curr.next = prev; prev = c...转载 2021-04-27 15:05:43 · 53 阅读 · 0 评论 -
岛屿数量
class Solution { public int numIslands(char[][] grid) { int count = 0; for(int i = 0; i < grid.length; i++) { for(int j = 0; j < grid[0].length; j++) { if(grid[i][j] == '1'){ dfs(g..转载 2021-04-27 14:44:52 · 50 阅读 · 0 评论 -
打家劫舍
class Solution { public int rob(int[] nums) { if (nums == null || nums.length == 0) { return 0; } int length = nums.length; if (length == 1) { return nums[0]; } int[] dp = new int.转载 2021-04-27 14:26:31 · 54 阅读 · 0 评论 -
多数元素
class Solution { public int majorityElement(int[] nums) { Map<Integer, Integer> counter = new HashMap<>(); // 遍历每个数统计次数 for (int num: nums) { counter.put(num, counter.getOrDefault(num, 0) + 1); .转载 2021-04-27 13:17:12 · 76 阅读 · 0 评论 -
相交链表
题解public ListNode getIntersectionNode(ListNode headA, ListNode headB) { if (headA == null || headB == null) return null; ListNode pA = headA, pB = headB; while (pA != pB) { pA = pA == null ? headB : pA.next; pB = pB == nul...转载 2021-04-26 16:54:28 · 46 阅读 · 0 评论 -
最小栈
题解解法一class MinStack { /** initialize your data structure here. */ private Stack<Integer> stack; private Stack<Integer> minStack; public MinStack() { stack = new Stack<>(); minStack = new Stack<>().转载 2021-04-26 16:38:46 · 55 阅读 · 0 评论 -
乘积最大数组
题解解法一 动态规划public int maxProduct(int[] nums) { int n = nums.length; if (n == 0) { return 0; } int[] dpMax = new int[n]; dpMax[0] = nums[0]; int[] dpMin = new int[n]; dpMin[0] = nums[0]; int max = nums[0]; for .转载 2021-04-26 16:19:20 · 111 阅读 · 0 评论 -
排序链表
题解递归/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next;转载 2021-04-17 21:31:55 · 47 阅读 · 0 评论 -
LRU缓存机制
题解class LRUCache extends LinkedHashMap<Integer, Integer>{ private int capacity; public LRUCache(int capacity) { super(capacity, 0.75F, true); this.capacity = capacity; } public int get(int key) { retur..转载 2021-04-17 20:32:44 · 55 阅读 · 0 评论 -
环形链表 II
Carl大神题解public class Solution { public ListNode detectCycle(ListNode head) { ListNode fast = head; ListNode slow = head; while(fast != null && fast.next != null) { slow = slow.next; fast = fas...转载 2021-04-17 19:47:30 · 66 阅读 · 0 评论 -
环形链表
方法一:哈希表public class Solution { public boolean hasCycle(ListNode head) { Set<ListNode> seen = new HashSet<ListNode>(); while (head != null) { if (!seen.add(head)) { return true; } ..转载 2021-04-17 19:17:09 · 46 阅读 · 0 评论 -
单词拆分
public class Solution { public boolean wordBreak(String s, List<String> wordDict) { Set<String> wordDictSet = new HashSet(wordDict); boolean[] dp = new boolean[s.length() + 1]; dp[0] = true; for (int i = 1; .转载 2021-04-17 19:04:40 · 58 阅读 · 0 评论 -
只出现一次的数字
class Solution { public int singleNumber(int[] nums) { int single = 0; for (int num : nums) { single ^= num; } return single; }}转载 2021-04-16 21:34:25 · 55 阅读 · 0 评论 -
最长连续序列
题解暴力解法class Solution { public int longestConsecutive(int[] nums) { if(nums.length==0||nums==null){ return 0; } Arrays.sort(nums); int len=nums.length; int count = 1; int res=0; for(.转载 2021-04-16 21:26:00 · 56 阅读 · 0 评论 -
二叉树中的最大路径和
大佬题解/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { int res = Integer.MIN_VALUE; public int maxPathS...转载 2021-04-16 20:09:00 · 82 阅读 · 0 评论 -
买卖股票的最佳时机
weiwei大佬题解方法一:暴力解法public class Solution { public int maxProfit(int[] prices) { int len = prices.length; if (len < 2) { return 0; } // 有可能不发生交易,因此结果集的初始值设置为 0 int res = 0; // 枚举所有发生一次交.转载 2021-04-16 19:47:04 · 50 阅读 · 0 评论 -
二叉树展开为链表
题解public void flatten(TreeNode root) { while (root != null) { //左子树为 null,直接考虑下一个节点 if (root.left == null) { root = root.right; } else { // 找左子树最右边的节点 TreeNode pre = root.left; .转载 2021-04-16 19:12:06 · 59 阅读 · 0 评论 -
从前序与中序遍历序列构造二叉树
weiwei大佬题解import javax.management.RuntimeErrorException;import java.util.HashMap;import java.util.Map;class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) {val = x;}}public class Solution { public TreeNode..转载 2021-04-16 10:52:41 · 80 阅读 · 0 评论 -
二叉树的最大深度
方法一:深度优先搜索class Solution { public int maxDepth(TreeNode root) { if (root == null) { return 0; } else { int leftHeight = maxDepth(root.left); int rightHeight = maxDepth(root.right); retur.转载 2021-04-15 21:18:51 · 66 阅读 · 0 评论 -
二叉树的层序遍历
不错的题解,图文并茂,易于理解public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); Queue<TreeNode> queue = new ArrayDeque<>(); if (root != null) { queue.add(ro.转载 2021-04-15 20:53:26 · 68 阅读 · 0 评论 -
对称二叉树
甜姨题解方法一:递归class Solution { public boolean isSymmetric(TreeNode root) { if (root == null) { return true; } // 比较 root.left 与 root.right 这两棵子树是否对称。 return cmp(root.left, root.right); } private boole.转载 2021-04-15 20:29:14 · 51 阅读 · 0 评论 -
验证二叉搜索树
甜姨解法class Solution { long pre = Long.MIN_VALUE; public boolean isValidBST(TreeNode root) { if (root == null) { return true; } // 访问左子树 if (!isValidBST(root.left)) { return false; }..转载 2021-04-15 20:13:57 · 56 阅读 · 0 评论 -
不同的二叉搜索树
题解class Solution { public int numTrees(int n) { int[] dp = new int[n + 1]; dp[0] = 1; dp[1] = 1; for (int i = 2; i <= n; i++) { for (int j = 0; j < i; j++) { dp[i] += dp[j] * dp[i - j..转载 2021-04-15 19:42:00 · 49 阅读 · 0 评论 -
二叉树的中序遍历
一题多解方法一:递归class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<Integer>(); inorder(root, res); return res; } public void inorder(TreeNode roo...转载 2021-04-14 17:54:26 · 108 阅读 · 0 评论 -
最大矩形
class Solution { public int maximalRectangle(char[][] matrix) { if (matrix.length == 0 || matrix[0].length == 0) { return 0; } int m = matrix.length; int n = matrix[0].length; int res = 0; /..转载 2021-04-14 16:45:16 · 58 阅读 · 0 评论 -
柱状图中最大的矩形
甜姨题解class Solution { public int largestRectangleArea(int[] heights) { // 这里为了代码简便,在柱体数组的头和尾加了两个高度为 0 的柱体。 int[] tmp = new int[heights.length + 2]; System.arraycopy(heights, 0, tmp, 1, heights.length); Deque&l.转载 2021-04-14 12:26:00 · 63 阅读 · 0 评论 -
单词搜索
视频讲解class Solution { public boolean exist(char[][] board, String word) { int n = board.length; int m = board[0].length; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (dfs(b...转载 2021-04-14 11:44:39 · 58 阅读 · 0 评论 -
子集
参考题解参考视频class Solution { public List<List<Integer>> subsets(int[] nums) { List<Integer> cur = new ArrayList(); List<List<Integer>> res = new ArrayList(); dfs(nums, 0, cur, res); return re.转载 2021-04-14 10:57:52 · 76 阅读 · 0 评论 -
最小覆盖子串
参考题解public static String minWindow(String s, String t) { if (s == null || s == "" || t == null || t == "" || s.length() < t.length()) { return ""; } //用来统计t中每个字符出现次数 int[] needs = new int[128]; //用来统.转载 2021-04-14 10:25:11 · 60 阅读 · 0 评论 -
颜色分类
class Solution { public void sortColors(int[] nums) { int zero = 0; int two = nums.length - 1; int i = 0; while( i <= two) { if (nums[i] == 0) { swap(nums, zero, i); zero+..转载 2021-04-13 21:39:30 · 84 阅读 · 0 评论 -
编辑距离
class Solution { public int minDistance(String word1, String word2) { int n1 = word1.length(); int n2 = word2.length(); int[][] dp = new int[n1 + 1][n2 + 1]; // 第一行 for (int j = 1; j <= n2; j++) dp[0][j] = d..转载 2021-04-13 20:52:42 · 50 阅读 · 0 评论 -
爬楼梯
方法一:动态规划class Solution { public int climbStairs(int n) { int p = 0, q = 0, r = 1; for (int i = 1; i <= n; ++i) { p = q; q = r; r = p + q; } return r; }}方法三:通项公式public c.转载 2021-04-13 20:27:51 · 733 阅读 · 0 评论