摘要: 生成二叉查找树;哎,思考了大半天才写出来,主要卡在生成结点上了,开始没考虑好怎么把不同的树在不同的地方复制前面的部分,还是对递归的使用不够成熟,理解和掌握的还不够好! 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 So... 阅读全文
posted @ 2013-09-09 22:16 Exio 阅读(173) 评论(0) 推荐(0) 编辑
摘要: 很显然的DP,基本算是一次过了,比较简单! 1 class Solution { 2 public: 3 bool isInterleave(string s1, string s2, string s3) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int n1 = s1.size(); 7 int n2 = s2.size(); 8 int n3 = s3.size()... 阅读全文
posted @ 2013-09-09 15:56 Exio 阅读(159) 评论(0) 推荐(0) 编辑
摘要: 很简单,可惜刚开始把题目都理解错了;实际是找出所有的anagrams,把每个字符串sort一下即可,然后用map去找是否存在和它一样的anagrams,找到之后把当前的加入到返回数组中,并把map中已存在那个字符串也加入到数组中,只加入一次。class Solution {public: vector anagrams(vector &strs) { // Start typing your C/C++ solution below // DO NOT write int main() function vector res; if... 阅读全文
posted @ 2013-09-09 12:14 Exio 阅读(199) 评论(0) 推荐(0) 编辑
摘要: 哎,这两天都是在做水题,做水题都还一点状态都木有,经常一道题调好久~~~哎,好拙计! 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution {10 public:11 ListNode *mergeKLists(vector &lists) {12 ... 阅读全文
posted @ 2013-09-08 16:49 Exio 阅读(221) 评论(0) 推荐(0) 编辑
摘要: 繁琐的链表操作,很开心,一次就过了,不过推敲的时间也有20分钟了,还是加快思考的速度。 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution {10 public:11 ListNode *reverseKGroup(ListNode *head, int k)... 阅读全文
posted @ 2013-09-07 13:54 Exio 阅读(140) 评论(0) 推荐(0) 编辑
摘要: 没想出来,想到了count sort,但是没想到怎样用常量空间的方法。看了大神Annie Kim(https://github.com/AnnieKim/LeetCode)的代码,才豁然开朗,也算有学习和总结到了一条规律:凡是要求常量空间的题目要尽量利用题目中已有的空间!这道题目很经典,非常好的面试题。就算知道了方法以后,实现的时候也还是有一定的技巧的,每个位置i都需要多次交换,直到A[i] = i + 1为止,不过对于A[i] > n的元素可以先不管,最后再交换到最后! 1 class Solution { 2 public: 3 int firstMissingPositive(i. 阅读全文
posted @ 2013-09-07 12:49 Exio 阅读(170) 评论(0) 推荐(0) 编辑
摘要: 平衡二叉树的判断,DFS,一直没想到怎么同时返回结点的长度和状态,把高度放到参数里面比较简便! 1 class Solution { 2 public: 3 bool isBalanced(TreeNode *root) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int height = 0; 7 return getHeight(root, height); 8 9 ... 阅读全文
posted @ 2013-09-01 23:34 Exio 阅读(188) 评论(0) 推荐(0) 编辑
摘要: 数据结构的基础知识,根据中序、后序遍历或者先序、中序遍历构建二叉树;典型的递归;中序、后序的代码: 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 TreeNode ... 阅读全文
posted @ 2013-09-01 18:30 Exio 阅读(195) 评论(0) 推荐(0) 编辑
摘要: 有点类似于层级遍历,用两个数组保存上下两层的结点即可!同样的代码两道题目都可以过! 1 /** 2 * Definition for binary tree with next pointer. 3 * struct TreeLinkNode { 4 * int val; 5 * TreeLinkNode *left, *right, *next; 6 * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {} 7 * }; 8 */ 9 class Solution {10 public:1... 阅读全文
posted @ 2013-09-01 13:11 Exio 阅读(128) 评论(0) 推荐(0) 编辑
摘要: 典型的dp,最开始写了个递归的,过不了大数据, 1 class Solution { 2 public: 3 int numDistinct(string S, string T) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int m = S.length(); 7 int n = T.length(); 8 return getDistinct(S, T, m - 1, n -... 阅读全文
posted @ 2013-09-01 10:17 Exio 阅读(117) 评论(0) 推荐(0) 编辑