641. Design Circular Deque
摘要:My first solution - using ArrayList: class MyCircularDeque { List<Integer> list = new ArrayList<>(); int k ; public MyCircularDeque(int k) { this.k =
阅读全文
posted @
2022-03-31 06:49
阳光明媚的菲越
阅读(27)
推荐(0)
66. Plus One
摘要:My first solution: class Solution { public int[] plusOne(int[] digits) { int n = digits.length; int carry = 1; for(int i=n-1;i>=0;i--){ int digit = di
阅读全文
posted @
2022-03-31 04:13
阳光明媚的菲越
阅读(282)
推荐(0)
69. Sqrt(x)
摘要:Using binary search, time complexity: O(log(x)) class Solution { public int mySqrt(int x) { int l=1, r =x; while(l<r-1){ int mid=l+(r-l)/2; int temp =
阅读全文
posted @
2022-03-31 01:01
阳光明媚的菲越
阅读(36)
推荐(0)
772. Basic Calculator III
摘要:class Solution { public int calculate(String s) { s = s.replace(" ", "")+'+'; //Add a '+' or '-' is necessary, otherwise, the case (1+1) will return 0
阅读全文
posted @
2022-03-29 14:03
阳光明媚的菲越
阅读(41)
推荐(0)
224. Basic Calculator
摘要:这道题跟227题,背下来! Iterative: class Solution { public int calculate(String s) { s=s.replace(" ", "")+'+'; Stack<Integer> stk = new Stack<>(); int sign =1;
阅读全文
posted @
2022-03-29 13:12
阳光明媚的菲越
阅读(31)
推荐(0)
875. Koko Eating Bananas
摘要:Using Binary Search Template: class Solution { public int minEatingSpeed(int[] piles, int h) { int l=1, r=0; for(int i=0;i<piles.length;i++){ r = Math
阅读全文
posted @
2022-03-24 05:47
阳光明媚的菲越
阅读(27)
推荐(0)
926. Flip String to Monotone Increasing
摘要:class Solution { public int minFlipsMonoIncr(String s) { int oneNum = 0, flipNum =0; int res =0; for(char c: s.toCharArray()){ if(c=='0'){ if(oneNum==
阅读全文
posted @
2022-03-22 06:09
阳光明媚的菲越
阅读(23)
推荐(0)
2104. Sum of Subarray Ranges
摘要:By adding a 'max' and 'min' variable, we can make the solution's time complexity from O(n3) to O(n2): public long subArrayRanges(int[] nums) { long re
阅读全文
posted @
2022-03-22 05:19
阳光明媚的菲越
阅读(40)
推荐(0)
1710. Maximum Units on a Truck
摘要:Solution 1, Using a PriorityQueue class Solution { public int maximumUnits(int[][] boxTypes, int truckSize) { PriorityQueue<int[]> queue = new Priorit
阅读全文
posted @
2022-03-22 03:53
阳光明媚的菲越
阅读(25)
推荐(0)
1268. Search Suggestions System
摘要:I user Trie to store the products. For every Trie node, it has the links to the next character and a PriorityQueue. The PriorityQueue is used to store
阅读全文
posted @
2022-03-19 06:46
阳光明媚的菲越
阅读(21)
推荐(0)
937. Reorder Data in Log Files
摘要:This is a problem which check whether you know how to user Arrays.sort(). Time complexity: O(n*log(n)) Solution 1: Using comparator class Solution { p
阅读全文
posted @
2022-03-19 03:39
阳光明媚的菲越
阅读(38)
推荐(0)
2096. Step-By-Step Directions From a Binary Tree Node to Another
摘要:My first solution: 1. find lowest common ancestor of start and dest. 2. find path's length from LCA to start 3. find path from LCA to dest. 4. Combine
阅读全文
posted @
2022-03-12 11:05
阳光明媚的菲越
阅读(87)
推荐(0)
1146. Snapshot Array
摘要:The following is my first solution, very easy to understand, but brute force: For every snapshot, we clone the whole array. Time complexity of snap():
阅读全文
posted @
2022-03-11 05:32
阳光明媚的菲越
阅读(50)
推荐(0)
366. Find Leaves of Binary Tree
摘要:Instead of counting levels form root to leaves, this problem is counting levels from leaves to root. That is the leaves has the height of 1, and the r
阅读全文
posted @
2022-03-11 04:13
阳光明媚的菲越
阅读(14)
推荐(0)
766. Toeplitz Matrix
摘要:The most simple version: class Solution { public boolean isToeplitzMatrix(int[][] matrix) { if(matrix==null||matrix[0].length==0) return true; int m =
阅读全文
posted @
2022-03-10 01:45
阳光明媚的菲越
阅读(23)
推荐(0)
498. Diagonal Traverse
摘要:This is a hard problem. If I got this problem the first time when I was interviewed, I would not be able to solve it. public int[] findDiagonalOrder(i
阅读全文
posted @
2022-03-09 14:23
阳光明媚的菲越
阅读(25)
推荐(0)
317. Shortest Distance from All Buildings
摘要:This problem is different with 542. 01 Matrix and 286. Walls and Gates. 542 and 286 is once get the shortest path from one of the targets, then the ce
阅读全文
posted @
2022-03-09 11:50
阳光明媚的菲越
阅读(44)
推荐(0)
286. Walls and Gates
摘要:The solution of this issue is as same as 542. 01 Matrix class Solution { int m, n; private int[][] dirs ={{-1,0},{1,0},{0,-1},{0,1}}; public void wall
阅读全文
posted @
2022-03-09 05:18
阳光明媚的菲越
阅读(35)
推荐(0)
48. Rotate Image
摘要:If you could find the secret of image rotate, then this would be a simple problem. 1. If you want to rotate the image by 90 degrees clockwise,then rev
阅读全文
posted @
2022-03-09 03:57
阳光明媚的菲越
阅读(34)
推荐(0)
212. Word Search II
摘要:This problem need to solve mutiple Strings, so we use a Trie to store all strings, and then do DFS. Time complexity: O(m*n*3L), L is the maximum of th
阅读全文
posted @
2022-03-08 15:28
阳光明媚的菲越
阅读(40)
推荐(0)
79. Word Search
摘要:Although this is a matrix problem, but it cannot be done by BFS, following is my solution, but it doesn't work when the test case is: [["A","B","C","E
阅读全文
posted @
2022-03-08 12:56
阳光明媚的菲越
阅读(50)
推荐(0)
200. Number of Islands
摘要:If you know how to solve 827. Making A Large Island, then this one is a piece of cake: class Solution { private int res =0; int m,n; public int numIsl
阅读全文
posted @
2022-03-08 08:33
阳光明媚的菲越
阅读(34)
推荐(0)
827. Making A Large Island
摘要:This problem is based on 695. Max Area of Island 1. Create a islands matrix, if a cell in grid is 1, there will be a corresponding island number in is
阅读全文
posted @
2022-03-08 08:16
阳光明媚的菲越
阅读(38)
推荐(0)
1730. Shortest Path to Get Food
摘要:This is a easier common grap and look for shortest path problem, time complexity: O(m*n): class Solution { public int getFood(char[][] grid) { int m =
阅读全文
posted @
2022-03-05 15:02
阳光明媚的菲越
阅读(33)
推荐(0)
1293. Shortest Path in a Grid with Obstacles Elimination
摘要:This is graph and look for the shortest path, so BFS should be solution. But how to handle K? We can look K as a parameter of every node. My first sol
阅读全文
posted @
2022-03-05 14:29
阳光明媚的菲越
阅读(53)
推荐(0)
1197. Minimum Knight Moves
摘要:This is a graph problem, and the problem try to find the shortest path. So BFS is just right solution, the following is my first solution. It works bu
阅读全文
posted @
2022-03-05 09:03
阳光明媚的菲越
阅读(51)
推荐(0)