文章分类 -  leetcode

摘要:Q:The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.For example, given n = 2, return [0,1,3,2]. Its gray 阅读全文
posted @ 2013-07-23 00:40 dmthinker 阅读(91) 评论(0) 推荐(0)
摘要:Q:Given an array Sof nintegers, are there elements a, b, cin Ssuch that a+ b+ c= 0? Find all unique triplets in the array which gives the sum of zero.Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a? b? c) The solution set must not contain duplicate triplets.For example, g 阅读全文
posted @ 2013-07-22 23:55 dmthinker 阅读(120) 评论(0) 推荐(0)
摘要:Q: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], []]A:每次将所有相同的元素作... 阅读全文
posted @ 2013-07-22 21:26 dmthinker 阅读(92) 评论(0) 推荐(0)
摘要:Q:Reverse a linked list from positionmton. Do it in-place and in one-pass.For example:Given1->2->3->4->5->NULL,m= 2 andn= 4,return1->4->3->2->5->NULL.Note:Givenm,nsatisfy the following condition:1 ?m?n? length of list.A:其实还是比较简单的,但是凭想象去交换元素的时候还是让我很烦躁,空间思维能力极差无比。。。。。。。。没 阅读全文
posted @ 2013-07-21 23:08 dmthinker 阅读(88) 评论(0) 推荐(0)
摘要:Q:Givenn, generate all structurally uniqueBST's(binary search trees) that store values 1...n.For example,Givenn= 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ ... 阅读全文
posted @ 2013-07-21 22:38 dmthinker 阅读(101) 评论(0) 推荐(0)
摘要:Q: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-07-21 21:44 dmthinker 阅读(106) 评论(0) 推荐(0)
摘要:Q:A:mirrors中序遍历,发生错误的节点保存,最后交换,主要分两种情况,错误的两个节点相邻或者不相邻。还是有点烦躁的。class Solution { public: void recoverTree(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function TreeNode* pre = NULL; TreeNode* cur = root; TreeNode* wrong_node1 = NULL; Tre... 阅读全文
posted @ 2013-07-08 21:05 dmthinker 阅读(102) 评论(0) 推荐(0)
摘要:Q: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 阅读全文
posted @ 2013-07-07 18:37 dmthinker 阅读(84) 评论(0) 推荐(0)
摘要:Q: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 3But the following is not: 1 / \ 2 2 \ \ 3 3Note:Bonus points if you could solve it both recursively and itera... 阅读全文
posted @ 2013-07-07 18:26 dmthinker 阅读(139) 评论(0) 推荐(0)
摘要:Q:Given a binary tree, return thelevel ordertraversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its level order traversal as:[ [3], [9,20], [15,7]]A:/** * Definition for binary tree * str... 阅读全文
posted @ 2013-07-07 16:26 dmthinker 阅读(106) 评论(0) 推荐(0)
摘要:Q: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-07-07 16:23 dmthinker 阅读(115) 评论(0) 推荐(0)
摘要:Q:Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.A:/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), righ... 阅读全文
posted @ 2013-07-07 01:13 dmthinker 阅读(119) 评论(0) 推荐(0)
摘要:Q:Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.A:没啥好说的,细心是王道class Solution {public: TreeNode *buildTree(vector &inorder, vector &postorder) { // Start typing your C/C++ solution below // DO NO... 阅读全文
posted @ 2013-07-07 01:07 dmthinker 阅读(129) 评论(0) 推荐(0)
摘要:Q:Given a binary tree, return thebottom-up level ordertraversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its bottom-up level order traversal as:[ [15,7] [9,20], [3],]co... 阅读全文
posted @ 2013-07-07 00:17 dmthinker 阅读(115) 评论(0) 推荐(0)
摘要:Q: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.A:跟I类似,还是先保 阅读全文
posted @ 2013-07-06 21:12 dmthinker 阅读(105) 评论(0) 推荐(0)
摘要:Q: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"] ]A:对于字符串A,A[i]代表从0到i的子字符串的回文子串集合,A[i]依赖于A[0 阅读全文
posted @ 2013-07-06 20:49 dmthinker 阅读(125) 评论(0) 推荐(0)
摘要:Q: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"isnota palindrome.Note:Have you consider that the string might be empty? This is a good question 阅读全文
posted @ 2013-07-03 23:56 dmthinker 阅读(95) 评论(0) 推荐(0)
摘要:Q:Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6Hints:If you notice carefull... 阅读全文
posted @ 2013-07-03 00:10 dmthinker 阅读(124) 评论(0) 推荐(0)
摘要:Q: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 represents the number123.Find the total sum of all root-to-leaf numbers.For example, 1 / \ 2 3The root-to-leaf path1->2represents the number12 阅读全文
posted @ 2013-07-02 21:36 dmthinker 阅读(117) 评论(0) 推荐(0)
摘要:Q: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","d 阅读全文
posted @ 2013-07-02 00:36 dmthinker 阅读(138) 评论(0) 推荐(0)