上一页 1 ··· 6 7 8 9 10 11 下一页
摘要: Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofeverynode never differ by more than 1./** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left;... 阅读全文
posted @ 2013-02-28 22:06 一只会思考的猪 阅读(129) 评论(0) 推荐(0) 编辑
摘要: 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 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 at 阅读全文
posted @ 2013-02-28 21:25 一只会思考的猪 阅读(116) 评论(0) 推荐(0) 编辑
摘要: Say you have an array for which theithelement is the price of a given stock on dayi.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.这个问题挺有意思,刚开始想到了DP,和最大递增子列有点像 C[i] = max{0 < j < i C[j 阅读全文
posted @ 2013-02-28 20:43 一只会思考的猪 阅读(119) 评论(0) 推荐(0) 编辑
摘要: LCS问题的递推公式是:C[i,j]->A的前i和元素和B的前j个元素的最大公共子列的长度。C[i,j] = max{C[i-1,j-1] + same{A[i],B[j]}, C[i -1,j], C[i,j-1]}.实际求解的时候,可以用递推的方法,因为子问题大量重合的情况出现[这才是DP的真正含义]。算法复杂度O(MN).回溯输出最长公共子序列过程:算法分析:由于每次调用至少向上或向左(或向上向左同时)移动一步,故最多调用(m + n)次就会遇到i = 0或j = 0的情况,此时开始返回。返回时与递归调用时方向相反,步数相同,故算法时间复杂度为Θ(m + n)。void FindL 阅读全文
posted @ 2013-02-28 00:40 一只会思考的猪 阅读(195) 评论(0) 推荐(0) 编辑
摘要: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama"is a palindrome."race a car"isnota palindrome.Note:Have you consider that the string might be empty? This is a good question to 阅读全文
posted @ 2013-02-27 22:44 一只会思考的猪 阅读(189) 评论(0) 推荐(0) 编辑
摘要: Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given[100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is[1, 2, 3, 4]. Return its length:4.Your algorithm should run in O(n) complexity.class Solution {public: struct Node{... 阅读全文
posted @ 2013-02-26 23:27 一只会思考的猪 阅读(491) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.An example is the root-to-leaf path1->2->3which represents the number123.Find the total sum of all root-to-leaf numbers.For example, 1 / \ 2 3The root-to-leaf path1->2represents the number12.T 阅读全文
posted @ 2013-02-26 22:03 一只会思考的猪 阅读(184) 评论(0) 推荐(0) 编辑
摘要: Given two words (startandend), and a dictionary, find the length of shortest transformation sequence fromstarttoend, such that:Only one letter can be changed at a timeEach intermediate word must exist in the dictionaryFor example,Given:start="hit"end="cog"dict=["hot",&q 阅读全文
posted @ 2013-02-15 22:47 一只会思考的猪 阅读(233) 评论(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 toNULL.Initially, all next pointers are set toNULL.N... 阅读全文
posted @ 2013-02-15 00:48 一只会思考的猪 阅读(137) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1return[ [5,4,11,2]... 阅读全文
posted @ 2013-02-15 00:22 一只会思考的猪 阅读(203) 评论(0) 推荐(0) 编辑
上一页 1 ··· 6 7 8 9 10 11 下一页