Skip to content

Commit b0c099e

Browse files
committed
Update
1 parent e5a882b commit b0c099e

15 files changed

+412
-29
lines changed

src/array/MoveZeroes.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package array;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/move-zeroes
5+
* Difficulty: Easy
6+
* Time complexity: O(n)
7+
* Space complexity: O(1)
8+
*/
9+
public class MoveZeroes {
10+
11+
public void moveZeroes(int[] nums) {
12+
int zero = 0;
13+
int nonZero = 0;
14+
15+
while (nonZero < nums.length) {
16+
if (nums[nonZero] == 0) {
17+
nonZero++;
18+
} else {
19+
swap(nums, zero, nonZero);
20+
zero++;
21+
nonZero++;
22+
}
23+
}
24+
}
25+
26+
private void swap(int[] nums, int i, int j) {
27+
int tmp = nums[i];
28+
nums[i] = nums[j];
29+
nums[j] = tmp;
30+
}
31+
}

src/array/SquaresOfSortedArray.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package array;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/squares-of-a-sorted-array
5+
* Difficulty: Easy
6+
* Time complexity: O(n)
7+
* Space complexity: O(n)
8+
*/
9+
public class SquaresOfSortedArray {
10+
11+
public int[] sortedSquares(int[] nums) {
12+
int[] squares = new int[nums.length];
13+
14+
int left = 0;
15+
int right = nums.length - 1;
16+
17+
for (int i = nums.length - 1; i >= 0; i--) {
18+
if (Math.abs(nums[right]) >= Math.abs(nums[left])) {
19+
squares[i] = nums[right] * nums[right];
20+
right--;
21+
} else {
22+
squares[i] = nums[left] * nums[left];
23+
left++;
24+
}
25+
}
26+
27+
return squares;
28+
}
29+
}

