自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

原创 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 244

原创 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 384

原创 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 246 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 180 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 387

原创 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 211

原创 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 138 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 150

原创 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 266

原创 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 148

原创 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 162

原创 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 179

原创 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 210

原创 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 &amp

2021-02-08 00:37:16 166 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 89

原创 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 110

原创 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 132

原创 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 213

原创 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 121

原创 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 143

原创 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 129

原创 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 106

原创 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 79

原创 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 160

原创 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 110

原创 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 254

原创 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 119

原创 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 132

原创 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 188

原创 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 122

原创 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 136

原创 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 125

原创 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 142

原创 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 116

原创 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 185

原创 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 135

原创 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 108

原创 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 122

原创 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 112

原创 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 142

空空如也

空空如也

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

TA关注的人

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