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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy