249. Group Shifted Strings
摘要:The key point of this problem is to find "key" of every words. Time complexity: O(n) How to find key of every string, we make every key as a string wi
阅读全文
posted @
2022-02-25 11:15
阳光明媚的菲越
阅读(30)
推荐(0)
523. Continuous Subarray Sum
摘要:This problem requires add continues numbers, so the best solution is using prefix sum. Time complexity: O(n). class Solution { public boolean checkSub
阅读全文
posted @
2022-02-25 10:42
阳光明媚的菲越
阅读(31)
推荐(0)
382. Linked List Random Node
摘要:This is a similar problem with 398. Random Pick Index The following is my solution, which is easy to undersand: class Solution { int count = 0; Random
阅读全文
posted @
2022-02-25 08:35
阳光明媚的菲越
阅读(37)
推荐(0)
398. Random Pick Index
摘要:This is a similar problem with: 382. Linked List Random Node The following is my solution, which is easy to understand: class Solution { Map<Integer,L
阅读全文
posted @
2022-02-25 08:19
阳光明媚的菲越
阅读(32)
推荐(0)
389. Find the Difference
摘要:Because the character can be duplicated, so we cannot use HashSet. This is easy if using bucket: public char findTheDifference(String s, String t) { i
阅读全文
posted @
2022-02-25 06:26
阳光明媚的菲越
阅读(25)
推荐(0)
4. Median of Two Sorted Arrays
摘要:This problem can be solved by using two PriorityQueue(s), which is just the same solution as 295. Find Median from Data Stream. PriorityQueue<Integer>
阅读全文
posted @
2022-02-25 03:41
阳光明媚的菲越
阅读(21)
推荐(0)
88. Merge Sorted Array
摘要:So easy! public void merge(int[] nums1, int m, int[] nums2, int n) { int i = m-1, j = n-1, index = nums1.length-1; while(i>=0 && j>=0){ if(nums2[j]>=n
阅读全文
posted @
2022-02-24 11:25
阳光明媚的菲越
阅读(26)
推荐(0)
1859. Sorting the Sentence
摘要:This problem can be solved by bicket sorting. Although the problem only requires the number is 1~9, but my solution can be extended to any number sent
阅读全文
posted @
2022-02-15 05:09
阳光明媚的菲越
阅读(39)
推荐(0)
1290. Convert Binary Number in a Linked List to Integer
摘要:This is a super easy problem. Time Compexity O(n), Space Complexity O(n) int res = 0; public int getDecimalValue(ListNode head) { if(head==null) retur
阅读全文
posted @
2022-02-15 05:02
阳光明媚的菲越
阅读(30)
推荐(0)
605. Can Place Flowers
摘要:This problem has three kinds of situations: 1. [0,0,1,1,1,] -> 0s start from i=0, the plant count = zeroNumber/2. 2.[1,0,0,0,1] -> 0s are in between 1
阅读全文
posted @
2022-02-10 09:47
阳光明媚的菲越
阅读(33)
推荐(0)
658. Find K Closest Elements
摘要:My PriorityQueue solution, the time complexity is O(n+klog(k)) = O(nlog(n)). class IndexDistance{ int index; int distance; public IndexDistance(int a,
阅读全文
posted @
2022-02-10 09:13
阳光明媚的菲越
阅读(32)
推荐(0)
199. Binary Tree Right Side View
摘要:The problem of this solution can be BFS and DFS. BFS is easy to understand. The following is DFS solution: private List<Integer> res = new ArrayList<>
阅读全文
posted @
2022-02-09 07:57
阳光明媚的菲越
阅读(27)
推荐(0)
215. Kth Largest Element in an Array
摘要:The first solution, the easiest one, time complexity: O(nlog(n)) public int findKthLargest(int[] nums, int k) { Arrays.sort(nums); if(k<=nums.length)
阅读全文
posted @
2022-02-09 07:39
阳光明媚的菲越
阅读(31)
推荐(0)
227. Basic Calculator II
摘要:For this problem, we must know the sign just before the num. For example, s = "3+2*2", when we deal with the first 2, we must know '+' is just before
阅读全文
posted @
2022-02-09 02:27
阳光明媚的菲越
阅读(32)
推荐(0)
339. Nested List Weight Sum
摘要:Just treat the Nested List as a tree, and dfs it: private int sum =0; public int depthSum(List<NestedInteger> nestedList) { helper(nestedList, 1); ret
阅读全文
posted @
2022-02-08 14:37
阳光明媚的菲越
阅读(25)
推荐(0)
973. K Closest Points to Origin
摘要:The simplest way that I can think to solve this problem is using a PriorityQueue: public int[][] kClosest(int[][] points, int k) { PriorityQueue<int[]
阅读全文
posted @
2022-02-08 14:10
阳光明媚的菲越
阅读(28)
推荐(0)
528. Random Pick with Weight
摘要:For example, if the int[] w ={3,14,1,7}, we can make a presum array based on it: int[] presums = {3, 17, 18, 25} We using Ramdom's nextInt() to genera
阅读全文
posted @
2022-02-08 13:46
阳光明媚的菲越
阅读(29)
推荐(0)
235. Lowest Common Ancestor of a Binary Search Tree
摘要:This problem can be solved by using the solution of 236. Lowest Common Ancestor of a Binary Tree. While the tree here is a binary search tree, so the
阅读全文
posted @
2022-02-08 09:44
阳光明媚的菲越
阅读(39)
推荐(0)
1676. Lowest Common Ancestor of a Binary Tree IV
摘要:This problem is just as same as "235. Lowest Common Ancestor of a Binary Search Tree", the only difference is, 235 is two points, 1676 is 1~n points.
阅读全文
posted @
2022-02-08 08:55
阳光明媚的菲越
阅读(26)
推荐(0)
1650. Lowest Common Ancestor of a Binary Tree III
摘要:The first solution of this problem can be based on the 236. Lowest Common Ancestor of a Binary Tree too: public Node lowestCommonAncestor(Node p, Node
阅读全文
posted @
2022-02-08 08:40
阳光明媚的菲越
阅读(49)
推荐(0)
1644. Lowest Common Ancestor of a Binary Tree II
摘要:When we get this problem, we need to confirm the following 2 questions: 1. Can root, p or q be null? (No) 2. Are both p and q in the tree (No, either
阅读全文
posted @
2022-02-08 07:36
阳光明媚的菲越
阅读(27)
推荐(0)
236. Lowest Common Ancestor of a Binary Tree
摘要:When we get this problem, we need to confirm the following 3 questions: 1. Can root, p or q be null? (No) 2. Are both p and q in the tree (yes) 3. Can
阅读全文
posted @
2022-02-08 06:30
阳光明媚的菲越
阅读(21)
推荐(0)
938. Range Sum of BST
摘要:Although this is an easy question, but it is prone to bugs, and the code can be better. Following is my first solution, didn't use the feature of BST.
阅读全文
posted @
2022-02-08 04:38
阳光明媚的菲越
阅读(25)
推荐(0)
1570. Dot Product of Two Sparse Vectors
摘要:For sparse venctors, there might be too many "0"s in the array. What we need to do is only abstract the items which are not "0". We store these non-ze
阅读全文
posted @
2022-02-08 04:13
阳光明媚的菲越
阅读(42)
推荐(0)
1762. Buildings With an Ocean View
摘要:My first solution is use two skacks, one stack store index, another one store value, the time complexity is O(n). public int[] findBuildings(int[] hei
阅读全文
posted @
2022-02-08 01:29
阳光明媚的菲越
阅读(56)
推荐(0)
670. Maximum Swap
摘要:My first version brute force solution just switch the chars one by one and compare with the max value. The time complaxity is O(n2). public int maximu
阅读全文
posted @
2022-02-07 15:02
阳光明媚的菲越
阅读(49)
推荐(0)
146. LRU Cache
摘要:When I got this problem, I used one Deque and one map to solve it, the solution is as following, the time complexity of get() and put() is O(n), n is
阅读全文
posted @
2022-02-07 11:31
阳光明媚的菲越
阅读(53)
推荐(0)
173. Binary Search Tree Iterator
摘要:For this problem, if don't consider the follow up limitation, the solution is very easy: The time complexity is O(1), the space complexity is O(n), n
阅读全文
posted @
2022-02-06 12:07
阳光明媚的菲越
阅读(34)
推荐(0)
71. Simplify Path
摘要:When we met path or parentheses problems, always think about Stack first. For this problem, only three conditions need to be considered: 1. the substr
阅读全文
posted @
2022-02-05 09:54
阳光明媚的菲越
阅读(38)
推荐(0)
Stack vs. Deque
摘要:Same: Both of Stack and Dequeu can be a stack. Differences: 1. Deque is a LinkedList, Stack is a Stack. 2. Dequeu can getFirst() and getLast(), but St
阅读全文
posted @
2022-02-05 09:39
阳光明媚的菲越
阅读(52)
推荐(0)
314. Binary Tree Vertical Order Traversal
摘要:This problem should not use DFS to solve it, I have tried, but failed with this test case: [3,9,8,4,0,1,7,null,null,null,2,5] My output is: [[4],[9,5]
阅读全文
posted @
2022-02-05 08:50
阳光明媚的菲越
阅读(32)
推荐(0)
284. Peeking Iterator
摘要:This is a Iterator problem too, the soltuion is: 1. keep a variable, nextInt, to store the potential next Integer. 2. when peek(), if the nextInt is n
阅读全文
posted @
2022-02-05 07:55
阳光明媚的菲越
阅读(34)
推荐(0)
341. Flatten Nested List Iterator
摘要:This is an itegrator problem. You can look nestedList as a tree, just recursively read the nestedList, and put the integration into a list, and then i
阅读全文
posted @
2022-02-05 07:50
阳光明媚的菲越
阅读(28)
推荐(0)
1522. Diameter of N-Ary Tree
摘要:This is a similar problem with "543. Diameter of Binary Tree", the only difference is 543 is a binary tree, and 1522 is an n_ary tree. For 1522, we ne
阅读全文
posted @
2022-02-05 05:00
阳光明媚的菲越
阅读(29)
推荐(0)
543. Diameter of Binary Tree
摘要:This is a Post Order binary tree problem. For every node, we need to know its left tree's maximal diameter and its right tree's maximal diameter, and
阅读全文
posted @
2022-02-05 03:31
阳光明媚的菲越
阅读(34)
推荐(0)
426. Convert Binary Search Tree to Sorted Doubly Linked List
摘要:This is a "In Order" traversal problem, because the order of the result is a in order or the tree. 1. The problem need to return the pointer to the sm
阅读全文
posted @
2022-02-05 02:33
阳光明媚的菲越
阅读(36)
推荐(0)
78. Subsets
摘要:如果你碰到排列组合的题,就想想是否可以用backtracking做,然后把模版记住: private List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> subsets(int[] nums) { backt
阅读全文
posted @
2022-02-04 12:37
阳光明媚的菲越
阅读(33)
推荐(0)
261. Graph Valid Tree
摘要:Solution 1: Union-Found private int[] root; private int count; public boolean validTree(int n, int[][] edges) { count = n; if(edges.length!=n-1) retur
阅读全文
posted @
2022-02-04 11:55
阳光明媚的菲越
阅读(41)
推荐(0)
545. Boundary of Binary Tree
摘要:I have originally write a BFS solution for this problem, but it seems too difficlut. And then I read other's code, found that solution is much easier.
阅读全文
posted @
2022-02-04 10:15
阳光明媚的菲越
阅读(26)
推荐(0)
31. Next Permutation
摘要:Take the following nums as an exmpel : [2,4,3,1] The 4, 3, 1 is a decending order, it cannot be larger any more. How we can make 2,4,3,1 larger? we ne
阅读全文
posted @
2022-02-04 05:56
阳光明媚的菲越
阅读(48)
推荐(0)
50. Pow(x, n)
摘要:This problem is very easy to solve if using bruteforece solution, the time complexity is O(n). public double myPow(double x, int n) { if (n == 0) retu
阅读全文
posted @
2022-02-04 04:33
阳光明媚的菲越
阅读(32)
推荐(0)
408. Valid Word Abbreviation
摘要:This is two points problem, just concentrate and carefully deal with the characters, then the problem can be solved. This is a very good problem for w
阅读全文
posted @
2022-02-04 03:07
阳光明媚的菲越
阅读(49)
推荐(0)
59. Spiral Matrix II
摘要:This is the same problem with https://www.cnblogs.com/feiflytech/p/15862380.html public int[][] generateMatrix(int n) { int[][] matrix = new int[n][n]
阅读全文
posted @
2022-02-04 02:34
阳光明媚的菲越
阅读(33)
推荐(0)
54. Spiral Matrix
摘要:This is an exactly same problem with "59. Spiral Matrix II". I set every direction as a status, when one direction was implemented, the current status
阅读全文
posted @
2022-02-04 02:32
阳光明媚的菲越
阅读(44)
推荐(0)
739. Daily Temperatures
摘要:I firstly solved this problem bruteforcely, the solution is easy, but the time complexity is O(n2): public int[] dailyTemperatures(int[] temperatures)
阅读全文
posted @
2022-02-03 14:31
阳光明媚的菲越
阅读(37)
推荐(0)
647. Palindromic Substrings
摘要:This is a "palindromic" problem, I think DP can solve this problem, but there is a easier way to solve it, the time complexity is O(n2): private int r
阅读全文
posted @
2022-02-03 10:32
阳光明媚的菲越
阅读(36)
推荐(0)
556. Next Greater Element III
摘要:This is the similar problem with "31. Next Permutation", the differences are: 1. 31 don't care about whether the "next" is larger or not, but 556 care
阅读全文
posted @
2022-02-03 08:01
阳光明媚的菲越
阅读(39)
推荐(0)
1868. Product of Two Run-Length Encoded Arrays
摘要:This problem can be resolved by two points, but the operation is pretty tricky: public List<List<Integer>> findRLEArray(int[][] encoded1, int[][] enco
阅读全文
posted @
2022-02-03 07:53
阳光明媚的菲越
阅读(44)
推荐(0)
1344. Angle Between Hands of a Clock
摘要:This is an absolutely math problem: 1. calculate minute angle: 360/60*minutes 2. calculate hour angle: 360/12*hour%12 3. calculate hour angle's plus,
阅读全文
posted @
2022-02-03 04:28
阳光明媚的菲越
阅读(44)
推荐(0)
1428. Leftmost Column with at Least a One
摘要:First I wrote a bruteforce solution, but it didn't pass because of too many calls, the time complexity is O(m*n): public int leftMostColumnWithOne(Bin
阅读全文
posted @
2022-02-03 03:50
阳光明媚的菲越
阅读(47)
推荐(0)
1004. Max Consecutive Ones III
摘要:This problem can be solved by sliding window: 1. firstly the right point, j, start to move, if it meet a zero, the zeroNum++, which means, we convert
阅读全文
posted @
2022-02-03 03:03
阳光明媚的菲越
阅读(39)
推荐(0)
1216. Valid Palindrome III
摘要:This is the typical DP problem, the time complexity is O(n2), Where n is the length of string s. This is due to the fact that we try to find result fo
阅读全文
posted @
2022-02-03 00:47
阳光明媚的菲越
阅读(30)
推荐(0)
1091. Shortest Path in Binary Matrix
摘要:To solve the shortest path problem, of course, you need to think of the BFS: public int shortestPathBinaryMatrix(int[][] grid) { int[][] dirs = {{-1,
阅读全文
posted @
2022-02-02 14:25
阳光明媚的菲越
阅读(36)
推荐(0)
766. Toeplitz Matrix
摘要:Don't think about a lot, this is a very easy problem, time complexity is O(m*n) public boolean isToeplitzMatrix(int[][] matrix) { if (matrix == null |
阅读全文
posted @
2022-02-02 14:18
阳光明媚的菲越
阅读(32)
推荐(0)
1011. Capacity To Ship Packages Within D Days
摘要:For the solution of this problem, we should be able to tell that it need a binary search. The left value is the maxmum weight in the weight list, beca
阅读全文
posted @
2022-02-02 13:55
阳光明媚的菲越
阅读(30)
推荐(0)
678. Valid Parenthesis String
摘要:My solution for this problem is using two stacks, it's very easy to understand: public boolean checkValidString(String s) { Stack<Integer> stars = new
阅读全文
posted @
2022-02-02 11:19
阳光明媚的菲越
阅读(89)
推荐(0)
22. Generate Parentheses
摘要:This problem is a typical backtracking solution problem, we need a recursive method as helper, the following is my first solution, which is not very g
阅读全文
posted @
2022-02-02 08:21
阳光明媚的菲越
阅读(41)
推荐(0)
1541. Minimum Insertions to Balance a Parentheses String
摘要:Parentheses的题,首先想到stack,思路如下: 1. If get a left parentheses, if the stack size is odd number, that means, one right parentheses is missing, so result p
阅读全文
posted @
2022-02-02 04:33
阳光明媚的菲越
阅读(33)
推荐(0)
1614. Maximum Nesting Depth of the Parentheses
摘要:这道题就是921的变种,话不多说: public int maxDepth(String s) { int count = 0; int max = 0; for (char c : s.toCharArray()) { if (c == '(') { count++; max = Math.max
阅读全文
posted @
2022-02-02 03:12
阳光明媚的菲越
阅读(36)
推荐(0)
1249. Minimum Remove to Make Valid Parentheses
摘要:For this problem, if you can remember, always push the index of '(', the problem can be solved easily. The solution is: 1. if meet a '(', push the ind
阅读全文
posted @
2022-02-02 02:28
阳光明媚的菲越
阅读(33)
推荐(0)
20. Valid Parentheses
摘要:这道题是括号题,这种括号题如果是一种括号,就用一个int做stack就行,但是如果是多种括号,用int就烦琐了,一定要用Stack,下面是我的算法,时间复杂度就是O(n). 我把每类括号的做括号和右括号放在map中,如果是左括号就push到stack,如果是右括号,就pop stack,map中对应
阅读全文
posted @
2022-02-02 01:11
阳光明媚的菲越
阅读(46)
推荐(0)