06 2013 档案
摘要: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
阅读全文
摘要: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) //...
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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 ...
阅读全文
摘要: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 和排列的思...
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要:今天看到有人面到一类似题,但是更简单一些。给定一个随机函数可以按照0.5的概率返回true,如何实现一个函数按照1/2^N的概率返回true。这让我想到了去年做过的一道Facebook的面试题。那道题比这个更难,要求实现一个函数随机返回任意概率的true。以下是我当时给出的解答。由于我给出了这个解,使得我有幸认识了版上的几个大牛,包括LeetCode。本来想把这个写到我的算法路里,不过由于正好有人面到了类似题,就给大家参考一下吧。boolean Prob(){return newRandom().nextInt(2)==0;}boolean Prob2(double p, boolean ex
阅读全文
摘要: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
阅读全文
摘要: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搜索
阅读全文
摘要: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
阅读全文
摘要: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){...
阅读全文
摘要: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...
阅读全文
摘要:A message containing letters fromA-Zis being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total number of ways to decode it.For example,Given encoded message"12", it cou
阅读全文
摘要: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?/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; ...
阅读全文
摘要:Given a linked list and a valuex, partition it such that all nodes less thanxcome before nodes greater than or equal tox.You should preserve the original relative order of the nodes in each of the two partitions.For example,Given1->4->3->2->5->2andx= 3,return1->2->2->4->3-
阅读全文
摘要:Given a string containing just the characters'('and')', find the length of the longest valid (well-formed) parentheses substring.For"(()", the longest valid parentheses substring is"()", which has length = 2.Another example is")()())", where the longest
阅读全文
摘要:class Solution {public: int reverse(int x) { // Start typing your C/C++ solution below // DO NOT write int main() function int c = x; int buffer[512]; int i = 0; x = abs(x); while(x){ int a = x%10; buffer[i++] = a; ...
阅读全文
浙公网安备 33010602011771号