CoderJesse  
wangjiexi@CS.PKU

2013年2月28日

摘要: 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 still work?Note:You may only use constant extra space.For example,Given the following binary tree, 1 / \ 2 3 / \ \ 4 5 ... 阅读全文
posted @ 2013-02-28 12:33 CoderJesse 阅读(122) 评论(0) 推荐(0)
 
摘要: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.Initially, all next pointers are set to NULL... 阅读全文
posted @ 2013-02-28 12:30 CoderJesse 阅读(128) 评论(0) 推荐(0)
 
摘要: Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?动规不解释。 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 // Start typing your C/C++ solution below.. 阅读全文
posted @ 2013-02-28 12:25 CoderJesse 阅读(112) 评论(0) 推荐(0)
 
摘要: Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]动规不解释。 1 class Solution { 2 public: 3 vector<vector<int> > generate(int numRows) { 4 // Start typing your C/C++ solution below 5 ... 阅读全文
posted @ 2013-02-28 12:24 CoderJesse 阅读(119) 评论(0) 推荐(0)
 
摘要: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [6,5,7], [4,1,8,3]]The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).Note:Bonus point if... 阅读全文
posted @ 2013-02-28 12:22 CoderJesse 阅读(140) 评论(0) 推荐(0)
 
摘要: Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most two transactions.Note:You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).由于最多只能 阅读全文
posted @ 2013-02-28 12:19 CoderJesse 阅读(111) 评论(0) 推荐(0)
 
摘要: Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions 阅读全文
posted @ 2013-02-28 12:12 CoderJesse 阅读(154) 评论(0) 推荐(0)
 
摘要: Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.最朴素的方法需要O(n2),需要一点小技巧就可以优化到 O(n)。将数组从头到尾扫一遍,扫的过程中记录下扫过的元 阅读全文
posted @ 2013-02-28 11:49 CoderJesse 阅读(131) 评论(0) 推荐(0)
 
摘要: 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 3Return 6.水题。可以加剪枝做到稍稍优化。class Solution {public: int max; int maxPathSum(TreeNode *root) { // Start typing your C/C++ solut... 阅读全文
posted @ 2013-02-28 11:42 CoderJesse 阅读(168) 评论(0) 推荐(0)