上一页 1 ··· 4 5 6 7 8 9 10 11 下一页
摘要: Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...n?For example,Givenn= 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 ... 阅读全文
posted @ 2013-06-14 21:23 一只会思考的猪 阅读(258) 评论(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], []]思路:构造子集有三种方法,分别是:1) DFS 和排列的思... 阅读全文
posted @ 2013-06-14 20:19 一只会思考的猪 阅读(181) 评论(0) 推荐(0) 编辑
摘要: Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).The replacement must be in-place, do not allocate extra memory.He 阅读全文
posted @ 2013-06-14 19:02 一只会思考的猪 阅读(240) 评论(0) 推荐(0) 编辑
摘要: Given two words (startandend), and a dictionary, find all shortest transformation sequence(s) 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","dot 阅读全文
posted @ 2013-06-13 15:24 一只会思考的猪 阅读(175) 评论(0) 推荐(0) 编辑
摘要: 今天看到有人面到一类似题,但是更简单一些。给定一个随机函数可以按照0.5的概率返回true,如何实现一个函数按照1/2^N的概率返回true。这让我想到了去年做过的一道Facebook的面试题。那道题比这个更难,要求实现一个函数随机返回任意概率的true。以下是我当时给出的解答。由于我给出了这个解,使得我有幸认识了版上的几个大牛,包括LeetCode。本来想把这个写到我的算法路里,不过由于正好有人面到了类似题,就给大家参考一下吧。boolean Prob(){return newRandom().nextInt(2)==0;}boolean Prob2(double p, boolean ex 阅读全文
posted @ 2013-06-12 18:38 一只会思考的猪 阅读(363) 评论(0) 推荐(0) 编辑
摘要: Given a 2D board containing'X'and'O', capture all regions surrounded by'X'.A region is captured by flipping all'O's into'X's in that surrounded region .For example,X X X XX O O XX X O XX O X XAfter running your function, the board should be:X X X XX X X XX X X 阅读全文
posted @ 2013-06-12 17:15 一只会思考的猪 阅读(225) 评论(0) 推荐(0) 编辑
摘要: Given a strings, partitionssuch that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning ofs.For example, givens="aab",Return1since the palindrome partitioning["aa","b"]could be produced using 1 cut.思路:第一种按照DFS搜索 阅读全文
posted @ 2013-06-11 23:29 一只会思考的猪 阅读(324) 评论(0) 推荐(0) 编辑
摘要: Given a strings, partitionssuch that every substring of the partition is a palindrome.Return all possible palindrome partitioning ofs.For example, givens="aab",Return [ ["aa","b"], ["a","a","b"] ] 1 class Solution { 2 public: 3 vector<ve 阅读全文
posted @ 2013-06-11 20:03 一只会思考的猪 阅读(227) 评论(0) 推荐(0) 编辑
摘要: class Solution {public: bool partition_s(string s){ if (s.empty()){ return palin_m; } for(size_t i = 0; i < s.size(); i++){ palin_v.push_back(s.substr(i,1)); } palin_m.push_back(palin_v); vector<string> temp; while(1){... 阅读全文
posted @ 2013-06-11 16:33 一只会思考的猪 阅读(168) 评论(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?思路:递归和非递归的版本都应该会,非递归的版本需要使用到stack,不是O(1)的空间。而O(1)的空间是用了Morris的方法,和Threading结合起来,第一遍先进行中序Th... 阅读全文
posted @ 2013-06-11 16:27 一只会思考的猪 阅读(297) 评论(0) 推荐(0) 编辑
上一页 1 ··· 4 5 6 7 8 9 10 11 下一页