摘要: SearchingAlgorithmData StructureTime ComplexitySpace ComplexityAverageWorstWorstDepth First Search (DFS)Graph of |V| vertices and |E| edges-O(|E| + |V|)O(|V|)Breadth First Search (BFS)Graph of |V| vertices and |E| edges-O(|E| + |V|)O(|V|)Binary searchSorted array of n elementsO(log(n))O(log(n))O(1)L 阅读全文
posted @ 2014-01-28 11:15 WinsCoder 阅读(240) 评论(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 @ 2014-01-28 10:48 WinsCoder 阅读(110) 评论(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-01-28 10:43 WinsCoder 阅读(169) 评论(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],]Code:class Solution {public: void findCombination(int &n, int &k, int level, vector &buf, vector> &res){ if(buf. 阅读全文
posted @ 2014-01-28 10:23 WinsCoder 阅读(132) 评论(0) 推荐(0) 编辑
摘要: Given a collection of integers that might contain duplicates,S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,IfS=[1,2,2], a solution is:[ [2], [1], [1,2,2], [2,2], [1,2], []]Code:class Solu... 阅读全文
posted @ 2014-01-28 10:16 WinsCoder 阅读(103) 评论(0) 推荐(0) 编辑
摘要: Given a set of distinct integers,S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,IfS=[1,2,3], a solution is:[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []]Code:class Solution {public: ... 阅读全文
posted @ 2014-01-28 10:11 WinsCoder 阅读(133) 评论(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 @ 2014-01-28 09:56 WinsCoder 阅读(146) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ ... 阅读全文
posted @ 2014-01-28 09:49 WinsCoder 阅读(117) 评论(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 still work?Note:You may only use constant extra space.For example,Given the following binary tree, 1 / \ 2 3 / \ \ 4 5 ... 阅读全文
posted @ 2014-01-28 09:24 WinsCoder 阅读(143) 评论(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 @ 2014-01-28 09:10 WinsCoder 阅读(136) 评论(0) 推荐(0) 编辑