Leetcode
GoodJobJasper
这个作者很懒,什么都没留下…
展开
-
Leetcode 133. Clone Graph
方法1: dfs。这是一道graph题,我第一感觉是要用recursion来做的。我离正确答案离的很近,最近好久没做题,有点生疏,如果是之前手感,做出来应该没问题。我没做出来主要是我使用了set而不是map,在怎样避免操作重复node上面我理解的不对。还是那句话,多联系吧,这题不难,应该拿下的。复杂度复盘看。/*// Definition for a Node.class Node { public int val; public List<Node> neighbors.原创 2021-02-15 09:12:58 · 153 阅读 · 0 评论 -
Leetcode 269. Alien Dictionary
方法1: topological sort + bfs。这道题和210题思路一模一样,都是graph题,拓扑排序问题。一般来说拓扑排序问题可以分为以下三个步骤:graph题里面还有一个很重要的点就是detect circle,这道题目关于这个点我还没搞得很清楚,复盘的时候记得搞清楚。这边我建议仔细阅读lc官方解答1,这个解答吧这个问题解释地非常清楚。class Solution { public String alienOrder(String[] words) { Map&l.原创 2021-02-15 07:53:20 · 287 阅读 · 0 评论 -
Leetcode 288. Unique Word Abbreviation
方法: hashtable。class ValidWordAbbr { private Set<String> set = new HashSet<>(); private Map<String, Integer> map = new HashMap<>(); public ValidWordAbbr(String[] dictionary) { for(String s : dictionary) .原创 2021-02-10 07:12:36 · 168 阅读 · 1 评论 -
Leetcode 284. Peeking Iterator
方法1: 这道题目我直接看的答案,因为对于java的iterator不熟。这道题目的关键就在于要保存顶部的元素,那我们方法1就是保存peek出来的元素。// Java Iterator interface reference:// https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.htmlimport java.util.NoSuchElementException;class PeekingIterator implemen.原创 2021-02-10 06:28:38 · 157 阅读 · 1 评论 -
Leetcode 281. Zigzag Iterator
方法1: 这道题目其实251题的简易版,所以自然方法是一模一样的。方法1是将两个链表整合成1个链表。这是最简单容易想到的方法。public class ZigzagIterator { List<Integer> list = new ArrayList<>(); int count = 0; public ZigzagIterator(List<Integer> v1, List<Integer> v2) { .原创 2021-02-10 05:47:32 · 288 阅读 · 0 评论 -
Leetcode 261. Graph Valid Tree
方法1: 我最近真的变菜了,很简单的dfs都做不出来。这边我是看了lc官方解答1,但是它是用iteration来实现dfs的,而我想用recursion来实现。我的代码参考的是这篇帖子。复杂度之后分析吧。class Solution { public boolean validTree(int n, int[][] edges) { if(edges.length != n - 1) return false; if(edges.length == 0) retu.原创 2021-02-10 04:54:36 · 176 阅读 · 0 评论 -
Leetcode 146. LRU Cache
方法1: 这道题目我直接看的答案,因为我知道我不会做。方法1是利用LinkedHashMap这个类,来实现一个ordered map,这属于投机的方法,这不是面试时候应该出现的答案,所以这边仅供参考。时间复杂1,空间复杂capacity。class LRUCache extends LinkedHashMap<Integer, Integer>{ private int capacity; public LRUCache(int capacity) { .原创 2021-02-09 09:03:22 · 113 阅读 · 1 评论 -
Leetcode 251. Flatten 2D Vector
方法1: 其实我自己的方法和lc官方解答2的思路是一样的,但是我实现的非常的复杂,很多edge case。基本思路是track当前处于matrix的什么位置,我是用pair,lc使用两个变量实现的,基本是一样的。具体思路请看lc官方解答2,写得很好。下面展示我的代码,以及lc官方解答2的代码:// 我的class Vector2D { int[][] arr; Pair<Integer, Integer> curr; boolean flag = true; .原创 2021-02-09 06:53:27 · 132 阅读 · 0 评论 -
Leetcode 211. Design Add and Search Words Data Structure
方法1: 和208题一样,都是trie数据结构。不是很难,直接上代码。class TrieNode { public char val; public boolean isEnd; public TrieNode[] children = new TrieNode[26]; public TrieNode(){} TrieNode(char c){ this.val = c; TrieNode node = new TrieNode.原创 2021-02-09 04:46:25 · 213 阅读 · 0 评论 -
Leetcode 208. Implement Trie (Prefix Tree)
方法1: hashset。这是我能想到的方法。class Trie { Set<String> set = new HashSet<>(); /** Initialize your data structure here. */ public Trie() { } /** Inserts a word into the trie. */ public void insert(String word) {.原创 2021-02-09 02:37:46 · 130 阅读 · 0 评论 -
Leetcode 207. Course Schedule
方法1: dfs/backtracking + memo 这题其实是一道circle detection的问题。这个思想和lc官方解答2一样,具体思路可以查看lc官方解答2。复杂度复盘时候自己分析。class Solution { Map<Integer, List<Integer>> map = new HashMap<>(); public boolean canFinish(int numCourses, int[][] prerequisite.原创 2021-02-09 01:41:14 · 139 阅读 · 0 评论 -
Leetcode 170. Two Sum III - Data structure design
方法1: 这道题目其实和two sum基本一样。这道题目难点就在于找到两个数的sum为一个给定的值。方法1我们用sorted list & two pointers来做。时间复杂nlogn,空间复杂n。具体思路参考lc官方解答1.class TwoSum { List<Integer> list = new ArrayList<>(); boolean is_sorted = false; /** Initialize your data stru.原创 2021-02-08 05:00:29 · 140 阅读 · 0 评论 -
Leetcode 201. Bitwise AND of Numbers Range
方法1: 最简单的方法就是iterate一下,但是会tle。所以这边我介绍两种方法。其实这道题目我们翻译一下就是找到两个数字的commom prefix,为什么是找common prefix请看lc官方解释。所以我们同时右移两个数字,直到相同为止。时间复杂1,空间复杂1.class Solution { public int rangeBitwiseAnd(int m, int n) { int count = 0; while(m != n){ .原创 2021-02-08 01:10:22 · 183 阅读 · 0 评论 -
Leetcode 191. Number of 1 Bits
网络问题,无法上传图片方法1: 这道题目和190题思路一样,所以方法1我采用的就是190题的方法。时间复杂1,空间复杂1.public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int n) { int res = 0; for(int i = 0; i < 32; i++){ if((n &原创 2021-02-08 00:37:16 · 146 阅读 · 1 评论 -
Leetcode 190. Reverse Bits
方法1: lc官方解答1。follow up没看。public class Solution { // you need treat n as an unsigned value public int reverseBits(int n) { int ans = 0; for (int i = 0; i < 32; i++) { ans <<= 1; ans = ans | (n &.原创 2021-02-07 11:00:53 · 69 阅读 · 0 评论 -
Leetcode 89. Gray Code
方法1: 这道题目我只推荐这个方法1。其实就是找规律,详细解释看这篇帖子。bit操作真的是一点也不熟悉,快把我整疯了。class Solution { public List<Integer> grayCode(int n) { List<Integer> gray = new ArrayList<Integer>(); gray.add(0); //初始化 n = 0 的解 for (int i = 0; i .原创 2021-02-07 08:47:29 · 92 阅读 · 0 评论 -
Leetcode 132. Palindrome Partitioning II
方法1: dfs + memo。比较简单,不过多赘述。class Solution { Map<String, Integer> map = new HashMap<>(); public int minCut(String s) { if(isPalindrome(s, s.length() - 1) || s.length() == 0 || s.length() == 1) return 0; if(map.containsK.原创 2021-02-07 07:04:12 · 109 阅读 · 0 评论 -
Leetcode 10. Regular Expression Matching
方法1: dp。我发誓,我再也不做dp hard题目了,如果做,我只看10分钟,想不出来直接看答案,真的是在浪费时间。这道题直接看这个链接,或者看lc官方解答2也行。代码是抄discussion区最高赞的答案。class Solution { public boolean isMatch(String s, String p) { if (s == null || p == null) { return false; } .原创 2021-02-07 05:31:54 · 152 阅读 · 0 评论 -
Leetcode 44. Wildcard Matching
方法1: recursion + memo。class Solution { Map<Pair<Integer, Integer>, Boolean> map = new HashMap<>(); public boolean isMatch(String s, String p) { return dfs(s, p, 0, 0); } public boolean dfs(String s, String p.原创 2021-02-07 01:44:13 · 98 阅读 · 0 评论 -
Leetcode 152. Maximum Product Subarray
方法1: 这道题目还挺难的,是53题的变种。具体思路直接参考lc官方解答2。时间复杂n,空间复杂1.class Solution { public int maxProduct(int[] nums) { if (nums.length == 0) return 0; int max_so_far = nums[0]; int min_so_far = nums[0]; int result = max_so_far; .原创 2021-02-02 06:22:46 · 124 阅读 · 0 评论 -
Leetcode 53. Maximum Subarray
方法1: greedy + recursion。这是我在discussion区域找到的一个比较好的办法。时间复杂m,m为input数组长度,空间复杂1。class Solution { int largestSum = Integer.MIN_VALUE; public int maxSubArray(int[] nums) { if(nums.length == 1) return nums[0]; if(nums.length .原创 2021-02-02 03:11:14 · 109 阅读 · 0 评论 -
Leetcode 213. House Robber II
方法1: 这道题是198的变种,其实改变的也很少,只是加了一个条件头尾项链。但是这就把我难住了。其实很简单,具体思路参照lc官方解答1,非常的简单明了。时间复杂m,空间复杂m。m为input数组的size。class Solution { public int rob(int[] nums) { int len = nums.length; if(len == 0) return 0; if(len == 1) return nums[0]; .原创 2021-02-02 01:10:28 · 81 阅读 · 0 评论 -
Leetcode 198. House Robber
方法1: dp。非常明显的dp问题,其实也可以dfs+memo来做,但是我这边直接一步到位了。时间复杂m,空间复杂m,m为input数组长度。当然空间复杂度可以优化成1,直接在原数组上修改,复盘时候自己去实现。class Solution { public int rob(int[] nums) { if(nums.length == 0) return 0; int[] dp = new int[nums.length + 1]; dp[0] =.原创 2021-02-02 00:19:06 · 61 阅读 · 0 评论 -
Leetcode 276. Paint Fence
方法1: 这道题还是挺不错的,然后我没做出来,但是我已经很接近答案了,还是很可惜的。这道题有很多种dp的做法,我只看了其中的一种,是lc官方解答2,复盘的时候有时间可以把其他额方法都看一遍。这个方法时间复杂n,空间复杂n。class Solution { public int numWays(int n, int k) { if(n == 0) return 0; int[][] dp = new int[n + 1][2]; d.原创 2021-01-31 05:48:09 · 124 阅读 · 0 评论 -
Leetcode 85. Maximal Rectangle
方法1: 这道题目太难了,超出了我的能力。这道题本应该是dp,但是dp好像很烦,我都没去看。但是我看到了一个还不错的解答,就是把这道题转换为第84题。下面我给出链接,看这个人的评论下面展示这种解法的代码,复杂度没分析,没精力了。复盘的时候自己分析复杂度,并且要把dp解答也弄明白。class Solution { public int maximalRectangle(char[][] matrix) { if(matrix.length == 0) return 0; .原创 2021-01-30 08:32:46 · 87 阅读 · 0 评论 -
Leetcode 221. Maximal Square
方法1: 二维dp。思路是这个这道题目我的想法和答案的区别就在于我认为dp[i][j]表示直到i,j,grid中最大正方形的边长,而答案的意思是dp[i][j]表示正方形的右下脚一定要顶着i,j。我一开始以为他这样做不能遍历所有的正方形区域,但是其实这样是可以的。我觉得这道题不是很难,我应该是要做出来的,但是很可惜,没做出来,dp题还需要多多练习啊。时间复杂mn,空间复杂mn。class Solution { public int maximalSquare(char[][] matrix) .原创 2021-01-30 07:35:41 · 143 阅读 · 0 评论 -
Leetcode 174. Dungeon Game
方法1: 这道题应该是可以用backtracking来做的,但是我还是直接去尝试了dp。dp思路其实也不难,但是很可惜我想反了。我的想法是top-down,但是其实必须得bottom-up。但是我估计用backtracking的话top-down是可以的。anyway,这道题还是很不错的题目,方法1时间复杂mn,空间复杂mn。然后方法1的空间复杂可以优化,用一维dp数组就行,但是我现在不要求自己去看,先把二维给整熟练再说吧。具体思路直接看lc官方解答1.class Solution { int in.原创 2021-01-30 06:32:36 · 104 阅读 · 0 评论 -
Leetcode 72. Edit Distance
方法1: 这道题目很明显不适合用recursion来做,情况将会非常的复杂。所以想到要用dp。我在找dp的关系式的时候败下阵来,其实我已经发现了dp[i][j] 与dp[i-1][j], dp[i][j-1], dp[i-1][j-1] 这三者有关系了,但是还是没能明确的分析出来是什么关系。这边我给出一个链接,这个帖子详细阐述了关系是什么。这个帖子里面还有这个方法的优化,优化成一维dp数组,减小了时间复杂,但是我现在不要求自己去掌握,我觉得要先把最基础的给掌握了。最后,方法1时间复杂mn,空间复杂mn.原创 2021-01-30 03:32:04 · 110 阅读 · 0 评论 -
Leetcode 97. Interleaving String
方法1: dfs+memo。时间复杂mn,空间复杂mn。class Solution { Map<Pair<String, String>, Boolean> map = new HashMap<>(); public boolean isInterleave(String s1, String s2, String s3) { int len1 = s1.length(); int len2 = s2.length().原创 2021-01-30 02:13:05 · 131 阅读 · 0 评论 -
Leetcode 265. Paint House II
方法1: 和道题和256题基本一模一样,只是颜色的数量不是固定的了。所以256的方法基本都是用于这道题目。方法1是dp,时间复杂n*k^2,n为房子数,k为颜色数,时间复杂1。这个做法会改变原数组。class Solution { public int minCostII(int[][] costs) { if(costs.length == 0) return 0; for(int i = 1; i < costs.length; i++){ .原创 2021-01-29 10:19:58 · 106 阅读 · 0 评论 -
Leetcode 256. Paint House
方法1: dfs + memoization。时间复杂n,空间复杂n,n为房屋数量。class Solution { int red = -1; int blue = 0; int green = 1; Map<Pair<Integer, Integer>, Integer> map = new HashMap<>(); public int minCost(int[][] costs) { if(costs.l.原创 2021-01-29 08:32:53 · 114 阅读 · 0 评论 -
Leetcode 279. Perfect Squares
方法1: dfs+memoization。复杂度我不太会分析。留给以后分析吧。class Solution { Map<Integer, Integer> map = new HashMap<>(); public int numSquares(int n) { if(n == 0) return 0; if(n == 1) return 1; if(map.containsKey(n)) return map.ge.原创 2021-01-29 07:01:20 · 100 阅读 · 0 评论 -
Leetcode 64. Minimum Path Sum
方法1: dfs+memo。时间复杂mn,空间复杂mn。这个复杂度我不确定分析的对不对。class Solution { int m; int n; int[][] grids; Map<Pair<Integer, Integer>, Integer> map = new HashMap<>(); public int minPathSum(int[][] grid) { this.m = grid.length.原创 2021-01-29 06:51:42 · 119 阅读 · 0 评论 -
Leetcode 120. Triangle
网络问题,图片无法上传方法1: dfs,can’t memo,cause tle。时间复杂2n,n为node数。空间复杂1.class Solution { List<List<Integer>> triangle; int min = Integer.MAX_VALUE; public int minimumTotal(List<List<Integer>> triangles) { this.triangle =原创 2021-01-29 06:41:53 · 96 阅读 · 0 评论 -
Leetcode 63. Unique Paths II
方法1: 这个很自然第一个想到的肯定是backtracking/dfs。我一开始是想就套用backtracking模版来写的,但是好像不是特别合适,感觉直接dfs就可以了。但是一定要用那个backtracking模版来做的话也可以,可以看下这个链接。下面我直接展示dfs的做法。时间复杂mn,空间复杂mn。class Solution { int m; int n; Map<Pair<Integer, Integer>, Integer> map = new.原创 2021-01-28 06:33:47 · 134 阅读 · 0 评论 -
Leetcode 62. Unique Paths
方法1: recursion+memoization。时间复杂mn,空间复杂mn。class Solution { Map<Pair<Integer, Integer>, Integer> map = new HashMap<>(); public int uniquePaths(int m, int n) { if(m < 1 || n < 1) return 0; if(m == 1 &&.原创 2021-01-28 04:43:07 · 121 阅读 · 0 评论 -
Leetcode 70. Climbing Stairs
方法1: 这道题很明显是dp,因为要找规律。但是我们还是一步一步来做,来优化。第一想到的肯定是recursion。但是会tle。然后想到了用memoization来优化。时间复杂n,空间复杂n。class Solution { Map<Integer, Integer> map = new HashMap<>(); public int climbStairs(int n) { if(map.containsKey(n)) return map.g.原创 2021-01-28 02:18:22 · 87 阅读 · 0 评论 -
Leetcode 218. The Skyline Problem
方法1: 这道题目目前是超过了我的能力范围的,不论是在数据结构的使用还是逻辑的理解上来说都是比较难的。这边我建议复盘的时候看这个视频,讲得很清楚,然后看完了,自己写一遍,现在我自己是没去实现的,因为感觉太难了,做了意义不大。然后记得还要去分析复杂度。class Solution { private class Point implements Comparable<Point>{ private int x; private int height; .原创 2021-01-28 01:46:00 · 98 阅读 · 0 评论 -
Leetcode 215. Kth Largest Element in an Array
方法1: sort。时间复杂nlogn,空间复杂1.class Solution { public int findKthLargest(int[] nums, int k) { Arrays.sort(nums); return nums[nums.length-k]; }}方法2: heap。时间复杂nlogk,空间复杂k。class Solution { public int findKthLargest(int[] nums, in.原创 2021-01-28 00:49:26 · 96 阅读 · 0 评论 -
Leetcode 227. Basic Calculator II
方法1: 这道题相对于224来说稍微容易一点,因为不用考虑括号配对问题。思路大致也是使用一个stack,遍历整个string,如果遇到乘除法,直接把答案算出来,然后直接push进stack,整个过程中stack中只保存数字,这些数字最后全部加起来就是答案。具体解释直接看lc官方解答1,写得不错。时间复杂n,空间复杂n。class Solution { public int calculate(String s) { Stack<Integer> stack = new.原创 2021-01-27 12:08:34 · 109 阅读 · 0 评论