摘要: 思路:每次插入一对括号,注意对于之前出现过的字符串就不要在处理了,否则会TLE. 1 1 #include 2 2 #include 3 3 #include 4 4 using namespace std; 5 5 class Solution { 6 6 public: 7 7 set strset; 8 8 vector generateParenthesis(int n) { 9 9 string s = "";10 10 vector ret;11 11 if(n &ret){16 16 ... 阅读全文
posted @ 2014-04-08 20:31 青轰的后花园 阅读(150) 评论(0) 推荐(0) 编辑
摘要: 想当初写过C的大数加减法,恍如隔世。 1 #include 2 using namespace std; 3 struct ListNode { 4 int val; 5 ListNode *next; 6 ListNode(int x) : val(x), next(NULL) {} 7 }; 8 class Solution { 9 public:10 ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {11 if(l1 == NULL) return l2;12 if(... 阅读全文
posted @ 2014-03-20 21:25 青轰的后花园 阅读(135) 评论(0) 推荐(0) 编辑
摘要: 并归排序的思想,新get一种地址取法~之前怎么想不到。。 1 #include 2 #include 3 using namespace std; 4 5 class Solution { 6 public: 7 void merge(int A[], int m, int B[], int n) { 8 if(n == 0) return ; 9 if(m == 0){10 memcpy(A,B,sizeof(int)*n);11 return ;12 }13 int... 阅读全文
posted @ 2014-03-18 21:25 青轰的后花园 阅读(140) 评论(0) 推荐(0) 编辑
摘要: 递归的思想,左子树整理成一条链,右子树整理成一条链,然后左子树的尾节点链上右子树的头节点就好了 1 #include 2 using namespace std; 3 struct TreeNode { 4 int val; 5 TreeNode *left; 6 TreeNode *right; 7 TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 }; 9 class Solution {10 public:11 void flatten(TreeNode *root) {12 ... 阅读全文
posted @ 2014-03-18 17:03 青轰的后花园 阅读(135) 评论(0) 推荐(0) 编辑
摘要: Given an array where elements are sorted in ascending order, convert it to a height balanced BST.构建平衡二叉排序树(AVL)注意到平衡二叉排序树的根节点是数组的中间点就好,之后是递归。 1 #include 2 #include 3 using namespace std; 4 5 struct TreeNode { 6 int val; 7 TreeNode *left; 8 TreeNode *right; 9 TreeNode(int x) : val(... 阅读全文
posted @ 2014-03-14 20:50 青轰的后花园 阅读(116) 评论(0) 推荐(0) 编辑
摘要: 单链表含环问题总结:给定一个单链表,只给出头指针h:1、如何判断是否存在环?2、如何知道环的长度?3、如何找出环的连接点在哪里?4、带环链表的长度是多少? 解法:1、对于问题1,使用追赶的方法,设定两个指针slow、fast,从头指针开始,每次分别前进1步、2步。如存在环,则两者相遇;如不存在环,fast遇到NULL退出。2、对于问题2,记录下问题1的碰撞点p,slow、fast从该点开始,再次碰撞所走过的操作数就是环的长度s。3、问题3:有定理:碰撞点p到连接点的距离=头指针到连接点的距离,因此,分别从碰撞点、头指针开始走,相遇的那个点就是连接点。4、问题3中已经求出连接点距离头指针的长度, 阅读全文
posted @ 2014-03-14 15:16 青轰的后花园 阅读(212) 评论(0) 推荐(0) 编辑
摘要: Given preorder and inorder traversal of a tree, construct the binary tree.给了前序和中序遍历,这个题也好做,主要问题是把左右分支的下标给搞清楚。对于前序来说,本次的根节点总是第一个preorder[0];左子树坐标是[prestart+1,prestart+i],右子树是[prestart+i+1,preend];而对于中序来说,如果根节点是第i个(从instart开始数),那么左边子树的坐标是[instart,instart+i-1],右子树是[instart+i+1,inend];如果觉得前序不好算的话,可以先算中序 阅读全文
posted @ 2014-03-13 21:46 青轰的后花园 阅读(150) 评论(0) 推荐(0) 编辑
摘要: Mergeksorted linked lists and return it as one sorted list. Analyze and describe its complexity.外部排序的基础 1 #include 2 #include 3 using namespace std; 4 5 struct ListNode { 6 int val; 7 ListNode *next; 8 ListNode(int x) : val(x), next(NULL) {} 9 };10 class Solution {11 public:12 stat... 阅读全文
posted @ 2014-03-09 18:04 青轰的后花园 阅读(119) 评论(0) 推荐(0) 编辑
摘要: Path SumGiven a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 ... 阅读全文
posted @ 2014-03-09 17:18 青轰的后花园 阅读(571) 评论(0) 推荐(0) 编辑
摘要: 对于堆排序,其实就是一个路径上的冒泡的过程,所以可以用这个思想去写代码,思路:每次父节点都和他的左子树和右子树比较,三者中最大的那个与父节点交换位置,这样递归之后根节点存放的就是该次遍历最大的值,之后将根节点与最后的节点交换,在进行查找(0~(lenth-1))中的最大值与倒数第二个交换位置就OK了。这样自己比较好理解。 1 #include 2 using namespace std; 3 4 void swap(int &a, int &b){ 5 int tmp = a; 6 a = b; 7 b = tmp; 8 } 9 10 int findmax(int... 阅读全文
posted @ 2014-03-08 22:25 青轰的后花园 阅读(167) 评论(0) 推荐(0) 编辑