摘要: Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1 / \ 2 3Return6.后序遍历! 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode... 阅读全文
posted @ 2014-04-03 14:27 Eason Liu 阅读(134) 评论(0) 推荐(0) 编辑
摘要: Given a singly linked listL:L0→L1→…→Ln-1→Ln,reorder it to:L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given{1,2,3,4}, reorder it to{1,4,2,3}.OH! MY GOD! I HATE LINKED LIST!看似简单,实现起来总会遇到各种指针错误,写程序之前最好先在纸上好好画画,把各种指针关系搞搞清楚。本题的想法就是先将列表平分成两份,后一份逆序,然 阅读全文
posted @ 2014-04-03 13:31 Eason Liu 阅读(780) 评论(0) 推荐(0) 编辑
摘要: Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integers in each row are sorted from l... 阅读全文
posted @ 2014-04-02 22:55 Eason Liu 阅读(178) 评论(0) 推荐(0) 编辑
摘要: Given a set of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Thesamerepeated number may be chosen fromCunlimited number of times.Note:All numbers (including target) will be positive integers.Elements in a combination (a1,a2, … ,ak 阅读全文
posted @ 2014-04-02 21:29 Eason Liu 阅读(114) 评论(0) 推荐(0) 编辑
摘要: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).好难啊,总是会有各种边界问题。想法如下:最后从medianof two sorted arrays中看到了一种非常好的方法。原文用英文进行解释,在此我们将其翻译成汉语。该方法的核心是将原问题转变成一个寻找第k小数的问题(假设两个原序列升序排列),这样中位数实际上是第(m+n 阅读全文
posted @ 2014-04-02 18:48 Eason Liu 阅读(256) 评论(0) 推荐(0) 编辑
摘要: 题目描述:又到毕业季,很多大公司来学校招聘,招聘会分散在不同时间段,小明想知道自己最多能完整的参加多少个招聘会(参加一个招聘会的时候不能中断或离开)。输入:第一行n,有n个招聘会,接下来n行每行两个整数表示起止时间,由从招聘会第一天0点开始的小时数表示。n 2 #include 3 #include 4 #include 5 using namespace std; 6 7 struct data { 8 int st; 9 int ed;10 };11 12 bool cmp(const data &a, const data &b) {13 return ... 阅读全文
posted @ 2014-04-02 01:27 Eason Liu 阅读(231) 评论(0) 推荐(0) 编辑
摘要: Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.没啥好说的。 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(... 阅读全文
posted @ 2014-04-02 00:18 Eason Liu 阅读(147) 评论(0) 推荐(0) 编辑
摘要: Given two integersnandk, return all possible combinations ofknumbers out of 1 ...n.For example,Ifn= 4 andk= 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]DFS! 1 class Solution { 2 public: 3 void getNextComb(vector > &res, vector v, int n, int k, int idx) { 4 if (idx ... 阅读全文
posted @ 2014-04-01 23:53 Eason Liu 阅读(282) 评论(0) 推荐(0) 编辑
摘要: Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution stil... 阅读全文
posted @ 2014-04-01 23:32 Eason Liu 阅读(151) 评论(0) 推荐(0) 编辑
摘要: Sort a linked list using insertion sort.虽然算法很简单,但是链表操作起来实正是烦啊,特别要注意各种边界条件。 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 *inser... 阅读全文
posted @ 2014-04-01 23:05 Eason Liu 阅读(175) 评论(0) 推荐(0) 编辑