摘要: 回文partition一般有以下几种情况:1. 求所有的partition结果。这个一般用DFS来解。2. 最后结果是一个整数,比如Palindrome Partitioning II。这个用DP来解3. 最后求一个结果,比如最小切法。这个用DP+backtrack来解4. 最长回文子串问题1:求所有的回文partition结果,用简单的DFS就可以求出。 bool isPalin(string &s,int l,int r) { while(l> partition(string s) { // Start typing your C/C++ solu... 阅读全文
posted @ 2013-09-20 23:18 summer_zhou 阅读(197) 评论(0) 推荐(0) 编辑
摘要: Q: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.A: 树的问题,第一个考虑就是递归解决。对于root为根节点的子树,maxPathSum的来源有以下几种情况:1. root左子树 2. root右子树 3. 路径穿过root:分为三种情况:来自左子树+root;来自右子树+root;左子树+roo... 阅读全文
posted @ 2013-09-20 20:17 summer_zhou 阅读(129) 评论(0) 推荐(0) 编辑
摘要: Q:Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete at mosttwotransactions.Note:You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).A: 允许最多交易 阅读全文
posted @ 2013-09-20 16:45 summer_zhou 阅读(176) 评论(0) 推荐(0) 编辑