Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 938c646

Browse files
Tidy up
1 parent 57a5c57 commit 938c646

14 files changed

+121
-144
lines changed

‎README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Algorithm exercises from LeetCode implemented in Java (v11) and JavaScript.
3131
### Stack
3232
- Min Stack | [Problem](https://leetcode.com/problems/min-stack) | [Java Solution](src/javacode/solutions/MinStack.java)
3333
- Valid Parentheses | [Problem](https://leetcode.com/problems/valid-parentheses) | [Java Solution](src/javacode/solutions/ValidParentheses.java)
34+
- Decode String | [Problem](https://leetcode.com/problems/decode-string) | [Java Solution](src/javacode/solutions/DecodeString.java)
3435
- Longest Absolute File Path | [Problem](https://leetcode.com/problems/longest-absolute-file-path) | [Java Solution](src/javacode/solutions/LongestAbsoluteFilePath.java)
3536
- Remove All Adjacent Duplicates In String | [Problem](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string) | [Java Solution](src/javacode/solutions/RemoveDuplicatesInString.java)
3637
- Binary Tree Inorder Traversal | [Problem](https://leetcode.com/problems/binary-tree-inorder-traversal) | [Java Solution](src/javacode/solutions/BinaryTreeInorderTraversal.java)
@@ -70,6 +71,7 @@ Algorithm exercises from LeetCode implemented in Java (v11) and JavaScript.
7071
- Peak Index in a Mountain Array | [Problem](https://leetcode.com/problems/peak-index-in-a-mountain-array) | [Java Solution](src/javacode/solutions/PeakIndexInMountainArray.java)
7172

7273
### Two Pointers
74+
- First Unique Character | [Problem](https://leetcode.com/problems/first-unique-character-in-a-string) | [Java Solution](src/javacode/solutions/FirstUniqueCharacter.java)
7375
- Valid Palindrome | [Problem](https://leetcode.com/problems/valid-palindrome) | [Java Solution](src/javacode/solutions/ValidPalindrome.java)
7476
- Is Subsequence | [Problem](https://leetcode.com/problems/is-subsequence) | [JS Solution](src/javascript/solutions/isSubsequence.js)
7577
- Squares of a Sorted Array | [Problem](https://leetcode.com/problems/squares-of-a-sorted-array) | [JS Solution](src/javascript/solutions/sortedSquares.js)
@@ -85,7 +87,7 @@ Algorithm exercises from LeetCode implemented in Java (v11) and JavaScript.
8587
- All Possible Full Binary Trees | [Problem](https://leetcode.com/problems/all-possible-full-binary-trees) | [Java Solution](src/javacode/solutions/AllPossibleFullBinaryTrees.java)
8688

8789
### BFS
88-
- Binary Tree Inorder Traversal | [Problem](https://leetcode.com/problems/binary-tree-inorder-traversal) | [Java Solution](src/javacode/solutions/BinaryTreeInorderTraversal.java)
90+
- Binary Tree Level Inorder Traversal | [Problem](https://leetcode.com/problems/binary-tree-level-order-traversal) | [Java Solution](src/javacode/solutions/BinaryTreeLevelOrderTraversal.java)
8991
- Binary Tree Zigzag Level Order Traversal | [Problem](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal) | [Java Solution](src/javacode/solutions/BinaryTreeZigzagLevelOrderTraversal.java)
9092
- Binary Tree Right Side View | [Problem](https://leetcode.com/problems/binary-tree-right-side-view) | [Java Solution](src/javacode/solutions/BinaryTreeRightSideView.java)
9193
- Shortest Path in Binary Matrix | [Problem](https://leetcode.com/problems/shortest-path-in-binary-matrix) | [Java Solution](src/javacode/solutions/ShortestPathInBinaryMatrix.java)

‎src/javacode/solutions/AllPathsFromSourceToTarget.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private void findPath(int currentPoint, List<Integer> path, List<List<Integer>>
2626
}
2727
}
2828

29-
// test
29+
// Test
3030
public static void main(String[] args) {
3131
AllPathsFromSourceToTarget solution = new AllPathsFromSourceToTarget();
3232

‎src/javacode/solutions/BestTimeToBuyAndSellStock.java‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,8 @@
22

33
// [Problem] https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
44
class BestTimeToBuyAndSellStock {
5-
6-
// test
7-
public static void main(String[] args) {
8-
BestTimeToBuyAndSellStock solution = new BestTimeToBuyAndSellStock();
9-
10-
// Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5
11-
int[] input = {7, 1, 5, 3, 6, 4};
12-
int expectedOutput = 5;
13-
int actualOutput = solution.maxProfit(input);
14-
15-
System.out.println("Test passed? " + (expectedOutput == actualOutput));
16-
}
17-
18-
// Linear solution - O(n) time, O(1) space
5+
// Simple math
6+
// O(n) time, O(1) space
197
public int maxProfit(int[] prices) {
208
if (prices == null || prices.length == 0) {
219
return 0;
@@ -27,4 +15,16 @@ public int maxProfit(int[] prices) {
2715
}
2816
return maxProfit;
2917
}
18+
19+
// Test
20+
public static void main(String[] args) {
21+
BestTimeToBuyAndSellStock solution = new BestTimeToBuyAndSellStock();
22+
23+
// Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5
24+
int[] input = {7, 1, 5, 3, 6, 4};
25+
int expectedOutput = 5;
26+
int actualOutput = solution.maxProfit(input);
27+
28+
System.out.println("Test passed? " + (expectedOutput == actualOutput));
29+
}
3030
}

‎src/javacode/solutions/BinaryTreeInorderTraversal.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
// [Problem] https://leetcode.com/problems/binary-tree-inorder-traversal/
1010
class BinaryTreeInorderTraversal {
11-
12-
// Recursion solution - O(N) time, O(logN) space
11+
// Recursion
12+
// O(n) time, O(logn) space
1313
public List<Integer> inorderTraversal(TreeNode root) {
1414
List<Integer> values = new ArrayList<>();
1515
addValueInorder(root, values);
@@ -24,7 +24,8 @@ private void addValueInorder(TreeNode node, List<Integer> values) {
2424
}
2525
}
2626

27-
// Stack solution - O(N) time, O(N) space
27+
// Stack
28+
// O(n) time, O(n) space
2829
public List<Integer> inorderTraversalStack(TreeNode root) {
2930
List<Integer> values = new ArrayList<>();
3031
Stack<TreeNode> stack = new Stack<>();
@@ -42,5 +43,4 @@ public List<Integer> inorderTraversalStack(TreeNode root) {
4243

4344
return values;
4445
}
45-
4646
}

‎src/javacode/solutions/ClimbingStairs.java‎

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
11
package javacode.solutions;
22

3-
// [Problem] https://leetcode.com/problems/climbing-stairs/
3+
// [Problem] https://leetcode.com/problems/climbing-stairs
44
class ClimbingStairs {
5-
6-
// test
7-
public static void main(String[] args) {
8-
ClimbingStairs solution = new ClimbingStairs();
9-
10-
int input = 3;
11-
int expectedOutput = 3;
12-
int actualOutput = solution.climbStairs(input);
13-
14-
System.out.println("Test passed? " + (expectedOutput == actualOutput));
15-
}
16-
17-
// Recursion with memoization - O(n) time, O(n) space
5+
// Recursion with memoization
6+
// O(n) time, O(n) space
187
private int[] memo;
198

209
public int climbStairs(int n) {
@@ -35,7 +24,8 @@ private int climbStairsRecursion(int n) {
3524
return memo[n];
3625
}
3726

38-
// Bottom up with memoization - O(n) time, O(n) space
27+
// Bottom up with memoization
28+
// O(n) time, O(n) space
3929
public int climbStairsBottomUp(int n) {
4030
if (n <= 1) {
4131
return n;
@@ -49,4 +39,15 @@ public int climbStairsBottomUp(int n) {
4939
}
5040
return memo[n];
5141
}
42+
43+
// Test
44+
public static void main(String[] args) {
45+
ClimbingStairs solution = new ClimbingStairs();
46+
47+
int input = 3;
48+
int expectedOutput = 3;
49+
int actualOutput = solution.climbStairs(input);
50+
51+
System.out.println("Test passed? " + (expectedOutput == actualOutput));
52+
}
5253
}

‎src/javacode/solutions/ConcatenationOfArray.java‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,7 @@
44

55
// [Problem] https://leetcode.com/problems/concatenation-of-array/
66
class ConcatenationOfArray {
7-
8-
// test
9-
public static void main(String[] args) {
10-
ConcatenationOfArray solution = new ConcatenationOfArray();
11-
12-
int[] nums = {1, 2, 1};
13-
int[] expectedOutput = {1, 2, 1, 1, 2, 1};
14-
int[] actualOutput = solution.getConcatenation(nums);
15-
16-
System.out.println(Arrays.toString(actualOutput));
17-
System.out.println("Test passed? " + Arrays.equals(expectedOutput, actualOutput));
18-
}
19-
7+
// Array
208
// O(n) time, O(n) space
219
public int[] getConcatenation(int[] nums) {
2210
int n = nums.length;
@@ -27,4 +15,14 @@ public int[] getConcatenation(int[] nums) {
2715
return concatenatedArr;
2816
}
2917

18+
// Test
19+
public static void main(String[] args) {
20+
ConcatenationOfArray solution = new ConcatenationOfArray();
21+
22+
int[] nums = {1, 2, 1};
23+
int[] expectedOutput = {1, 2, 1, 1, 2, 1};
24+
int[] actualOutput = solution.getConcatenation(nums);
25+
26+
System.out.println("Test passed? " + Arrays.equals(expectedOutput, actualOutput));
27+
}
3028
}

‎src/javacode/solutions/ConvertSortedArrayToBST.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
// [Problem] https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
66
class ConvertSortedArrayToBST {
7-
8-
// Binary search solution - O(logN) time, O(1) space
7+
// Binary search
8+
// O(logn) time, O(1) space
99
public TreeNode sortedArrayToBST(int[] nums) {
1010
return sortedArrayToBST(nums, 0, nums.length - 1);
1111
}
@@ -20,5 +20,4 @@ private TreeNode sortedArrayToBST(int[] nums, int startIndex, int endIndex) {
2020
node.right = sortedArrayToBST(nums, midIndex + 1, endIndex);
2121
return node;
2222
}
23-
2423
}

‎src/javacode/solutions/CountSortedVowelStrings.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// [Problem] https://leetcode.com/problems/count-sorted-vowel-strings
44
class CountSortedVowelStrings {
5+
// Dynamic programming
56
// O(n) time, O(1) space
67
public int countVowelStrings(int n) {
78
int aCount = 1, eCount = 1, iCount = 1, oCount = 1, uCount = 1;
@@ -18,7 +19,7 @@ public int countVowelStrings(int n) {
1819
return (aCount + eCount + iCount + oCount + uCount);
1920
}
2021

21-
// test
22+
// Test
2223
public static void main(String[] args) {
2324
CountSortedVowelStrings solution = new CountSortedVowelStrings();
2425

‎src/javacode/solutions/CountingBits.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,5 @@ public static void main(String[] args) {
4646
int[] actualOutput = solution.countBits(input);
4747

4848
System.out.println("Test passed? " + Arrays.equals(expectedOutput, actualOutput));
49-
5049
}
5150
}

‎src/javacode/solutions/DecodeString.java‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,10 @@
22

33
import java.util.Stack;
44

5-
// [Problem] https://leetcode.com/problems/decode-string/
5+
// [Problem] https://leetcode.com/problems/decode-string
66
class DecodeString {
7-
8-
// test
9-
public static void main(String[] args) {
10-
DecodeString solution = new DecodeString();
11-
12-
String input = "3[a2[c]]";
13-
String expectedOutput = "accaccacc";
14-
String actualOutput = solution.decodeString(input);
15-
16-
System.out.println("Test passed? " + expectedOutput.equals(actualOutput));
17-
}
18-
19-
// Using stack - O(n) time, O(n) space
7+
// Stack
8+
// O(n) time, O(n) space
209
public String decodeString(String s) {
2110
Stack<Integer> repeatCounts = new Stack<>();
2211
Stack<String> storedStrings = new Stack<>();
@@ -45,4 +34,14 @@ public String decodeString(String s) {
4534
return storedStrings.pop();
4635
}
4736

37+
// Test
38+
public static void main(String[] args) {
39+
DecodeString solution = new DecodeString();
40+
41+
String input = "3[a2[c]]";
42+
String expectedOutput = "accaccacc";
43+
String actualOutput = solution.decodeString(input);
44+
45+
System.out.println("Test passed? " + expectedOutput.equals(actualOutput));
46+
}
4847
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /