Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9b7d9f5

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

19 files changed

+123
-114
lines changed

README.md

+1-1
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

+5-4
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

+2-2
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

+1-1
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

+1-1
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

+1-1
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
}
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

-42
This file was deleted.

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

-42
This file was deleted.

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

+4-4
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){

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
2525
*/
2626
public class _286 {
27-
class BFS_solution_without_queue{
27+
class BFSSolutionWithoutQueue {
2828

2929
int[] dirs = new int[]{0,1,0,-1,0};
3030
public void wallsAndGates(int[][] rooms) {
@@ -50,7 +50,7 @@ void bfs(int[][] rooms, int i, int j, int m, int n){
5050

5151
}
5252

53-
class BFS_solution_with_a_queue{
53+
class BFSSolutionWithAQueue {
5454

5555
//push all gates into the queue first, and then put all its neighbours into the queue with one distance to the gate, then continue to push the rest of the nodes into the queue, and put all their neighbours into the queue with the nodes' value plus one until the queue is empty
5656
int[] dirs = new int[]{0,1,0,-1,0};

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public int findDuplicate(int[] nums) {
2929
return dup;
3030
}
3131

32-
class Solution_O1 {
32+
class SolutionO1 {
3333
public int findDuplicate(int[] nums) {
3434
int slow = 0;
3535
int fast = 0;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ public double findMedian() {
6060

6161
}
6262

63-
public static class MedianFinder_verbose {
63+
public static class MedianFinderVerbose {
6464
private Queue<Long> large;
6565
private Queue<Long> small;
6666

67-
public MedianFinder_verbose() {
67+
public MedianFinderVerbose() {
6868
large = new PriorityQueue<>();
6969
small = new PriorityQueue<>(Collections.reverseOrder());
7070
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Space complexity should be O(n).
2424
*
2525
*/
2626
public class _338 {
27-
private class DP_Solution{
27+
private class DPSolution {
2828
//lixx2100's post is cool:https://discuss.leetcode.com/topic/40162/three-line-java-solution
2929
//An easy recurrence for this problem is f[i] = f[i / 2] + i % 2
3030
//and then we'll use bit manipulation to express the above recursion function

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.util.List;
66

77
public class _339 {
8-
class Solution_with_global_sum {
8+
class SolutionWithGlobalSum {
99
private int sum = 0;
1010

1111
public int depthSum(List<NestedInteger> nestedList) {
@@ -24,7 +24,7 @@ private int dfs(List<NestedInteger> nestedList, int depth) {
2424
}
2525
}
2626

27-
class Solution_with_local_sum {
27+
class SolutionWithLocalSum {
2828
public int depthSum(List<NestedInteger> nestedList) {
2929
return dfs(nestedList, 1);
3030
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ public class _384 {
2626

2727
public static void main(String... strings) {
2828
int[] nums = new int[]{1, 2, 3};
29-
Solution_for_this_question test = new Solution_for_this_question(nums);
29+
Solution test = new Solution(nums);
3030
}
3131

32-
public static class Solution_for_this_question {
32+
public static class Solution {
3333
//Note: the problem states that this is a set without duplicates which makes building all combinations easier
3434

3535
private List<List<Integer>> combinations;
3636
private int[] original;
3737
private Random random;
3838

39-
public Solution_for_this_question(int[] nums) {
39+
public Solution(int[] nums) {
4040
original = nums;
4141
random = new Random();
4242
combinations = buildAllComb(nums);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ public int pick(int target) {
4747
}
4848

4949

50-
class Solution_MemoryLimitExceeded {
50+
class SolutionMemoryLimitExceeded {
5151

5252
private Map<Integer, List<Integer>> map = new HashMap();
5353
java.util.Random rand = new java.util.Random();
5454

55-
public Solution_MemoryLimitExceeded(int[] nums) {
55+
public SolutionMemoryLimitExceeded(int[] nums) {
5656
for (int i = 0; i < nums.length; i++) {
5757
if (map.containsKey(nums[i])) {
5858
List<Integer> list = map.get(nums[i]);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private int dfs(TreeNode root, int result, boolean left) {
3737
return leftResult + rightResult;
3838
}
3939

40-
private class Solution_more_concise{
40+
private class Solution2 {
4141

4242
public int sumOfLeftLeaves(TreeNode root) {
4343
int sum = 0;

0 commit comments

Comments
 (0)