Skip to content

Commit bd9ef9b

Browse files
[LEET-0000] clean up
1 parent 5f2480a commit bd9ef9b

File tree

7 files changed

+94
-14
lines changed

7 files changed

+94
-14
lines changed

leetcode-algorithms/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
|308|[Range Sum Query 2D - Mutable](https://leetcode.com/problems/range-sum-query-2d-mutable/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/RangeSumQuery2DMutable.java)| ? | ? | Hard| Tree
7575
|306|[Additive Number](https://leetcode.com/problems/additive-number/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/AdditiveNumber.java)| ? | ? | Medium|
7676
|305|[Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/NumberofIslandsII.java)| ? | ? | Hard| Union Find
77+
|304|[Range Sum Query 2D - Immutable](https://leetcode.com/problems/range-sum-query-2d-immutable/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/RangeSumQuery2DImmutable.java)| ? | ? |Medium|
7778
|302|[Smallest Rectangle Enclosing Black Pixels](https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/SmallestRectangleEnclosingBlackPixels.java)| ? | O(m*n) | Hard| DFS, BFS
7879
|301|[Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/RemoveInvalidParentheses.java)| ? | ? | Hard| BFS
7980
|299|[Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BullsandCows.java)| O(n)|O(1) | Easy|
@@ -182,9 +183,11 @@
182183
|141|[Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/LinkedListCycle.java)| O(n)|O(1) | Easy| Linked List
183184
|140|[Word Break II](https://leetcode.com/problems/word-break-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/WordBreakII.java)| ? |O(n^2) | Hard| Backtracking/DFS
184185
|139|[Word Break](https://leetcode.com/problems/word-break/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/WordBreak.java)| O(n^2)|O(n) | Medium| DP
186+
|137|[Single Number II](https://leetcode.com/problems/single-number-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/SingleNumberII.java)| O(n)|O(n) | Medium|
185187
|135|[Candy](https://leetcode.com/problems/candy/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/Candy.java)| O(n)|O(1) | Hard| Greedy
186188
|133|[Clone Graph](https://leetcode.com/problems/clone-graph/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/CloneGraph.java)| O(n)|O(n) | Medium| HashMap, BFS
187189
|132|[Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ImplementQueueUsingStacks.java)| O(n)|O(n) | Easy| Stack, Queue
190+
|129|[Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/SumRootToLeafNumbers.java)| O(n)|O(h) | Medium| DFS
188191
|125|[Valid Palindrome](https://leetcode.com/problems/valid-palindrome/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ValidPalindrome.java)| O(n)|O(1) | Easy| Two Pointers
189192
|124|[Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BinaryTreeMaximumPathSum.java)| O(n)|O(h) | Hard | Tree
190193
|122|[Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BestTimeToBuyAndSellStockII.java)| O(n)|O(1) | Medium | Greedy
@@ -194,6 +197,7 @@
194197
|118|[Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/PascalsTriangle.java)| O(n^2)|O(1) | Easy|
195198
|117|[Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/PopulatingNextRightPointersinEachNodeII.java)| O(n)|O(1) | Hard| BFS
196199
|116|[Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/PopulatingNextRightPointersinEachNode.java)| O(n)|O(1) | Medium| BFS
200+
|115|[Distinct Subsequences](https://leetcode.com/problems/distinct-subsequences/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/DistinctSubsequences.java)| O(m*n)|O(m*n) | Hard| DP
197201
|114|[Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FlattenBinaryTreetoLinkedList.java)| O(n)|O(h) | Medium| Tree
198202
|112|[Path Sum](https://leetcode.com/problems/path-sum/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/PathSum.java)| O(n)|O(1) | Easy| DFS
199203
|111|[Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/MinimumDepthofBinaryTree.java)| O(n)|O(1)~O(h) | Easy| BFS, DFS
@@ -208,6 +212,7 @@
208212
|97|[Interleaving String](https://leetcode.com/problems/interleaving-string/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/InterleavingString.java)| O(?)|O(?) | Hard| DP
209213
|96|[Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/UniqueBinarySearchTrees.java) | O(n^2) | O(n) | Medium | Recursion, DP
210214
|94|[Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BinaryTreeInorderTraversal.java)| O(n)|O(h) | Medium| Binary Tree
215+
|92|[Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ReverseLinkedListII.java)| O(n)|O(1) | Medium
211216
|91|[Decode Ways](https://leetcode.com/problems/decode-ways/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/DecodeWays.java)| O(n)|O(n) | Medium| DP
212217
|89|[Gray Code](https://leetcode.com/problems/gray-code/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/GrayCode.java)|O(n) |O(1)|Medium|Bit Manipulation
213218
|86|[Partition List](https://leetcode.com/problems/partition-list/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/PartitionList.java)|O(?) |O(?)|Medium|
@@ -252,6 +257,7 @@
252257
|22|[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/GenerateParentheses.java)|TBD|O(n)|Medium|Backtracking
253258
|21|[Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/MergeTwoSortedLists.java)|O(n)|O(1)|Easy|
254259
|20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ValidParentheses.java)|O(n)|O(n)|Easy|Stack
260+
|19|[Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/RemoveNthNodeFromEndOfList)|O(n)|O(1)|Medium| Linked List
255261
|18|[4Sum](https://leetcode.com/problems/4sum/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/_4Sum.java)|O(n^2)|O(1)|Medium|Two Pointers
256262
|17|[Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/LetterCombinationsofaPhoneNumber.java)|O(n*4^n)|O(n)|Medium|Backtracking
257263
|16|[3Sum Closest](https://leetcode.com/problems/3sum-closest/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/_3SumClosest.java)|O(nlogn)|O(1)|Medium|Two Pointers
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.stevesun.solutions;
2+
3+
/**
4+
* Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).
5+
6+
Range Sum Query 2D
7+
The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.
8+
9+
Example:
10+
Given matrix = [
11+
[3, 0, 1, 4, 2],
12+
[5, 6, 3, 2, 1],
13+
[1, 2, 0, 1, 5],
14+
[4, 1, 0, 1, 7],
15+
[1, 0, 3, 0, 5]
16+
]
17+
18+
sumRegion(2, 1, 4, 3) -> 8
19+
sumRegion(1, 1, 2, 2) -> 11
20+
sumRegion(1, 2, 2, 4) -> 12
21+
Note:
22+
You may assume that the matrix does not change.
23+
There are many calls to sumRegion function.
24+
You may assume that row1 ≤ row2 and col1 ≤ col2.
25+
*/
26+
public class RangeSumQuery2DImmutable {
27+
28+
public class NumMatrix {
29+
30+
public NumMatrix(int[][] matrix) {
31+
if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
32+
return;
33+
34+
/**The dimensions of this tot matrix is actually 1 bigger than the given matrix, cool!*/
35+
tot = new int[matrix.length + 1][matrix[0].length + 1];
36+
for (int i = 0; i < matrix.length; i++) {
37+
for (int j = 0; j < matrix[0].length; j++) {
38+
tot[i + 1][j + 1] = matrix[i][j] + tot[i + 1][j] + tot[i][j + 1] - tot[i][j];
39+
}
40+
}
41+
}
42+
43+
public int sumRegion(int row1, int col1, int row2, int col2) {
44+
return tot[row2 + 1][col2 + 1] - tot[row2 + 1][col1] - tot[row1][col2 + 1]
45+
+ tot[row1][col1];
46+
}
47+
48+
int[][] tot;
49+
}
50+
51+
/**
52+
* Your NumMatrix object will be instantiated and called as such:
53+
* NumMatrix obj = new NumMatrix(matrix);
54+
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
55+
*/
56+
}

leetcode-algorithms/src/main/java/com/stevesun/solutions/RemoveNthNodeFromEndOfList.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
import com.stevesun.common.classes.ListNode;
44
import com.stevesun.common.utils.CommonUtils;
55

6-
/**19. Remove Nth Node From End of List QuestionEditorial Solution My Submissions
7-
Total Accepted: 122330
8-
Total Submissions: 400291
9-
Difficulty: Easy
6+
/**19. Remove Nth Node From End of List
107
Given a linked list, remove the nth node from the end of list and return its head.
118
129
For example,

leetcode-algorithms/src/main/java/com/stevesun/solutions/ReverseLinkedListII.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
import com.stevesun.common.classes.ListNode;
44
import com.stevesun.common.utils.CommonUtils;
55

6+
/**Reverse a linked list from position m to n. Do it in-place and in one-pass.
7+
8+
For example:
9+
Given 1->2->3->4->5->NULL, m = 2 and n = 4,
10+
11+
return 1->4->3->2->5->NULL.
12+
13+
Note:
14+
Given m, n satisfy the following condition:
15+
1 ≤ m ≤ n ≤ length of list.*/
616
public class ReverseLinkedListII {
717

818
// then I turned to Discuss and find this most upvoted solution:

leetcode-algorithms/src/main/java/com/stevesun/solutions/RomantoInteger.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import java.util.HashMap;
44
import java.util.Map;
55

6+
/**Given a roman numeral, convert it to an integer.
7+
8+
Input is guaranteed to be within the range from 1 to 3999.*/
69
public class RomantoInteger {
710

811
public int romanToInt(String s) {

leetcode-algorithms/src/main/java/com/stevesun/solutions/SingleNumberII.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,15 @@
33
import java.util.HashMap;
44
import java.util.Map;
55

6-
/**137. Single Number II QuestionEditorial Solution My Submissions
7-
Total Accepted: 91512
8-
Total Submissions: 236469
9-
Difficulty: Medium
6+
/**137. Single Number II
107
Given an array of integers, every element appears three times except for one. Find that single one.
118
129
Note:
1310
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
1411
15-
Hide Tags Bit Manipulation
16-
Show Similar Problems
1712
*/
1813
public class SingleNumberII {
1914

20-
//TODO: study its bit manipulation approach, this is a MUST!
21-
2215
public int singleNumber(int[] nums) {
2316
Map<Integer, Integer> map = new HashMap();
2417
for(int i : nums){

leetcode-algorithms/src/main/java/com/stevesun/solutions/SumRootToLeafNumbers.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,24 @@
55
import java.util.ArrayList;
66
import java.util.List;
77

8+
/**Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
9+
10+
An example is the root-to-leaf path 1->2->3 which represents the number 123.
11+
12+
Find the total sum of all root-to-leaf numbers.
13+
14+
For example,
15+
16+
1
17+
/ \
18+
2 3
19+
The root-to-leaf path 1->2 represents the number 12.
20+
The root-to-leaf path 1->3 represents the number 13.
21+
22+
Return the sum = 12 + 13 = 25.
23+
24+
*/
825
public class SumRootToLeafNumbers {
9-
//it could overflow when the tree is very deep/large, so definitely use long
10-
//let's assume it won't overflow first
1126
public int sumNumbers(TreeNode root) {
1227
if(root == null) return 0;
1328
List<Integer> allNumbers = new ArrayList();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy