Leetcode
我把葡萄酿成酒
吾日三省吾身
展开
-
Leetcode---Reverse Linked List
Reverse a singly linked list.很简单的一道题,问题在于要避免翻转后链表尾部的环。ListNode* reverseList(ListNode* head) { if(head==NULL) return NULL; ListNode * header=new ListNode(-1); ListNode * p0=原创 2015-05-06 22:04:58 · 373 阅读 · 0 评论 -
Leetcode---Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?很简单,就是求卡特兰数,直接公式求 int numTrees(int n) { vector num(n+1); num[0]=1; num[1]=1; num[原创 2015-04-11 09:47:55 · 381 阅读 · 0 评论 -
Leetcode---N-Queens
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.著名的n皇后问题,要求记录摆放位置,下面几行是关键:void dfs(int level){ if(level==N){原创 2015-04-11 17:11:45 · 369 阅读 · 0 评论 -
Leetcode---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 exam原创 2015-04-06 18:09:25 · 435 阅读 · 0 评论 -
Leetcode---Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.这个有所变化,是要求输出卡特兰数的结果,可以用DFS。以每个节点作为树的根节点,生成一系列对应的二叉树。 vector generateTrees(int n) { re原创 2015-04-11 09:50:45 · 353 阅读 · 0 评论 -
Leetcode---Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.由树的前序遍历序列和中序遍历序列构建二叉树1. 在中序序列中找到根节点2. 把中序序列拆分成左右两个中序序列3. 找对应的前序序列,注意,对应的中序和前序序列长度相同4. 递归TreeNode *buildTree(ve原创 2015-04-11 10:34:06 · 494 阅读 · 0 评论 -
Leetcode---Valid Number
Validate if a given string is numeric.Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement to be ambiguo原创 2015-03-07 20:15:15 · 501 阅读 · 0 评论 -
Leetcode---Pow(x, n)
求幂,杨幂。。。好吧,很冷solutions: double pow(double x, int n) { if(n==0) return 1; else if(n>0){ if(n%2==0){ double temp=pow(x,n/2);原创 2015-03-08 21:09:07 · 373 阅读 · 0 评论 -
Leetcode---Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.先举几个例子:1的平方根是11的平方根是12的平方根是13的平方根是14的平方根是2所以,当x>=1时,它的平方根应该在[1, x/2+1]内,当x>=2时,它的平方根在[1,x/原创 2015-03-08 12:03:37 · 377 阅读 · 0 评论 -
Leetcode---Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers原创 2015-01-03 18:04:27 · 451 阅读 · 0 评论 -
Leetcode---Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree,原创 2015-01-02 12:02:45 · 419 阅读 · 0 评论 -
Leetcode---Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.这题直观的做法就是先序遍历,一个节点一个节点的塞到list里面,但是为了避免毁掉原本的链接,所以在递归中使用临时变量存储节点 TreeNode * p=NULL; void f(TreeNode * root){原创 2015-01-02 12:02:32 · 492 阅读 · 0 评论 -
Leetcode---Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never di原创 2015-01-02 12:02:26 · 360 阅读 · 0 评论 -
Leetcode---N-Queens II
N皇后问题,求数量,比记录轨迹简单。int N;int sum;vector > m;bool check(int row,int column){ if(row==1) return true; int i,j; for(i=0;i<=row-2;i++){ if(m[i][原创 2015-04-11 17:18:42 · 453 阅读 · 0 评论 -
Leetcode---Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.给后序序列和中序序列,求二叉树。这个怎么说呢,和上一道题如出一辙,只不过是顺序变了,简单!TreeNode * f(vector &inorder, vector &postorder, int inl,int inr,int p原创 2015-04-11 10:38:48 · 427 阅读 · 0 评论 -
Leetcode---Clone Graph
这道题可以用DFS和BFS分别完成。要说DFS和BFS讲的透彻的,还是算法导论,下面给出算法导论上的伪代码,注意,其中任何一行code都是非常值得玩味的:BFS伪代码BFS(G, s) 1 for each vertex u ∈ V [G] - {s} 2 do color[u] ← WHITE 3 d[u] ← ∞ 4原创 2015-04-06 17:44:37 · 357 阅读 · 0 评论 -
Leetcode---Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.原创 2015-01-02 12:02:20 · 384 阅读 · 0 评论 -
Leetcode---Path Sum II
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 and sum = 22, 5 /原创 2015-01-02 12:02:40 · 314 阅读 · 0 评论 -
Leetcode---Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1].原创 2015-04-12 13:59:56 · 369 阅读 · 0 评论 -
Leetcode---Surrounded Regions
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原创 2015-04-12 12:58:27 · 519 阅读 · 0 评论 -
Leetcode---Path Sum
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 a原创 2015-01-02 12:02:42 · 375 阅读 · 0 评论 -
Leetcode---Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character '.'.You may assume that there will be only one unique solution.A sudoku puzzle...原创 2015-01-02 12:01:19 · 345 阅读 · 0 评论 -
Leetcode---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.比较奇葩的设定,要是没有左子树,那么就要算右子树的最小高度原创 2015-01-02 12:02:29 · 339 阅读 · 0 评论 -
Leetcode---Permutations
Given a collection of numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].Show Tags原创 2015-04-06 18:17:52 · 331 阅读 · 0 评论 -
Leetcode---Recover Binary Search Tree
一个二叉排序树的某两个节点颠倒了为了,去恢复它。不可思议的是这道题居然被标记为了hard,其实难度远低于其他的hard或medium。思路:中序遍历,然后看是不是递增的,如果不是,记录下来。注意,可能有一次逆序,也可能有两次,一次的发生在相邻节点。注意,中序遍历是这样的:void f(node * root){if(root==NULL)return;f原创 2015-04-06 17:32:42 · 562 阅读 · 0 评论 -
Leetcode---word Break II
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.Return all such possible sentences.For example, givens = "原创 2015-04-06 17:33:19 · 463 阅读 · 0 评论 -
Leetcode---Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()原创 2015-04-06 18:20:46 · 421 阅读 · 0 评论 -
Leetcode---Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the tota原创 2015-04-06 17:38:22 · 410 阅读 · 0 评论 -
Leetcode---Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value.很像那个镜原创 2015-01-02 12:02:23 · 383 阅读 · 0 评论 -
Leetcode---Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3原创 2015-01-02 12:02:17 · 396 阅读 · 0 评论 -
Leetcode---Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.我试着用了最简单的匹配,居然过了,以后再用KMP之类的好好研究一下。 char *str原创 2015-01-02 12:02:12 · 739 阅读 · 0 评论 -
Leetcode---word ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a t原创 2015-01-02 12:01:30 · 290 阅读 · 0 评论 -
Leetcode---Surrounded Regions
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.原创 2015-01-02 12:01:22 · 425 阅读 · 0 评论 -
Leetcode---Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not原创 2015-01-02 12:01:11 · 325 阅读 · 0 评论 -
Leetcode---Validate Binary Search Tree
需要牢记BST的定义:左边的所有节点都小于根节点,根节点小于右边的所有节点所以这样的做法是错误的: bool isValidBST(TreeNode *root) { if(root==NULL) return true; bool l=true;原创 2015-01-02 12:02:37 · 363 阅读 · 0 评论 -
Leetcode---Recover Binary Search Tree
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 f原创 2015-01-02 12:02:34 · 346 阅读 · 0 评论 -
Leetcode---Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.原创 2015-01-02 12:02:09 · 351 阅读 · 0 评论 -
Leetcode---Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree,原创 2015-01-02 12:02:06 · 391 阅读 · 0 评论 -
Leetcode---Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses subst原创 2015-01-02 12:01:58 · 288 阅读 · 0 评论 -
Leetcode---Divide Two Integers
Divide two integers without using multiplication, division and mod operator.整数的除法,但无法用乘号,除号和余号。可以考虑二分,假设A/B, 则解空间在[0, A]当B*A/2注意这里A/2可能是解,因为整数的除法是舍去余数的当B*A/2>A时,表明解在[0, A/2)之间,当B*A/2==原创 2015-01-02 12:01:50 · 308 阅读 · 0 评论