Skip to content

Commit b54258d

Browse files
authored
Merge pull request #910 from beingshub02/main
added code for leetcode solutions
2 parents da8d780 + 59df090 commit b54258d

6 files changed

+139
-0
lines changed
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public char repeatedCharacter(String s) {
3+
HashSet<Character> c =new HashSet<>();
4+
char ch='a';
5+
for(int i=0;i<s.length();i++){
6+
ch = s.charAt(i);
7+
if(c.isEmpty()!=true && c.contains(ch)){
8+
return ch;
9+
}else{
10+
c.add(ch);
11+
}
12+
}
13+
return ch;
14+
}
15+
}

LeetCode/Four_divisors.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public int sumFourDivisors(int[] nums) {
3+
int res = 0;
4+
5+
for (int i = 0; i < nums.length; i++){
6+
7+
int sum = 0; int count = 0;
8+
9+
for (int j = 1; j <= Math.sqrt(nums[i]); j++){
10+
11+
if (count <= 4)
12+
{
13+
if(nums[i] % j == 0)
14+
{
15+
count++;
16+
sum = sum + j;
17+
18+
if ( (nums[i]/j) != j )
19+
{
20+
count++;
21+
sum = sum + (nums[i]/j);
22+
}
23+
}
24+
25+
}
26+
27+
}
28+
29+
if (count == 4)
30+
res = res + sum;
31+
}
32+
33+
return res;
34+
}
35+
}

LeetCode/Inorder_Traversal.java

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public List<Integer> inorderTraversal(TreeNode root) {
18+
List<Integer> li = new ArrayList<>();
19+
Inorder(root,li);
20+
return li;
21+
}
22+
public List<Integer> Inorder(TreeNode root,List<Integer> li){
23+
if(root==null){
24+
return li;
25+
}
26+
Inorder(root.left,li);
27+
li.add(root.val);
28+
Inorder(root.right,li);
29+
30+
31+
return li;
32+
}
33+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public String mergeAlternately(String word1, String word2) {
3+
String s = "";
4+
int m = word2.length();
5+
int n= word1.length();
6+
int l = m>n?n:m;
7+
int i=0;
8+
for(;i<l;i++){
9+
char ch = word1.charAt(i);
10+
s=s+ch;
11+
m--;
12+
char ch2 = word2.charAt(i);
13+
s=s+ch2;
14+
n--;
15+
}
16+
if(m>0){
17+
s=s+word2.substring(i);
18+
}else{
19+
s=s+word1.substring(i);
20+
}
21+
return s;
22+
}
23+
24+
25+
}
26+
Footer

LeetCode/Power_of_three.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public boolean isPowerOfThree(int n) {
3+
if(n==0){
4+
return false;
5+
}
6+
if(n%3!=0 && n !=1){
7+
return false;
8+
}
9+
10+
if(n==1){
11+
return true;
12+
}
13+
else{
14+
return isPowerOfThree(n/3);
15+
}
16+
17+
18+
}
19+
}

LeetCode/Shuffle_the_array.java

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int[] shuffle(int[] nums, int n) {
3+
int num2[] = new int[nums.length];
4+
for(int i = 0,j=0;j<num2.length;i++,j++){
5+
num2[j] = nums[i];
6+
j++;
7+
num2[j] = nums[n+i];
8+
}
9+
return num2;
10+
}
11+
}

0 commit comments

Comments
 (0)