Class static functions CANNOT call non-static member variables, neither can it call non-static class functions.
Wednesday, March 20, 2013
Tuesday, March 5, 2013
Generate Possible Sub-strings
Problem:
Generate all the possible substrings using the characters of a given string. Write code. (The order of chars do not matter, i.e., ac <=> ca)
i/p: abc
o/p: { a,b,c,ab,ac,bc,abc}
Solution (backtracking):
https://gist.github.com/leehomyc/5335144
rebuild BST
Node* RebuildBST(vector<int> output)
{
int n=output.size();
if(n==0)
return NULL;
stack<Node*> myStack;
int i=0;
Node* root;
while(i<n)
{
int curVal=output[i];
Node* preNode=NULL,*prePreNode=NULL;
while(!myStack.empty())
{
preNode=myStack.top();
if(preNode->val>curVal)
break;
prePreNode=myStack.top();
myStack.pop();
}
Node* curNode=new Node;
curNode->val=output[i];
curNode->L=NULL;
curNode->R=NULL;
if(!preNode)
{
root=curNode;
}
else if(!prePreNode)
{
preNode->L=curNode;
}
else
{
prePreNode->R=curNode;
}
myStack.push(curNode);
i++;
}
return root;
}
Monday, March 4, 2013
find the kth largest node of a BSTBST
void kth_largest(Node* root,unsigned int k)
{
if(!root)return;
stack<Node*> myStack;
Node* curNode=root;
int i=0;
while(curNode || !myStack.empty())
{
if(curNode)
{
myStack.push(curNode);
curNode=curNode->L;
}
else
{
Node* topNode=myStack.top();
curNode=topNode->R;
myStack.pop();
i++;
if(i==k)
cout<<topNode->val<<endl;
}
}
}
Sunday, March 3, 2013
Be careful with iterators!
OK, I am writing this to give you a strong warning!
STL has much more than you can imagine:
Remember this,
1. vector, deque, they are contiguous
2. list, set they are not.
So the difference is that
1. you can do arithmetic operations with vector deque iterators.
like: vector<int>::iterator iter; iter=iter+3;
but you cant do this: set<int>::iterator iter; iter=iter-1;
but for both iterators you can do: iter++, iter--; so equivalently you can do copy(set.begin(),set.end(),
ostream_iterator<int>(cout," ")) all this stuff...
2. when you destroy an iterator, something unpredictable will happen...ok predictable.
if it is in category one, all iterators will be destroyed. so like:
deque<int>::iterator iter=myQueue.begin();
myQueue.erase(iter+3);
then iter will also be lost!
However, if it is in category two, you can safely delete one iterator without sabotaging others.
list<int>::iterator iter=myList.begin(), iter2=iter;
iter2++;
myList.erase(iter);
iter=iter2;
...and this is fine.
STL has much more than you can imagine:
Remember this,
1. vector, deque, they are contiguous
2. list, set they are not.
So the difference is that
1. you can do arithmetic operations with vector deque iterators.
like: vector<int>::iterator iter; iter=iter+3;
but you cant do this: set<int>::iterator iter; iter=iter-1;
but for both iterators you can do: iter++, iter--; so equivalently you can do copy(set.begin(),set.end(),
ostream_iterator<int>(cout," ")) all this stuff...
2. when you destroy an iterator, something unpredictable will happen...ok predictable.
if it is in category one, all iterators will be destroyed. so like:
deque<int>::iterator iter=myQueue.begin();
myQueue.erase(iter+3);
then iter will also be lost!
However, if it is in category two, you can safely delete one iterator without sabotaging others.
list<int>::iterator iter=myList.begin(), iter2=iter;
iter2++;
myList.erase(iter);
iter=iter2;
...and this is fine.
Friday, March 1, 2013
Find the next node given parent node in BST
#include"tree_test.h"
#include<iostream>
using namespace std;
Node* InorderSuccessor(Node* node)
{
if(node->R)
{
Node* pre=node;
node=node->R;
while(node)
{
pre=node;
node=node->L;
}
return pre;
}
else if(node->parent)
{
Node* pre=node;
node=node->parent;
while(node && node->R==pre)
{
pre=node;
node=node->parent;
}
return node;
}
else
return NULL;
}
template specialization
template<>
inline cv::Point2f CPanoramicCore<cv::Point2f>::Interpolate(double x,double y,int& flag)
{
}
template<>
void spec1<char, int>()
{
}
cannot be partially specialized!
but can use following:
template <typename T1>
class class1<T1, int>
{
};
Subscribe to:
Posts (Atom)