- 博客(32)
- 收藏
- 关注
原创 77. Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]
2016-12-31 23:47:13 202
原创 60. Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3):"123""132""213""231""3
2016-12-31 22:41:10 202
原创 46, 47 Permutations I, II
1:Given a collection of distinct 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],
2016-12-31 10:47:48 216
原创 39,40,216 Combination Sum I II III
1:Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be
2016-12-31 00:06:40 275
原创 22. 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:[ "((()))", "(()())", "(())()", "()(())
2016-12-30 08:15:47 243
原创 17. Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit st
2016-12-28 06:25:18 308
原创 133. Clone Graph :一个典型的DFS
题目:Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.代码:/** * Definition for undirected graph. * struct UndirectedGraphNode { * int label;
2016-12-13 23:23:59 321
原创 109. Convert Sorted List to Binary Search Tree
题目:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.解法:/** * Definition for singly-linked list. * struct ListNode { * int val
2016-12-13 00:58:01 310
原创 145. Binary Tree Postorder Traversal 树后续遍历 flag方法和reverse方法
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas
2016-12-11 23:37:03 323
原创 144. Binary Tree Preorder Traversal 二叉树的前序遍历
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas
2016-12-11 22:38:09 261
原创 129. Sum Root to Leaf Numbers 注意要和124题对比,都是DFS问题
题目: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
2016-12-11 22:25:09 206
原创 124. Binary Tree Maximum Path Sum
题目:Given a binary tree, find the maximum path sum.For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connecti
2016-12-11 21:39:34 250
原创 117. Populating Next Right Pointers in Each Node II 非常精炼的代码,值得好好体会
题目:Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?Note:You may only use
2016-12-09 22:41:27 190
原创 116. Populating Next Right Pointers in Each Node
题目:Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next rig
2016-12-09 21:17:04 172
原创 114. Flatten Binary Tree to Linked List, pre节点的妙用
题目: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:
2016-12-09 20:13:57 178
原创 113. 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
2016-12-09 16:47:18 265
原创 110. 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 neve
2016-12-09 15:19:57 476
原创 105. Construct Binary Tree from Preorder and Inorder Traversal,前序+中序 构建 树
观察: A / \ B C / \ D Epreorder: A B D E Cinorder: D B E A C 规律:对于inorder序列,最左边的一定是整个树的leftmost。这样,从左☞右遍历pre,直到pre[i] == in[0], 便找到了le
2016-12-07 21:00:02 395
原创 104. Maximum Depth of Binary Tree 求树的最大深度
1,my solution/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
2016-12-06 23:03:58 273
原创 103. Binary Tree Zigzag Level Order Traversal
vectorvectorint> > zigzagLevelOrder(TreeNode* root) { if (root == NULL) { return vectorvectorint> > (); } vectorvectorint> > result; queue nodesQueue; nodesQueue.push(root
2016-12-06 22:44:52 178
原创 102. Binary Tree Level Order Traversal 树的层序遍历 递归 和 队列方法
题目:Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree [3,9,20,null,null,15,7], 3 /
2016-12-06 18:58:01 246
原创 101. Symmetric Tree
题目:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \3
2016-12-06 17:48:34 179
原创 99. Recover Binary Search Tree
题目:基础题,代码里给出了三种解答方法,其中Morris法是O(1)space。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) spa
2016-12-05 21:28:58 562
转载 彻底理解线索二叉树
一、线索二叉树的原理 通过考察各种二叉链表,不管儿叉树的形态如何,空链域的个数总是多过非空链域的个数。准确的说,n各结点的二叉链表共有2n个链域,非空链域为n-1个,但其中的空链域却有n+1个。如下图所示。 因此,提出了一种方法,利用原来的空链域存放指针,指向树中其他结点。这种指针称为线索。 记ptr指向二叉链表中的一个结点,以下是建立线索的规
2016-12-05 17:18:42 649
原创 98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST).class Solution {public: bool isValidBST(TreeNode* root) { TreeNode* prev = NULL; return validate(r
2016-12-05 15:24:25 162
原创 149. Max Points on a Line
题目:Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.总结:浪费了一个多小时, 总结,头脑不清醒的时候,就不要编代码了。休息十分钟,然后清醒的投入战斗。代码:/** * Definition for a point.
2016-12-05 01:18:53 203
转载 138. Copy List with Random Pointer
题目复制带有随机指针的链表:一个单链表除了next指针外还有一个random指针随机指向任何一个元素(可能为空) 《剑指offer》上的面试题26分析方法一: map,先按普通方法复制链表,再两个链表同时走复制random(旧节点a,新节点a’)a'->random=map[a->random](空指针单独处理) 方法二: 插入:每个节点后面插入一个自身的“复本
2016-12-04 23:14:02 222
原创 136. Single Number, leetcode
题目:Given an array of integers, every element appears twice except for one. Find that single one.解法1,hash map 如代码中注释掉的部分;解法2,bit manipulation 异或法class Solution {public: int singleNumbe
2016-12-04 21:37:32 200
原创 94. Binary Tree Inorder Traversal
题目:Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree [1,null,2,3], 1 \ 2 / 3return [1,3,2].解法:Hi,
2016-12-04 21:14:21 180
原创 76. Minimum Window Substring, leetcode
题目:Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S = "ADOBECODEBANC"T = "ABC"Minimum windo
2016-12-04 17:32:23 271
原创 49. Group Anagrams, leetcode
Given an array of strings, group anagrams together.For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return:[ ["ate", "eat","tea"], ["nat","tan"], ["bat"]]Note: Al
2016-12-01 20:53:46 224
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人