2013年10月17日

Combination Sum II

摘要: 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-10-17 11:42 waruzhi 阅读(121) 评论(0) 推荐(0) 编辑

2013年10月16日

Combination Sum

摘要: 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-10-16 21:58 waruzhi 阅读(190) 评论(0) 推荐(0) 编辑

Pow(x, n)

摘要: Implement pow(x,n).思路二分,然后注意特殊情况的判断。最开始提交好几次都不成功。超时是因为使用递归的时候算了两次,应该用一个变量把值存起来然后直接使用的。WA是因为忘记了负数模2可能得到-1的情况。 1 double pow(double x, int n) { 2 // Note: The Solution object is instantiated only once and is reused by each test case. 3 if(x == 0 || x == 1) 4 return x; 5 ... 阅读全文

posted @ 2013-10-16 21:06 waruzhi 阅读(446) 评论(0) 推荐(0) 编辑

Validate Binary Search Tree

摘要: Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keysless thanthe node's key.The right subtree of a node contains only nodes with keysgreater thanthe node's key.Both the left and ri 阅读全文

posted @ 2013-10-16 20:23 waruzhi 阅读(153) 评论(0) 推荐(0) 编辑

2013年10月15日

Count and Say

摘要: The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1is read off as"one 1"or11.11is read off as"two 1s"or21.21is read off as"one 2, thenone 1"or1211.Given an integern, generate thenthsequence.Note: The sequence of inte 阅读全文

posted @ 2013-10-15 14:35 waruzhi 阅读(199) 评论(0) 推荐(0) 编辑

Binary Tree Zigzag Level Order Traversal

摘要: Given a binary tree, return thezigzag level ordertraversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its zigzag level order traversal as:[ [3], ... 阅读全文

posted @ 2013-10-15 14:01 waruzhi 阅读(149) 评论(0) 推荐(0) 编辑

Add Binary

摘要: Given two binary strings, return their sum (also a binary string).For example,a ="11"b ="1"Return"100".思路:先将原字符串反转,对齐逐位进行相加,再把结果反转 1 string addBinary(string a, string b) { 2 // Note: The Solution object is instantiated only once and is reused by each test case. 3 revers 阅读全文

posted @ 2013-10-15 13:05 waruzhi 阅读(137) 评论(0) 推荐(0) 编辑

Subsets II

摘要: 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], []]思路在上一题的基础上,因为数组... 阅读全文

posted @ 2013-10-15 10:40 waruzhi 阅读(134) 评论(0) 推荐(0) 编辑

Subsets

摘要: 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], []]思路递归 1 vector > result; 2... 阅读全文

posted @ 2013-10-15 10:15 waruzhi 阅读(145) 评论(0) 推荐(0) 编辑

2013年10月11日

Minimum Depth of Binary Tree

摘要: Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.思路:递归 1 int min(int a, int b){ 2 if(a left == NULL && root->right == NULL)11 return 1;12 if(root->left == NULL... 阅读全文

posted @ 2013-10-11 16:22 waruzhi 阅读(172) 评论(0) 推荐(0) 编辑

导航