摘要: 树的遍历,递归: 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 */10 class Solution {11 public:12 bool isSymmetric(TreeNode *root) {13 // St... 阅读全文
posted @ 2013-09-20 13:53 Exio 阅读(290) 评论(0) 推荐(0) 编辑
摘要: 编辑距离,很常见的题目,貌似去年google笔试就考了这道题,典型的DP,dp[i][j]表示word2[i]到word1[j]的最小编辑距离;当word2[i]与word1[j]不相同时有三种方式:1.改变字符,dp[i][j] = dp[i - 1][j- 1] + 1;2.插入字符,dp[i][j] = dp[i ][j - 1] + 1;3.删除字符,dp[i][j] = dp[i - 1][j] + 1;故dp[i][j] = min(1,2,3);当word2[i]与word1[j]相同时:dp[i][j] = dp[i - 1][j - 1]; 1 class Solution 阅读全文
posted @ 2013-09-20 13:04 Exio 阅读(397) 评论(0) 推荐(0) 编辑
摘要: 看清问题,用DP就可以了,之前我首先想到的是图的搜索,实际上在这里并不适用;必须遍历每个点,计算它的最大矩阵,遍历的顺序为从左上角到右下角,x, y表示左边的最大长度,y表示上面的最大长度,然后计算每个点的最大面积; 1 class Solution { 2 public: 3 int maximalRectangle(vector > &matrix) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int res... 阅读全文
posted @ 2013-09-20 10:02 Exio 阅读(257) 评论(0) 推荐(0) 编辑
摘要: 又是这种需要仔细考虑的题目,很容易就会漏掉一些情况,哎,感冒了,完全不在状态,调了好几个小时,写代码的能力实在是不行,总感觉写出来的代码不够优雅, 1 class Solution { 2 public: 3 int trap(int A[], int n) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int res = 0; 7 if (A == NULL) { 8 re... 阅读全文
posted @ 2013-09-20 01:00 Exio 阅读(151) 评论(0) 推荐(0) 编辑