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 <K extends Comparable<K>, V extends Comparable<V>>Map<K, V> sortMap(Map<K, V> map) { | |
List<Map.Entry<K, V>> entries = mapToEntries(map); | |
sortEntries(entries); | |
return entriesToMap(entries); | |
} | |
private static <K extends Comparable<K>, V extends Comparable<V>> List<Map.Entry<K,V>> mapToEntries(Map<K, V> map) { | |
return new ArrayList<>(map.entrySet()); | |
} |
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
#include <stdio.h> | |
int main() { | |
int foo = 1994; | |
// let pFoo be a pointer that points to an integer variable, whose value is the address of foo | |
int* pFoo = &foo; | |
printf("pFoo = &foo = %p = %p\n", pFoo, &foo); | |
// go to the variable pFoo points to (foo) and get its value. This is called dereferencing pFoo. |
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 int findMin(int[] a) { | |
int l = 0, r = a.length - 1; | |
while(l < r) { | |
int m = l + (r - l) / 2; | |
if(a[l] < a[r]) return a[l]; | |
if(m>=1 && a[m-1] > a[m]) return a[m]; | |
if(a[l] > a[m]) r = m - 1; | |
else if(a[l] < a[m]) l = m + 1; // or a[m] > a[r] |
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
//Digit root Problem | |
//Congruence formula | |
public int addDigits(int n) { | |
int b = 10; | |
if(n == 0) return 0; | |
else if(n % (b - 1) == 0) return b-1; | |
else return n % (b - 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
// if a > b and a > c, then a < b + c for a triangle to be formed | |
public int triangleNumber(int[] a) { | |
int n = a.length; | |
if(n < 3) return 0; | |
int count = 0; | |
Arrays.sort(a); | |
for(int i = 2; i < n; i++) { | |
int left = 0, right = i-1; | |
while(left < right) { |
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 String longestCommonPrefix(String[] strs) { | |
if(strs == null || strs.length == 0) | |
return ""; | |
int minLength = getMinLength(strs); | |
for(int i = 0; i < minLength; i++) { //For each index | |
char ch = strs[0].charAt(i); | |
for(int j = 1; j < strs.length; j++){ // For each string | |
if(strs[j].charAt(i) != ch) | |
return strs[0].substring(0, i); | |
} |
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 ListNode addTwoNumbers(ListNode l1, ListNode l2) { | |
if(l1 == null && l2 == null) return null; | |
ListNode help1 = l1, help2 = l2, dummyHead = new ListNode(0), help3 = dummyHead; | |
int c = 0; | |
while(help1 != null || help2 != null) { | |
int a = help1 == null ? 0 : help1.val; | |
int b = help2 == null ? 0 : help2.val; | |
int sum = a + b + c; |
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 class BSTIterator { | |
private Stack<TreeNode> stack; | |
public BSTIterator(TreeNode root) { | |
stack = new Stack<TreeNode>(); | |
pushLeft(root); | |
} | |
/** @return whether we have a next smallest number */ | |
public boolean hasNext() { |
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 isMatch(String str, String pat) { | |
char[] s = str.toCharArray(); | |
char[] p = pat.toCharArray(); | |
boolean[][] dp = new boolean[s.length+1][p.length+1]; | |
dp[0][0] = true; | |
for(int j = 1; j < dp[0].length; j++) { // For cases like (a, a*) | |
if(p[j-1] == '*') | |
dp[0][j] = dp[0][j-2]; |
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 LRUCache { | |
int capacity; | |
Map<Integer, ListNode> map; | |
ListNode head, tail; | |
private static class ListNode { | |
int key, value; | |
ListNode prev, next; | |
ListNode(int key, int value) { | |
this.key = key; |