Skip to content

Commit bfee36b

Browse files
committed
Added more solutions to all categories
1 parent 40b59f9 commit bfee36b

File tree

12 files changed

+280
-9
lines changed

12 files changed

+280
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
*
3+
*/
4+
package com.javaaid.solutions.easy.bitmanipulations;
5+
6+
/**
7+
* @author Kanahaiya Gupta
8+
*
9+
*/
10+
public class CountingBits {
11+
public int[] countBits(int num) {
12+
int dp[]=new int[num+1];
13+
for(int i=0;i<=num;i++){
14+
dp[i]=dp[i>>1]+(i&1);
15+
}
16+
return dp;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
*
3+
*/
4+
package com.javaaid.solutions.easy.bitmanipulations;
5+
6+
/**
7+
* @author Kanahaiya Gupta
8+
*
9+
*/
10+
public class NumberOf1Bits {
11+
public int hammingWeight(int n) {
12+
int noOfOnes=0;
13+
while(n!=0){
14+
n=n&(n-1);
15+
noOfOnes++;
16+
}
17+
return noOfOnes;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
*
3+
*/
4+
package com.javaaid.solutions.easy.bitmanipulations;
5+
6+
/**
7+
* @author Kanahaiya Gupta
8+
*
9+
*/
10+
public class PowerOfFour {
11+
public boolean isPowerOfFour(int num) {
12+
return (num>0 && (num&(num-1))==0 && (num&0x55555555)!=0);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
*
3+
*/
4+
package com.javaaid.solutions.easy.bitmanipulations;
5+
6+
/**
7+
* @author Kanahaiya Gupta
8+
*
9+
*/
10+
public class PowerOfTwo {
11+
public boolean isPowerOfTwo(int n) {
12+
return (n>0 && (n&(n-1))==0);
13+
}
14+
}

LeetCodeSolutions/EasyLevelSolutions/src/main/java/com/javaaid/solutions/easy/dp/MinCostClimbingStairs.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
*
99
*/
1010
public class MinCostClimbingStairs {
11-
public int minCostClimbingStairs(int[] cost) {
12-
int n=cost.length;
13-
int[] dp=new int[cost.length+1];
14-
dp[0]=cost[0];
15-
dp[1]=cost[1];
16-
for(int i=2;i<cost.length;i++) {
17-
dp[i]=cost[i]+Math.min(dp[i-1], dp[i-2]);
11+
public int minCostClimbingStairs(int[] cost) {
12+
int n = cost.length;
13+
int[] dp = new int[cost.length + 1];
14+
dp[0] = cost[0];
15+
dp[1] = cost[1];
16+
for (int i = 2; i < cost.length; i++) {
17+
dp[i] = cost[i] + Math.min(dp[i - 1], dp[i - 2]);
18+
}
19+
return Math.min(dp[n - 2], dp[n - 1]);
1820
}
19-
return Math.min(dp[n-2], dp[n-1]);
20-
}
2121
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
*
3+
*/
4+
package com.javaaid.solutions.easy.strings;
5+
6+
/**
7+
* @author Kanahaiya Gupta
8+
*
9+
*/
10+
public class ReverseString {
11+
public String reverseString(String s) {
12+
StringBuilder sb = new StringBuilder();
13+
for (int i = s.length() - 1; i >= 0; i--) {
14+
sb.append(s.charAt(i));
15+
}
16+
return sb.toString();
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
*
3+
*/
4+
package com.javaaid.solutions.easy.trees;
5+
6+
/**
7+
* @author Kanahaiya Gupta
8+
*
9+
*/
10+
public class MaximumDepthOfBinaryTree {
11+
static class TreeNode {
12+
int val;
13+
TreeNode left;
14+
TreeNode right;
15+
16+
TreeNode(int x) {
17+
val = x;
18+
}
19+
}
20+
21+
public int maxDepth(TreeNode root) {
22+
if (root == null)
23+
return 0;
24+
return (1 + Math.max(maxDepth(root.left), maxDepth(root.right)));
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
*
3+
*/
4+
package com.javaaid.solutions.easy.trees;
5+
6+
/**
7+
* @author Kanahaiya Gupta
8+
*
9+
*/
10+
public class MinimumDepthOfBinaryTree {
11+
static class TreeNode {
12+
int val;
13+
TreeNode left;
14+
TreeNode right;
15+
16+
TreeNode(int x) {
17+
val = x;
18+
}
19+
}
20+
21+
public int minDepth(TreeNode root) {
22+
if (root == null)
23+
return 0;
24+
if (root.left == null || root.right == null)
25+
return (1 + Math.max(minDepth(root.left), minDepth(root.right)));
26+
return (1 + Math.min(minDepth(root.left), minDepth(root.right)));
27+
}
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
*
3+
*/
4+
package com.javaaid.solutions.easy.trees;
5+
6+
/**
7+
* @author Kanahaiya Gupta
8+
*
9+
*/
10+
public class PathSum {
11+
static class TreeNode {
12+
int val;
13+
TreeNode left;
14+
TreeNode right;
15+
16+
TreeNode(int x) {
17+
val = x;
18+
}
19+
}
20+
21+
public boolean hasPathSum(TreeNode root, int sum) {
22+
if (root == null)
23+
return false;
24+
if (root.left == null && root.right == null && sum - root.val == 0)
25+
return true;
26+
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
27+
28+
}
29+
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
*
3+
*/
4+
package com.javaaid.solutions.easy.trees;
5+
6+
/**
7+
* @author Kanahaiya Gupta
8+
*
9+
*/
10+
public class SameTree {
11+
static class TreeNode {
12+
int val;
13+
TreeNode left;
14+
TreeNode right;
15+
16+
TreeNode(int x) {
17+
val = x;
18+
}
19+
}
20+
21+
public boolean isSameTree(TreeNode p, TreeNode q) {
22+
if (p == null && q == null)
23+
return true;
24+
if (p == null && q != null || q == null && p != null)
25+
return false;
26+
return (p.val == q.val) && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
*
3+
*/
4+
package com.javaaid.solutions.medium.trees;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
/**
10+
* @author Kanahaiya Gupta
11+
*
12+
*/
13+
public class BinaryTreeInorderTraversal {
14+
15+
static class TreeNode {
16+
int val;
17+
TreeNode left;
18+
TreeNode right;
19+
20+
TreeNode(int x) {
21+
val = x;
22+
}
23+
}
24+
25+
public List<Integer> inorderTraversal(TreeNode root) {
26+
List<Integer> list = new ArrayList<Integer>();
27+
inorderTraversalUtil(root, list);
28+
return list;
29+
}
30+
31+
public void inorderTraversalUtil(TreeNode root, List<Integer> list) {
32+
if (root != null) {
33+
inorderTraversalUtil(root.left, list);
34+
list.add(root.val);
35+
inorderTraversalUtil(root.right, list);
36+
}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
*
3+
*/
4+
package com.javaaid.solutions.medium.trees;
5+
6+
/**
7+
* @author Kanahaiya Gupta
8+
*
9+
*/
10+
public class ValidateBinarySearchTree {
11+
12+
static class TreeNode {
13+
int val;
14+
TreeNode left;
15+
TreeNode right;
16+
17+
TreeNode(int x) {
18+
val = x;
19+
}
20+
}
21+
22+
public boolean isValidBST(TreeNode root) {
23+
return validateBSTUtil(root, null);
24+
}
25+
26+
private boolean validateBSTUtil(TreeNode root, Integer prevValue) {
27+
if (root == null)
28+
return true;
29+
if (!validateBSTUtil(root.left, prevValue))
30+
return false;
31+
if (prevValue != null && prevValue >= root.val)
32+
return false;
33+
prevValue = root.val;
34+
if (!validateBSTUtil(root.right, prevValue))
35+
return false;
36+
return true;
37+
}
38+
}

0 commit comments

Comments
 (0)