Permalink
Browse files

.

  • Loading branch information...
1 parent 26111d5 commit b3e6d1ccc520a028218b6d92d07c1aeb02e6deec @dabaopku committed Dec 9, 2012
Showing with 253 additions and 0 deletions.
  1. +142 −0 DataStructure/TreeNode.h
  2. +65 −0 MinimumPathSum/MinimumPathSum.h
  3. +46 −0 Pow_x_n/Pow_x_n.h
View
@@ -0,0 +1,142 @@
+#include <iostream>
+#include <string>
+#include <queue>
+using namespace std;
+
+#define BUFFER_SIZE 10000
+
+struct TreeNode
+{
+ int val;
+ TreeNode *left;
+ TreeNode *right;
+ TreeNode(int x) : val(x), left(NULL), right(NULL) {}
+
+
+ ~TreeNode()
+ {
+ if(left)
+ delete left;
+ if(right)
+ delete right;
+ }
+ static TreeNode* load()
+ {
+ char buffer[BUFFER_SIZE];
+ cin.getline(buffer,BUFFER_SIZE,'\n');
+
+ TreeNode* root=NULL;
+ TreeNode* curNode=NULL;
+ queue<TreeNode*> que;
+
+ string str=buffer;
+ int n=str.length();
+ int cur=1;
+ int next=1;
+ while(cur<n)
+ {
+ next=str.find_first_of(",}",cur);
+ if(next==str.npos)
+ break;
+
+ string item=str.substr(cur,next-cur);
+ int val=-1;
+ if(item!="#")
+ {
+ val=atoi(item.c_str());
+ }
+
+ cur=next+1;
+
+ if(que.empty())
+ {
+ if(root==NULL)
+ {
+ if(val==-1)
+ {
+ cout<<"Invalid tree string!"<<endl;
+ exit(1);
+ }
+ root=new TreeNode(val);
+ que.push(root);
+ }
+ else
+ {
+ cout<<"Invalid tree string!"<<endl;
+ exit(1);
+ }
+ }
+ else
+ {
+ if(curNode==NULL)
+ {
+ curNode=que.front();
+ que.pop();
+ if(val!=-1)
+ {
+ TreeNode* nd=new TreeNode(val);
+ curNode->left=nd;
+ que.push(nd);
+ }
+ }
+ else
+ {
+ if(val!=-1)
+ {
+ TreeNode* nd=new TreeNode(val);
+ curNode->right=nd;
+ que.push(nd);
+ }
+ curNode=NULL;
+ }
+ }
+ }
+
+ return root;
+ }
+
+ void print()
+ {
+ int level=calLevel();
+ visitPrint(0);
+ }
+
+private:
+ int calLevel()
+ {
+ int l=0,r=0;
+ if(left)
+ {
+ l=left->calLevel();
+ }
+ if(right)
+ {
+ r=right->calLevel();
+ }
+ return max(r,l)+1;
+ }
+
+ void visitPrint(int level)
+ {
+ cout<<val<<endl;
+ printNode(level,left);
+ printNode(level,right);
+ }
+
+ void printNode(int level, TreeNode* node)
+ {
+ for(int i=0;i<level;++i)
+ {
+ cout<<' ';
+ }
+ cout<<"|-";
+ if(node)
+ {
+ node->visitPrint(level+2);
+ }
+ else
+ {
+ cout<<"#"<<endl;
+ }
+ }
+};
@@ -0,0 +1,65 @@
+/*
+Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
+
+Note: You can only move either down or right at any point in time.
+*/
+
+#include <vector>
+using namespace std;
+
+class Solution {
+public:
+ int minPathSum(vector<vector<int> > &grid)
+ {
+ int row=grid.size();
+ if(row==0) return 0;
+ int column=grid[0].size();
+ if(column==0) return 0;
+
+ vector<int> resRow;
+ vector<vector<int> > res;
+ for(int i=0;i<=column;++i)
+ resRow.push_back(0);
+ for(int i=0;i<=row;++i)
+ res.push_back(resRow);
+
+ for(int i=row-1;i>=0;--i)
+ {
+ res[i][column-1]=res[i+1][column-1]+grid[i][column-1];
+ }
+ for(int i=column-1;i>=0;--i)
+ {
+ res[row-1][i]=res[row-1][i+1]+grid[row-1][i];
+ }
+
+ for(int i=column-2;i>=0;--i)
+ {
+ for(int j=row-2;j>=0;--j)
+ {
+ res[j][i]=min(res[j+1][i],res[j][i+1])+grid[j][i];
+ }
+ }
+
+ return res[0][0];
+ }
+
+ void test()
+ {
+ int m,n;
+ cin>>m>>n;
+ vector<vector<int> > grid;
+ vector<int> row;
+ while(m--)
+ {
+ int c;
+ row.clear();
+ for(int i=0;i<n;++i)
+ {
+ cin>>c;
+ row.push_back(c);
+ }
+ grid.push_back(row);
+ }
+ cout<<minPathSum(grid);
+ }
+};
View
@@ -0,0 +1,46 @@
+/*
+Implement pow(x, n).
+*/
+
+class Solution {
+public:
+
+#define eps 1e-10
+
+ double pow(double x, int n)
+ {
+ if(n==0) return 1;
+ if(abs(x)<eps) return 0;
+ bool neg=false;
+ if(n<0)
+ {
+ n=-n;
+ neg=true;
+ }
+
+ double factor=x,res=1;
+ while(n)
+ {
+ if(n%2)
+ res*=factor;
+ n/=2;
+ factor*=factor;
+ }
+ if(neg)
+ {
+ if(fabs(res)<eps)
+ {
+ bool s=res>0;
+ res=(s?1:-1)*eps;
+ }
+ return 1/res;
+ }
+ else
+ return res;
+ }
+
+ void test()
+ {
+ cout<<pow(-1.213,-8);
+ }
+};

0 comments on commit b3e6d1c

Please sign in to comment.