Skip to content

Instantly share code, notes, and snippets.

View rootid's full-sized avatar
💭
Keep Learning+Coding+Building

V23 rootid

💭
Keep Learning+Coding+Building
View GitHub Profile
@rootid
rootid / CopyCtor.cpp
Created June 6, 2015 22:01
Generic copy ctor in c++
//Copy-Constructor and assignment operator
MyClass::MyClass() : /* Fill in initializer list. */ {
/* Default initialization here. */
}
//copy constructor
MyClass::MyClass(const MyClass& other) {
copyOther(other);
}
@rootid
rootid / String.cpp
Created June 6, 2015 21:50
Implentation of String class with rule of three
#include<iostream>
#include<cstring>
//Implentation of String class with rule of three
class String
{
public:
String(const char* args)
: data_(new char[std::strlen(args) + 1]), length_(std::strlen(args))
@rootid
rootid / functor.cpp
Created June 6, 2015 21:48
Functor template
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class X {
public :
void op() {
cout << "X::op() " << endl;
}
@rootid
rootid / Queue.java
Created June 6, 2015 20:28
Queue using DLL
class Queue<T> {
/* In Queue updates made at the both ends
*/
class Node {
T data;
Node prev;
Node next;
Node (T data) {
this.data = data;
class HashTable {
class Entry {
//private final String key; Deleting the Entry ?
private String key;
private String value;
private Entry next;
Entry (String key,String value) {
this.key = key;
this.value = value;
@rootid
rootid / LRUCache.java
Created June 6, 2015 20:03
LRU cache design with DLL and HashMap
public class LRUCache {
//Keep track of least recently used elements
class Node {
String key_;
String val_;
Node next;
Node prev;
Node (String key,String val) {
this.key_ = key;
List<Integer> list = new LinkedList<Integer>();
//The Good
public List<Integer> preorderTraversalRec(TreeNode root) {
List<Integer> ans = new ArrayList<Integer>();
if (root == null) {
return ans;
}
ans.add(root.val);
ans.addAll(preorderTraversalRec(root.left));
//T(n) = O(n)
int dfsHeight (TreeNode *root) {
if (root == NULL) return 0;
int leftHeight = dfsHeight (root -> left);
if (leftHeight == -1) return -1;
int rightHeight = dfsHeight (root -> right);
if (rightHeight == -1) return -1;
if (abs(leftHeight - rightHeight) > 1) {
//T(n) = O(n^2)
int maxDepth(TreeNode *root) {
if (root == NULL) {
return 0;
}
int leftDepth = 0;
int rightDepth = 0;
leftDepth = maxDepth(root->left);
rightDepth = maxDepth(root->right);
return 1 + max(leftDepth, rightDepth);
//Augmenting the JS objects (NOTE : in JS function are objects)
//Ugly
function makePerson(first, last) {
return {
first: first,
last: last
};
}
function personFullName(person) {