2013年9月22日

Climbing Stairs

摘要: 最后一步只可能是1或2,所以每一种方案n都是方案n-1和方案n-2的和,其实是斐波那契数列。 int climbStairs(int n) { int result[1000]; // Start typing your C/C++ solution below // DO NOT write int main() function result[0] = 1; result[1] = 1; result[2] = 2; if(result[n] != 0) retur... 阅读全文

posted @ 2013-09-22 21:20 waruzhi 阅读(154) 评论(0) 推荐(1) 编辑

Unique Binary Search Trees

摘要: 使用带标记的DP int result[1000]; int numTrees(int n) { result[0] = 1; result[1] = 1; result[2] = 2; // Start typing your C/C++ solution below // DO NOT write int main() function if(result[n] != 0) return result[n]; int num = 0, t; ... 阅读全文

posted @ 2013-09-22 21:11 waruzhi 阅读(129) 评论(0) 推荐(1) 编辑

Remove Duplicates from Sorted Array

摘要: int removeDuplicates(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function if(n == 0) return n; int i; int result = 0; for(i = 1; i < n; i++){ if(A[i] != A[result]){ A[result+... 阅读全文

posted @ 2013-09-22 20:58 waruzhi 阅读(135) 评论(0) 推荐(1) 编辑

Remove Duplicates from Sorted List

摘要: 基本的链表操作,只需一次遍历即可 1 ListNode *deleteDuplicates(ListNode *head) { 2 if(!head) 3 return NULL; 4 ListNode *cur, *next; 5 cur = head; 6 while(cur){ 7 next = cur->next; 8 while(next){ 9 if(next->val != cur->val)10 ... 阅读全文

posted @ 2013-09-22 19:45 waruzhi 阅读(221) 评论(0) 推荐(1) 编辑

Remove Element

摘要: Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't mat... 阅读全文

posted @ 2013-09-22 19:29 waruzhi 阅读(156) 评论(0) 推荐(1) 编辑

Binary Tree Inorder Traversal

摘要: 二叉树的中序遍历,需要注意的是需要在每次运行之前初始化结果vector vector aVector; void dfs(TreeNode *root){ if(root->left != NULL) dfs(root->left); aVector.push_back(root->val); if(root->right != NULL) dfs(root->right); } vector inorderTraversal(TreeNode *root) { // S... 阅读全文

posted @ 2013-09-22 19:16 waruzhi 阅读(115) 评论(0) 推荐(1) 编辑

导航