2013年9月12日

Pascal's Triangle

摘要: 帕斯卡三角形,主要考察vector的用法。vector > generate(int numRows){ vector > result; vector tmp; result.clear(); tmp.clear(); int i,j; if(numRows == 0) return result; else if(numRows == 1){ tmp.push_back(1); result.push_back(tmp); return result; } else if(numR... 阅读全文

posted @ 2013-09-12 20:49 waruzhi 阅读(210) 评论(0) 推荐(0) 编辑

Merge Sorted Array

摘要: 归并排序,实现了一个需要额外m+n空间的方法,应该可以优化使用更少的空间void merge(int A[], int m, int B[], int n){ int C[100000]; int i=0,j=0; while(i < m && j < n){ if(A[i] <= B[j]){ C[i+j] = A[i]; i++; } else{ C[i+j] = B[j]; j++; } } while(i < m)... 阅读全文

posted @ 2013-09-12 20:47 waruzhi 阅读(108) 评论(0) 推荐(0) 编辑

Same Tree

摘要: 判断两个二叉树是否相等,入门级递归。bool isSameTree(TreeNode *p, TreeNode *q) { if(p == NULL && q == NULL) return true; if(p == NULL || q == NULL) return false; if(p->val != q->val) return false; return isSameTree(p->left, q->left)&&isSameTree(p->right, q->right); } 阅读全文

posted @ 2013-09-12 20:46 waruzhi 阅读(84) 评论(0) 推荐(0) 编辑

Maximum Depth of Binary Tree

摘要: 求二叉树的最大深度,入门级别的递归。 int max(int a, int b){ if(a > b) return a; return b; } int maxDepth(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if(root == NULL) return 0; else r... 阅读全文

posted @ 2013-09-12 20:45 waruzhi 阅读(109) 评论(0) 推荐(0) 编辑

Best Time to Buy and Sell Stock II

摘要: 既然可以无数次地购买出售,那么之后后一天比前一天贵那就卖,否则留着。所以只需要从前往后扫一次,O(n)的复杂度。int maxProfit(vector &prices) { int n = prices.size(); if(n == 0 || n == 1) return 0; int i; int previous=prices[0], latter; int result = 0; for(i = 1; i = previous){ result = result + latter - previous; ... 阅读全文

posted @ 2013-09-12 20:41 waruzhi 阅读(171) 评论(0) 推荐(0) 编辑

导航