Skip to content

Instantly share code, notes, and snippets.

@deyindra
deyindra / Reverse
Created September 16, 2018 20:20
Reverse Word
public static String reverseWord(String str){
char[] array = str.toCharArray();
int start=0;
int end=array.length-1;
reverse(array,0,array.length-1);
while (start<=end && array[start]==' ')
start++;
while (end>=start && array[end] == ' ')
end--;
@deyindra
deyindra / FindPair
Created September 17, 2018 19:50
Find Pair
public static void printPairUnsorted(int[] array, int sum){
Set<Integer> s = new HashSet<>();
for(int v:array){
int temp = sum - v;
if(s.contains(temp)){
System.out.println(String.format("Pair found %d %d", temp,v));
}
s.add(v);
}
}
@deyindra
deyindra / AssertJ
Created October 9, 2018 21:31
User Hit count
public class AssertJ {
private AssertJ(){
throw new AssertionError("Invalid Access");
}
/**
*
* @param object which will satisfy following code
* <pre>
* new Predicate&lt;T&gt;(){
@deyindra
deyindra / LFUCache
Last active October 13, 2018 18:07
LFU Cache
package org.idey.algo.datastructure.cache;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
public class LFUCache<K,V> {
private Map<K, Node<K,V>> map = new HashMap<>();
private Map<K, Integer> counts = new HashMap<>();
@deyindra
deyindra / LRU Cache
Created October 13, 2018 18:07
LRU Cache
package org.idey.algo.datastructure.cache;
import java.util.HashMap;
import java.util.Map;
public class LRUCache<K,V> {
private int capacity;
private Map<K, Node<K,V>> map = new HashMap<>();
private DoubleLinkedList<K,V> doubleLinkedList=new DoubleLinkedList<>();
public abstract class AbstractTreeIterator<E> implements Iterator<E> {
protected AbstractTreeIterator(TreeNode<E> root){
assert(root!=null);
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove is not supported");
}