Posts

Unique Binary Search Trees

/*** Given  n , how many structurally unique  BST's  (binary search trees) that store values 1... n ? For example, Given  n  = 3, there are a total of 5 unique BST's.    1         3     3      2      1     \       /     /      / \      \      3     2     1      1   3      2     /     /       \                 \    2     1         2         ...

Remove Duplicates from Sorted Array

/* Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array A = [1,1,2], Your function should return length = 2, and A is now [1,2]. */ class Solution { public :        int findnext( int A [], int n , int st ) {               int id = st +1;               while (id < n ) {                      if ( A [ st ] != A [id]) break ;                      id++;            ...

reverse linked list between m and n

/* Reverse a linked list from position   m   to   n . Do it in-place and in one-pass. For example: Given   1->2->3->4->5->NULL ,   m   = 2 and   n   = 4, return   1->4->3->2->5->NULL . Note: Given   m ,   n   satisfy the following condition: 1 ≤   m   ≤   n   ≤ length of list. */ struct ListNode {      int val;      ListNode *next;      ListNode( int x ) : val( x ), next( NULL ) {} }; ListNode *reverse( ListNode * head , int sz ) {        if ( sz == 1) return head ;        ListNode *tail;        if ( sz > 1) tail = head ;        ListNode *rlist = nullptr ;        while ( sz != 0) {      ...

program exception

Exceptions provide a way to react to exceptional circumstances (like run-time errors) in our program by transferring control to special functions called handlers. try { //expection inspectation /* code */ //if exception occured, throw.. throw T; } catch(T error)   // argument type match the thrown var { /* code */ } /*** catch can be chainning with different type of argument as following ***/ try {   // code here } catch (int param) { cout << "int exception"; } catch (char param) { cout << "char exception"; } catch (...) { cout << "default exception"; } /*** It is also possible to nest try-catch blocks within more external try blocks. ***/ reference http://www.tutorialspoint.com/cplusplus/cpp_exceptions_handling.htm http://www.cplusplus.com/doc/tutorial/exceptions/

Prototype

Why Prototype : Need to duplicate objects with different dynamic types Prototype is particularly useful with static languages like C++, where classes are not objects, and little or no type information is available at run-time. Benefits: 1.        Adding and removing products at run-time. 2.        Specifying new objects by varying values 3.        Specifying new objects by varying structure. 4.        Reduced subclassing. 5.        Configuring an application with classes dynamically. Difficulty: The implementation of clone operation: especially when their internals include objects that don't support copying or have circular references. Implementation Solutions: -           Provide a polymorphic method that returns an instance of the same type as the object on whi...

Factory Pattern

Definition Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. Application Use the Factory Method pattern when · a class can't anticipate the class of objects it must create . · a class wants its subclasses to specify the objects it creates. · classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate. Implementation 1.        Two major varieties. The two main variations of the Factory Method pattern are (1) the case when the Creator class is an abstract class and does not provide an implementation for the factory method it declares, and (2) the case when the Creator is a concrete class and provides a default implementation for the factory method . 2.        Parameterized factory methods . Another variati...