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 86e0d8d

Browse files
Added some more Medium questions 🚀
1 parent 5f99578 commit 86e0d8d

24 files changed

+456
-8
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public List<Integer> inorderTraversal(TreeNode root) {
12+
13+
List<Integer> list = new ArrayList<>();
14+
Stack<TreeNode> stack = new Stack<TreeNode>();
15+
TreeNode node = root;
16+
17+
while (node != null || !stack.isEmpty())
18+
{
19+
if (node != null)
20+
{
21+
stack.push(node);
22+
node = node.left;
23+
}
24+
else
25+
{
26+
node = stack.pop();
27+
list.add(node.val);
28+
node = node.right;
29+
}
30+
}
31+
32+
return list;
33+
}
34+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.util.*;
2+
3+
class TreeNode {
4+
int val;
5+
TreeNode left;
6+
TreeNode right;
7+
TreeNode(int x) { val = x; }
8+
}
9+
10+
class Solution {
11+
public List<List<Integer>> levelOrder(TreeNode root) {
12+
13+
List<List<Integer>> res = new ArrayList<>();
14+
15+
if (root == null) return res;
16+
17+
Queue<TreeNode> queue = new LinkedList<>();
18+
queue.add(root);
19+
20+
while (!queue.isEmpty()) {
21+
22+
List<Integer> level = new ArrayList<>();
23+
int size = queue.size();
24+
25+
for (int i = 0; i < size; i++) {
26+
27+
TreeNode node = queue.poll(); //Returns & Del element from front
28+
level.add(node.val); //Add to the List
29+
30+
if (node.left != null) {
31+
queue.add(node.left);
32+
}
33+
34+
if (node.right != null) {
35+
queue.add(node.right);
36+
}
37+
}
38+
39+
res.add(level);
40+
}
41+
return res;
42+
}
43+
44+
45+
/* ANOTHER METHOD USING DFS */
46+
public List<List<Integer>> levelOrder2(TreeNode root) {
47+
List<List<Integer>> res = new ArrayList<List<Integer>>();
48+
levelHelper(res, root, 0);
49+
return res;
50+
}
51+
52+
public void levelHelper(List<List<Integer>> res, TreeNode root, int height) {
53+
if (root == null) return;
54+
55+
if (height >= res.size()) {
56+
res.add(new LinkedList<Integer>());
57+
}
58+
59+
res.get(height).add(root.val);
60+
levelHelper(res, root.left, height+1);
61+
levelHelper(res, root.right, height+1);
62+
}
63+
}
File renamed without changes.

