This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyStack { | |
Queue<Integer> queue; | |
/** Initialize your data structure here. */ | |
public MyStack() { | |
queue = new java.util.LinkedList<>(); | |
} | |
/** Push element x onto stack. */ | |
public void push(int x) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class QueueFromStack { | |
/** Initialize your data structure here. */ | |
Stack<Integer> stack1; | |
Stack<Integer> stack2; | |
public MyQueue() { | |
stack1 = new Stack<>(); | |
stack2 = new Stack<>(); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1 | |
// / \ | |
// 2 3 | |
// / \ | |
// 4 5 | |
// "1 2 3 * * 4 5 * * * *" | |
public String serialize(TreeNode root) { | |
if(root == null) | |
return ""; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public TreeNode sortedArrayToBST(int[] nums) { | |
return getBalancedTree(nums, 0, nums.length-1); | |
} | |
private TreeNode getBalancedTree(int[] a, int start, int end) { | |
if(end < start) | |
return null; | |
int mid = (start + end) / 2; | |
TreeNode root = new TreeNode(a[mid]); | |
root.left = getBalancedTree(a, start, mid-1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int low, max; | |
public String longestPalindrome(String s) { | |
int n = s.length(); | |
if(n < 2) | |
return s; | |
for(int i = 0; i < n-1; i++) { | |
extendPal(s, i, i); | |
extendPal(s, i, i+1); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static String computeNext(String seed) { | |
StringBuilder next = new StringBuilder(); | |
for(int i = 0; i < seed.length(); i++) { | |
char ch = seed.charAt(i); | |
int count = 1; | |
while(i+1 < seed.length() && seed.charAt(i+1) == ch) { | |
i++; | |
count++; | |
} | |
next.append(count); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static int max; | |
public int diameterOfBinaryTree(TreeNode root) { | |
max = 0; | |
maxDepth(root); | |
return max; | |
} | |
private static int maxDepth(TreeNode root) { | |
if(root == null) | |
return 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<List<Integer>> subsets(int[] nums) { | |
List<List<Integer>> answer = new ArrayList<>(); | |
int n = nums.length; | |
long ansSize = (long)Math.pow(2, n); | |
for(long i = 0; i < ansSize; i++) { | |
List<Integer> element= new ArrayList<>(); | |
for(int j = 0; j < n; j++) { | |
// add jth number if jth bit in i is set |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Brian Kernighan’s Algorithm | |
// For 1011 | |
// 1011 & 1010 = 1010 | |
// 1010 & 1001 = 1000 | |
// 1000 & 0111 = 0000 | |
// Number of &s till zero = 3 | |
private int calcSetBits(int n) { | |
int count = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean isSymmetric(TreeNode root) { | |
return isSymmetricHelper(root, root); | |
} | |
private static boolean isSymmetricHelper(TreeNode root1, TreeNode root2) { | |
if(root1 == null && root2 == null) | |
return true; | |
if(root1 == null || root2 == null) | |
return false; |