Skip to content

Commit 9b7d9f5

Browse files
enable TypeName check
1 parent 77452bc commit 9b7d9f5

File tree

19 files changed

+123
-114
lines changed

19 files changed

+123
-114
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ Your ideas/fixes/algorithms are more than welcome!
434434
|186|[Reverse Words in a String II](https://leetcode.com/problems/reverse-words-in-a-string-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_186.java)| O(n)|O(1)| Medium
435435
|179|[Largest Number](https://leetcode.com/problems/largest-number/)|[Solution](../../master/src/main/java/com/fishercoder/solutions/_179.java)| O(?) |O(?) | Medium|
436436
|174|[Dungeon Game](https://leetcode.com/problems/dungeon-game/)|[Queue](../master/src/main/java/com/fishercoder/solutions/BSTIterator_using_q.java) [Stack](../../blmaster/MEDIUM/src/medium/_174.java)| O(m*n) |O(m*n) | Hard| DP
437-
|173|[Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/)|[Queue](../master/src/main/java/com/fishercoder/solutions/_173_using_q.java) [Stack](../../blmaster/MEDIUM/src/medium/_173_using_stack.java)| O(1) |O(h) | Medium| Stack, Design
437+
|173|[Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/)|[Solution](../../blmaster/MEDIUM/src/medium/_173.java)| O(1) |O(h) | Medium| Stack, Design
438438
|172|[Factorial Trailing Zeroes](https://leetcode.com/problems/factorial-trailing-zeroes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_172.java)| O(logn)|O(1)| Easy
439439
|171|[Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_171.java)| O(n)|O(1)| Easy
440440
|170|[Two Sum III - Data structure design](https://leetcode.com/problems/two-sum-iii-data-structure-design/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_170.java)| O(n)|O(n)| Easy

fishercoder_checkstyle.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,11 @@
108108
<message key="name.invalidPattern"
109109
value="Package name ''{0}'' must match pattern ''{1}''."/>
110110
</module>
111-
<!--<module name="TypeName">-->
112-
<!--<message key="name.invalidPattern"-->
113-
<!--value="Type name ''{0}'' must match pattern ''{1}''."/>-->
114-
<!--</module>-->
111+
<module name="TypeName">
112+
<property name="format" value="^[_]*[0-9]*[a-zA-Z0-9]*$"/>
113+
<message key="name.invalidPattern"
114+
value="Type name ''{0}'' must match pattern ''{1}''."/>
115+
</module>
115116
<module name="MemberName">
116117
<property name="format" value="^[a-z]*[a-z0-9][a-zA-Z0-9]*$"/>
117118
<message key="name.invalidPattern"

src/main/java/com/fishercoder/solutions/_110.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
public class _110 {
1111

12-
class Solution_1 {
12+
class Solution1 {
1313
//recursively get the height of each subtree of each node, compare their difference, if greater than 1, then return false
1414
//although this is working, but it's not efficient, since it repeatedly computes the heights of each node every time
1515
//Its time complexity is O(n^2).
@@ -27,7 +27,7 @@ private int getH(TreeNode root) {
2727
}
2828
}
2929

30-
class Solution_2 {
30+
class Solution2 {
3131

3232
public boolean isBalanced(TreeNode root) {
3333
return getH(root) != -1;

src/main/java/com/fishercoder/solutions/_119.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public List<Integer> getRow(int rowIndex) {
3535
}
3636
}
3737

38-
public static class Solution_OkSpace {
38+
public static class SolutionOkSpace {
3939
public List<Integer> getRow(int rowIndex) {
4040
List<Integer> row = new ArrayList<>();
4141
for (int i = 0; i < rowIndex + 1; i++) {

src/main/java/com/fishercoder/solutions/_128.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public int maxUnion(){//this is O(n)
6565
}
6666
}
6767

68-
class Solution_using_HashSet{
68+
class SolutionUsingHashSet {
6969
//inspired by this solution: https://discuss.leetcode.com/topic/25493/simple-fast-java-solution-using-set
7070
public int longestConsecutive(int[] nums) {
7171
if(nums == null || nums.length == 0) return 0;

src/main/java/com/fishercoder/solutions/_129.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private void dfs(TreeNode root, StringBuilder sb, List<Integer> allNumbers) {
4848
sb.deleteCharAt(sb.length()-1);
4949
}
5050

51-
class more_concise_version {
51+
class MoreConciseVersion {
5252
public int sumNumbers(TreeNode root) {
5353
return dfs(root, 0);
5454
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
import java.util.LinkedList;
6+
import java.util.Queue;
7+
import java.util.Stack;
8+
9+
/**
10+
* 173. Binary Search Tree Iterator
11+
* Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
12+
* <p>
13+
* Calling next() will return the next smallest number in the BST.
14+
* <p>
15+
* Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree.
16+
*/
17+
public class _173 {
18+
19+
public static class Solution1 {
20+
21+
public static class BSTIterator {
22+
23+
private Queue<Integer> queue;
24+
25+
/**
26+
* My natural idea is to use a queue to hold all elements in the BST, traverse it while constructing the iterator, although
27+
* this guarantees O(1) hasNext() and next() time, but it uses O(n) memory.
28+
*/
29+
//Cheers! Made it AC'ed at first shot! Praise the Lord! Practice does make perfect!
30+
//I created a new class to do it using Stack to meet O(h) memory: {@link fishercoder.algorithms._173_using_stack}
31+
public BSTIterator(TreeNode root) {
32+
queue = new LinkedList<Integer>();
33+
if (root != null) dfs(root, queue);
34+
}
35+
36+
private void dfs(TreeNode root, Queue<Integer> q) {
37+
if (root.left != null) dfs(root.left, q);
38+
q.offer(root.val);
39+
if (root.right != null) dfs(root.right, q);
40+
}
41+
42+
/**
43+
* @return whether we have a next smallest number
44+
*/
45+
public boolean hasNext() {
46+
return !queue.isEmpty();
47+
}
48+
49+
/**
50+
* @return the next smallest number
51+
*/
52+
public int next() {
53+
return queue.poll();
54+
}
55+
}
56+
}
57+
58+
public static class Solution2 {
59+
public static class BSTIterator {
60+
/**
61+
* This is a super cool/clever idea: use a stack to store all the current left nodes of the BST, when pop(), we
62+
* push all its right nodes into the stack if there are any.
63+
* This way, we use only O(h) memory for this iterator, this is a huge saving when the tree is huge
64+
* since h could be much smaller than n. Cheers!
65+
*/
66+
67+
private Stack<TreeNode> stack;
68+
69+
public BSTIterator(TreeNode root) {
70+
stack = new Stack();
71+
pushToStack(root, stack);
72+
}
73+
74+
private void pushToStack(TreeNode root, Stack<TreeNode> stack) {
75+
while (root != null) {
76+
stack.push(root);
77+
root = root.left;
78+
}
79+
}
80+
81+
public boolean hasNext() {
82+
return !stack.isEmpty();
83+
}
84+
85+
public int next() {
86+
TreeNode curr = stack.pop();
87+
pushToStack(curr.right, stack);
88+
return curr.val;
89+
}
90+
}
91+
}
92+
}

src/main/java/com/fishercoder/solutions/_173_using_queue.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/main/java/com/fishercoder/solutions/_173_using_stack.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/main/java/com/fishercoder/solutions/_270.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
You are guaranteed to have only one unique value in the BST that is closest to the target.*/
1010
public class _270 {
1111

12-
class General_tree_solution {
12+
class GeneralTreeSolution {
1313
//this finished in 1 ms
1414
public int closestValue(TreeNode root, double target) {
1515
if (root == null)
@@ -33,7 +33,7 @@ private int dfs(TreeNode root, double target, double delta, int closestVal) {
3333
}
3434
}
3535

36-
class BST_solution_recursive{
36+
class BSTSolutionRecursive {
3737
//we can tailor the solution to use the BST feature: left subtrees are always smaller than the root the right subtrees
3838
//this finished in 0 ms
3939
public int closestValue(TreeNode root, double target) {
@@ -55,7 +55,7 @@ private int dfs(TreeNode root, double target, int minVal) {
5555
}
5656
}
5757

58-
class General_tree_solution_more_concise{
58+
class GeneralTreeSolutionMoreConcise {
5959
public int closestValue(TreeNode root, double target) {
6060
if(root == null) return 0;
6161
return dfs(root, target, root.val);
@@ -73,7 +73,7 @@ private int dfs(TreeNode root, double target, int minVal) {
7373
}
7474
}
7575

76-
class BST_solution_iterative{
76+
class BSTSolutionIterative {
7777
public int closestValue(TreeNode root, double target) {
7878
long minVal = Long.MAX_VALUE;
7979
while(root != null){

0 commit comments

Comments
 (0)