‎Medium/DecodingWays.java‎ renamed to ‎Medium/DecodeWays.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class DecodingWays {
1+
class DecodeWays {
22

33
public static int numDecodings(String s) {
44

‎Medium/FindTheDuplicateNumber.java‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
class Solution {
2+
public int findDuplicate(int[] nums) {
3+
4+
/* 1st Method
5+
Arrays.sort(nums);
6+
for (int i = 1; i < nums.length; i++) {
7+
if (nums[i] == nums[i-1]) {
8+
return nums[i];
9+
}
10+
}
11+
return -1; */
12+
13+
14+
/* 2nd Method
15+
Set<Integer> seen = new HashSet<Integer>();
16+
for (int num : nums) {
17+
if (seen.contains(num)) {
18+
return num;
19+
}
20+
seen.add(num);
21+
}
22+
23+
return -1; */
24+
25+
/* 3rd Method Efficient
26+
Find the intersection point of the two runners.
27+
int tortoise = nums[0];
28+
int hare = nums[0];
29+
30+
do {
31+
tortoise = nums[tortoise];
32+
hare = nums[nums[hare]];
33+
34+
} while (tortoise != hare);
35+
36+
37+
// Find the "entrance" to the cycle.
38+
int ptr1 = nums[0];
39+
int ptr2 = tortoise;
40+
41+
while (ptr1 != ptr2) {
42+
ptr1 = nums[ptr1];
43+
ptr2 = nums[ptr2];
44+
}
45+
46+
return ptr1;
47+
48+
*/
49+
50+
51+
//Easy to Understand same as Hare Tortoise
52+
int slow = 0, fast = 0;
53+
do{
54+
slow = nums[slow];
55+
fast = nums[nums[fast]];
56+
57+
}while(slow != fast);
58+
59+
slow = 0;
60+
61+
while(slow != fast){
62+
slow = nums[slow];
63+
fast = nums[fast];
64+
}
65+
66+
return slow;
67+
68+
}
69+
}

‎Medium/GenerateParentheses.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.*;
2+
class Solution {
3+
public List<String> generateParenthesis(int n) {
4+
List<String> list = new ArrayList<>();
5+
backtrack(list, "", 0, 0, n);
6+
return list;
7+
}
8+
9+
public void backtrack(List<String> list, String cur, int open, int close, int max){
10+
11+
if (cur.length() == max * 2) { //Eg max = 3
12+
list.add(cur); //If len becomes 6 add to List
13+
return;
14+
}
15+
16+
if (open < max) //Add all the (((
17+
backtrack(list, cur+"(", open+1, close, max);
18+
19+
if (close < open) //Add all the )))
20+
backtrack(list, cur+")", open, close+1, max);
21+
}
22+
}

‎Medium/GroupAnagrams.java‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.*;
2+
class Solution {
3+
public List<List<String>> groupAnagrams(String[] strs) {
4+
if (strs.length == 0) return new ArrayList<>();
5+
6+
Map<String, List<String>> map = new HashMap<String, List<String>>();
7+
8+
for (String s : strs) { //["eat", "tea", "tan", "ate", "nat", "bat"],
9+
10+
char[] ca = s.toCharArray(); //will give [ e a t ]
11+
12+
Arrays.sort(ca);
13+
String key = String.valueOf(ca);
14+
15+
if (!map.containsKey(key)) //Add the sorted ie aet as key
16+
map.put(key, new ArrayList<>());
17+
18+
map.get(key).add(s); //Add the original eat as Value in the key aet
19+
}
20+
21+
return new ArrayList<>(map.values()); //Return all the values
22+
//Since we want to return List<List<String>>
23+
//Map.values() will return List<String> & Arraylist will return <List> of this List<String>
24+
}
25+
26+
/* ANOTHER WAY OF DOING */
27+
public List<List<String>> groupAnagrams2(String[] strs) {
28+
Map<String, List<String>> map = new HashMap<>();
29+
30+
for(String s: strs){
31+
int[] arr = new int[26];
32+
33+
for(int i = 0;i<s.length();i++){
34+
arr[s.charAt(i) - 'a']++;
35+
}
36+
37+
String key = Arrays.toString(arr);
38+
List<String> tempList = map.getOrDefault(key, new LinkedList<String>());
39+
tempList.add(s);
40+
map.put(key,tempList);
41+
}
42+
43+
return new LinkedList<>(map.values());
44+
}
45+
}

‎Medium/GrumpyBookStoreOwner.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class GrumpyBookStoreOwner {
1+
class GrumpyBookstoreOwner {
22

33
public int maxSatisfied(int[] customers, int[] grumpy, int X) {
44
int sum = 0, maxWindow = 0, window = 0;

‎Medium/InOrderIterative.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import java.util.*;
22

33
class TreeNode {
4-
int val;
5-
TreeNode left;
6-
TreeNode right;
7-
TreeNode(int x) { val = x; }
8-
}
4+
int val;
5+
TreeNode left;
6+
TreeNode right;
7+
TreeNode(int x) { val = x; }
8+
}
99

1010
class InOrderIterative {
1111
public List<Integer> inorderTraversal(TreeNode root) {

‎Medium/JumpGame.class‎

-967 Bytes
Binary file not shown.

0 commit comments

Comments
(0)

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