src/binary/CountingBits.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package binary;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/counting-bits
5+
* Difficulty: Easy
6+
* Time complexity: O(n)
7+
* Space complexity: O(n)
8+
*/
9+
public class CountingBits {
10+
11+
public int[] countBits(int n) {
12+
int[] dp = new int[n + 1];
13+
for (int i = 0; i <= n; i++) {
14+
/*
15+
1. Unit digit of even numbers is always '0': 2 -> 10, 4 -> 100, 6 -> 110, 8 -> 1000
16+
2. Thus number of '1's for N and N / 2 is equal: 6 -> 110 && 3 -> 11, 10 -> 1010 && 5 -> 101
17+
3. For odd numbers one more '1' is added: 10 -> 1010 && 11 -> 1011, 2 -> 10 && 3 -> 11
18+
*/
19+
dp[i] = dp[i / 2] + (i % 2);
20+
}
21+
22+
return dp;
23+
}
24+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package binary_search;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/subtree-of-another-tree
5+
* Difficulty: Easy
6+
* Time complexity: O(m * n)
7+
* Space complexity: O(h2)
8+
*/
9+
public class SubtreeOfAnotherTree {
10+
11+
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
12+
if (root == null) return false;
13+
if (isEqual(root, subRoot)) return true;
14+
15+
boolean left = isSubtree(root.left, subRoot);
16+
boolean right = isSubtree(root.right, subRoot);
17+
18+
return left || right;
19+
}
20+
21+
private boolean isEqual(TreeNode first, TreeNode second) {
22+
if (first == null && second == null) return true;
23+
if (first == null || second == null) return false;
24+
if (first.val != second.val) return false;
25+
26+
boolean left = isEqual(first.left, second.left);
27+
boolean right = isEqual(first.right, second.right);
28+
29+
return left && right;
30+
}
31+
32+
private static class TreeNode {
33+
TreeNode left;
34+
TreeNode right;
35+
int val;
36+
}
37+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package binary_search_tree;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree
5+
* Difficulty: Easy
6+
* Time complexity: O(n)
7+
* Space complexity: O(log n)
8+
*/
9+
public class ConvertSortedArrayToBinarySearchTree {
10+
11+
public TreeNode sortedArrayToBST(int[] nums) {
12+
return toBST(nums, 0, nums.length - 1);
13+
}
14+
15+
private TreeNode toBST(int[] nums, int left, int right) {
16+
if (left > right) return null;
17+
18+
int mid = left + (right - left) / 2;
19+
TreeNode root = new TreeNode(nums[mid]);
20+
root.left = toBST(nums, left, mid - 1);
21+
root.right = toBST(nums, mid + 1, right);
22+
23+
return root;
24+
}
25+
26+
private static class TreeNode {
27+
TreeNode right;
28+
TreeNode left;
29+
int val;
30+
31+
public TreeNode(int val) {
32+
this.val = val;
33+
}
34+
}
35+
}

src/binary_search_tree/LowestCommonAncestorOfBinarySearchTree.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/**
44
* Description: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree
55
* Difficulty: Medium
6-
* Time complexity: O(log n)
7-
* Space complexity: O(log n)
6+
* Time complexity: O(h)
7+
* Space complexity: O(h)
88
*/
99
public class LowestCommonAncestorOfBinarySearchTree {
1010

src/binary_search_tree/ValidateBinarySearchTree.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Description: https://leetcode.com/problems/validate-binary-search-tree
55
* Difficulty: Medium
66
* Time complexity: O(n)
7-
* Space complexity: O(n)
7+
* Space complexity: O(h)
88
*/
99
public class ValidateBinarySearchTree {
1010

src/binary_tree/SameTree.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package binary_tree;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/same-tree
5+
* Difficulty: Easy
6+
* Time complexity: O(n)
7+
* Space complexity: O(h)
8+
*/
9+
public class SameTree {
10+
11+
public boolean isSameTree(TreeNode p, TreeNode q) {
12+
if (p == null && q == null) return true;
13+
if (p == null || q == null) return false;
14+
if (p.val != q.val) return false;
15+
16+
boolean left = isSameTree(p.left, q.left);
17+
boolean right = isSameTree(p.right, q.right);
18+
19+
return left && right;
20+
}
21+
22+
private static class TreeNode {
23+
TreeNode left;
24+
TreeNode right;
25+
int val;
26+
}
27+
}

src/binary_tree/SymmetricTree.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package binary_tree;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/symmetric-tree
5+
* Difficulty: Easy
6+
* Time complexity: O(n)
7+
* Space complexity: O(log n)
8+
*/
9+
public class SymmetricTree {
10+
11+
public boolean isSymmetric(TreeNode root) {
12+
return isMirror(root.left, root.right);
13+
}
14+
15+
private boolean isMirror(TreeNode left, TreeNode right) {
16+
if (left == null && right == null) return true;
17+
if (left == null || right == null) return false;
18+
if (left.val != right.val) return false;
19+
20+
return isMirror(left.left, right.right)
21+
&& isMirror(left.right, right.left);
22+
}
23+
24+
private static class TreeNode {
25+
TreeNode left;
26+
TreeNode right;
27+
int val;
28+
}
29+
}

src/dynamic_programming/ClimbingStairs.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ public class ClimbingStairs {
1313
* Time complexity: O(n)
1414
* Space complexity: O(n)
1515
*/
16-
public int climbStairsWithMemoization(int n) {
17-
Map<Integer, Integer> memo = new HashMap<>();
18-
memo.put(1, 1);
19-
memo.put(2, 2);
16+
public int climbStairsViaDP(int n) {
17+
Map<Integer, Integer> dp = new HashMap<>();
18+
dp.put(1, 1);
19+
dp.put(2, 2);
2020

21-
return count(n, memo);
21+
return count(n, dp);
2222
}
2323

24-
private int count(int n, Map<Integer, Integer> memo) {
25-
if (memo.containsKey(n)) return memo.get(n);
24+
private int count(int n, Map<Integer, Integer> dp) {
25+
if (dp.containsKey(n)) return dp.get(n);
2626

27-
int count = count(n - 1, memo) + count(n - 2, memo);
28-
memo.put(n, count);
27+
int count = count(n - 1, dp) + count(n - 2, dp);
28+
dp.put(n, count);
2929

3030
return count;
3131
}

src/dynamic_programming/CoinChange.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@ public class CoinChange {
1414
public int coinChange(int[] coins, int amount) {
1515
if (amount < 1) return 0;
1616

17-
Map<Integer, Integer> memo = new HashMap<>();
18-
memo.put(0, 0);
17+
Map<Integer, Integer> dp = new HashMap<>();
18+
dp.put(0, 0);
1919

2020
for (int currentAmount = 1; currentAmount <= amount; currentAmount++) {
2121
int count = Integer.MAX_VALUE;
2222

2323
for (int coin : coins) {
2424
if (currentAmount - coin < 0) continue;
2525

26-
if (memo.get(currentAmount - coin) != null) {
27-
count = Math.min(memo.get(currentAmount - coin) + 1, count);
26+
if (dp.get(currentAmount - coin) != null) {
27+
count = Math.min(dp.get(currentAmount - coin) + 1, count);
2828
}
2929
}
3030

3131
if (count != Integer.MAX_VALUE) {
32-
memo.put(currentAmount, count);
32+
dp.put(currentAmount, count);
3333
}
3434
}
3535

36-
return memo.getOrDefault(amount, -1);
36+
return dp.getOrDefault(amount, -1);
3737
}
3838
}

src/dynamic_programming/CoinChange2.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
public class CoinChange2 {
1010

1111
public int change(int amount, int[] coins) {
12-
int[] memo = new int[amount + 1];
13-
memo[0] = 1;
12+
int[] dp = new int[amount + 1];
13+
dp[0] = 1;
1414

1515
for (int coin : coins) {
1616
for (int i = coin; i <= amount; i++) {
1717
int prev = i - coin;
18-
memo[i] += memo[prev];
18+
dp[i] += dp[prev];
1919
}
2020
}
2121

22-
return memo[amount];
22+
return dp[amount];
2323
}
2424
}

src/dynamic_programming/LongestCommonSubsequence.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@
99
public class LongestCommonSubsequence {
1010

1111
public int longestCommonSubsequence(String text1, String text2) {
12-
int[][] memo = new int[text1.length()][text2.length()];
12+
int[][] dp = new int[text1.length()][text2.length()];
1313

1414
for (int i = 0; i < text1.length(); i++) {
1515
for (int j = 0; j < text2.length(); j++) {
1616
if (text1.charAt(i) == text2.charAt(j)) {
17-
int prev = (i > 0 && j > 0) ? memo[i - 1][j - 1] : 0;
18-
memo[i][j] = prev + 1;
17+
int prev = (i > 0 && j > 0) ? dp[i - 1][j - 1] : 0;
18+
dp[i][j] = prev + 1;
1919
} else {
20-
int prev1 = i > 0 ? memo[i - 1][j] : 0;
21-
int prev2 = j > 0 ? memo[i][j - 1] : 0;
22-
memo[i][j] = Math.max(prev1, prev2);
20+
int prev1 = i > 0 ? dp[i - 1][j] : 0;
21+
int prev2 = j > 0 ? dp[i][j - 1] : 0;
22+
dp[i][j] = Math.max(prev1, prev2);
2323
}
2424
}
2525
}
2626

27-
return memo[text1.length() - 1][text2.length() - 1];
27+
return dp[text1.length() - 1][text2.length() - 1];
2828
}
2929
}

0 commit comments

Comments
 (0)