上一页 1 ··· 3 4 5 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 of every node never differ by more than 1.class Solution {public: bool f(TreeNode *root, int & height){ if (!root){ ... 阅读全文
posted @ 2013-07-01 23:01 一只会思考的猪 阅读(132) 评论(0) 推荐(0) 编辑
摘要: Given a set ofnon-overlappingintervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.Example 1:Given intervals[1,3],[6,9], insert and merge[2,5]in as[1,5],[6,9].Example 2:Given[1,2],[3,5],[6,7],[8 阅读全文
posted @ 2013-06-30 11:43 一只会思考的猪 阅读(175) 评论(0) 推荐(0) 编辑
摘要: MIT的算法课,看到一个很新颖的Partition的方法,代码很简洁,在也不用判断几个边界(original i#include using namespace std;//Partition a[begin,end)/** [p| = x | unknow ] i j 6 10 13 5 8 3 2 || x=6 i j j if ok 6 5 13 10 8 3 2 i j =x MIT 算法导论课的Partition算法 Partition(A,p,q) //... 阅读全文
posted @ 2013-06-30 00:21 一只会思考的猪 阅读(217) 评论(0) 推荐(0) 编辑
摘要: Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given"25525511135",return["255.255.11.135", "255.255.111.35"]. (Order does not matter)class Solution {public: vector ans; vector restoreIpAddresses(stri 阅读全文
posted @ 2013-06-29 11:33 一只会思考的猪 阅读(223) 评论(0) 推荐(0) 编辑
摘要: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Each number inCmay only be usedoncein the combination.Note:All numbers (including target) will be positive integers.Elements in a combination (a1,a2, � ,ak) must 阅读全文
posted @ 2013-06-29 10:20 一只会思考的猪 阅读(234) 评论(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 @ 2013-06-29 09:48 一只会思考的猪 阅读(195) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, reverse the nodes of a linked listkat a time and return its modified list.If the number of nodes is not a multiple ofkthen left-out nodes in the end should remain as it is.You may not alter the values in the nodes, only nodes itself may be changed.Only constant memory is allowed 阅读全文
posted @ 2013-06-22 11:17 一只会思考的猪 阅读(202) 评论(0) 推荐(0) 编辑
摘要: Implement strStr().Returns a pointer to the first occurrence of needle in haystack, ornullif needle is not part of haystack.思路:朴素算法和KMP,此外还有Karbin-ship算法。关于next的数组,只需要知道一点:求patten串i位置(包括自己)最大前缀子串的长度,这个长度其实就是将来i+1的位置失配之后,下一次j重新比较的位置。也可以理解为一个dp的过程#include <iostream>#include <string.h>#incl 阅读全文
posted @ 2013-06-20 01:52 一只会思考的猪 阅读(270) 评论(0) 推荐(0) 编辑
摘要: Implement wildcard pattern matching with support for'?'and'*'.'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matching should cover the entire input string (not partial).The function prototype should be:bool i 阅读全文
posted @ 2013-06-20 01:49 一只会思考的猪 阅读(214) 评论(0) 推荐(0) 编辑
摘要: Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?思路:BST就是对应的InOrder,1) 那么只需要InOrder,维持一个当前的Val,然后看下一个是不是比当前的大,如果不大,则记录位置,直到找到2个位置fi 阅读全文
posted @ 2013-06-15 11:10 一只会思考的猪 阅读(173) 评论(0) 推荐(0) 编辑
上一页 1 ··· 3 4 5 6 7 8 9 10 11 下一页