摘要: 1 class Solution { 2 public: 3 int removeDuplicates(int arr[], int n) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 if(n<=1) 7 return n; 8 int insertPos = 0; 9 for (int i = 1; i < n; i++)10 {11 if (arr[i] != arr[insertPos])12 arr[++in... 阅读全文
posted @ 2013-05-01 21:23 caijinlong 阅读(101) 评论(0) 推荐(0) 编辑
摘要: Givenn, generate all structurally uniqueBST's(binary search trees) that store values 1...n.For example,Givenn= 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ ... 阅读全文
posted @ 2013-05-01 21:12 caijinlong 阅读(123) 评论(0) 推荐(0) 编辑
摘要: Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...n?For example,Givenn= 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 ... 阅读全文
posted @ 2013-05-01 20:31 caijinlong 阅读(144) 评论(0) 推荐(0) 编辑
摘要: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 ... 阅读全文
posted @ 2013-05-01 16:30 caijinlong 阅读(123) 评论(0) 推荐(0) 编辑
摘要: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [6,5,7], [4,1,8,3]]The minimum path sum from top to bottom is11(i.e.,2+3+5+1= 11).Note:Bonus point if you are a... 阅读全文
posted @ 2013-05-01 16:16 caijinlong 阅读(126) 评论(0) 推荐(0) 编辑
摘要: Best Time to Buy and Sell Stock ISay you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. 1 int maxProfit(vector< 阅读全文
posted @ 2013-05-01 14:28 caijinlong 阅读(3047) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the following is not: 1 / \ 2 2 \ \ 3 3Note:Bonus points if you could solve it both recursively and iterati... 阅读全文
posted @ 2013-04-30 13:39 caijinlong 阅读(127) 评论(0) 推荐(0) 编辑
摘要: 1 class Solution { 2 public: 3 bool isSameTree(TreeNode *p, TreeNode *q) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 if (p == NULL && q == NULL) 7 return true; 8 if ((p == NULL && q != NULL) || (p != NULL && q... 阅读全文
posted @ 2013-04-30 10:53 caijinlong 阅读(116) 评论(0) 推荐(0) 编辑
摘要: Given an indexk, return thekthrow of the Pascal's triangle.For example, givenk= 3,Return[1,3,3,1].Note:Could you optimize your algorithm to use onlyO(k) extra space? 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 vector<int> res; 5 if(rowIndex < 0) 6 ... 阅读全文
posted @ 2013-04-30 10:44 caijinlong 阅读(117) 评论(0) 推荐(0) 编辑
摘要: GivennumRows, generate the firstnumRowsof Pascal's triangle.For example, givennumRows= 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]] 1 class Solution { 2 public: 3 vector<vector<int> > generate(int numRows) { 4 // Start typing your C/C++ solution below 5 // DO ... 阅读全文
posted @ 2013-04-30 10:16 caijinlong 阅读(120) 评论(0) 推荐(0) 编辑