上一页 1 ··· 6 7 8 9 10 11 12 13 14 ··· 16 下一页
摘要: 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], []][解题思路]这个就是排列组合中的排列问题。递归如下逻辑:F... 阅读全文
posted @ 2014-02-03 02:21 Razer.Lu 阅读(244) 评论(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.[解题思路]数组,O(n)---->使用hash,空间换时间对于当前数N 阅读全文
posted @ 2014-02-03 02:06 Razer.Lu 阅读(235) 评论(0) 推荐(0) 编辑
摘要: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.Ref:http://www.cnblogs.com/feiling/p/3267... 阅读全文
posted @ 2014-02-03 01:39 Razer.Lu 阅读(297) 评论(0) 推荐(0) 编辑
摘要: DFS 1 public class Solution { 2 public ArrayList> combine(int n, int k) { 3 ArrayList> res = new ArrayList>(); 4 if(k == 0 || n == 0) 5 return res; 6 7 ArrayList output = new ArrayList(); 8 generate(1, n, k, output, res); 9 return ... 阅读全文
posted @ 2014-02-02 02:42 Razer.Lu 阅读(213) 评论(0) 推荐(0) 编辑
摘要: Given a stringsconsists of upper/lower-case alphabets and empty space characters' ', return the length of last word in the string.If the last word does not exist, return 0.Note:A word is defined as a character sequence consists of non-space characters only.For example,Givens="Hello Worl 阅读全文
posted @ 2014-02-01 17:27 Razer.Lu 阅读(239) 评论(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 rep... 阅读全文
posted @ 2014-02-01 09:19 Razer.Lu 阅读(155) 评论(0) 推荐(0) 编辑
摘要: [解题思路]记录下一层元素个数,这样每次遍历时就知道何时结束,只需一个queue用一个count来计数每一层的node的个数,/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public ArrayList> levelOrder(TreeNode root) { ... 阅读全文
posted @ 2014-02-01 08:31 Razer.Lu 阅读(186) 评论(0) 推荐(0) 编辑
摘要: [解题思路]二叉树的层序遍历通过使用queue来实现,上网搜索了下,发现该题可以使用栈来解决,通过分析执行结果确实是后进先出这里通过定义leftToRight来表示是从左到右还是从右到左从左到右:先加left后加right从右到左:先加right后加leftorderlevel traversal 遍历最后每行添加到result前 判断是leftToRight 还是Right to Left/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * Tr... 阅读全文
posted @ 2014-02-01 08:05 Razer.Lu 阅读(182) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, return theinordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,3,2].Note:Recursive solution is trivial, could you do it iteratively?Recursive Version/** * Definition for binary tree * public class TreeNode { * int val;... 阅读全文
posted @ 2014-02-01 03:05 Razer.Lu 阅读(198) 评论(0) 推荐(0) 编辑
摘要: 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./** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { v... 阅读全文
posted @ 2014-02-01 02:08 Razer.Lu 阅读(141) 评论(0) 推荐(0) 编辑
上一页 1 ··· 6 7 8 9 10 11 12 13 14 ··· 16 下一页