类似于二叉树的三种遍历,我们可以基于遍历的模板做很多额外的事情,图的两种遍历,深度和广度模板同样也可以做很多额外的事情,这里举例利用深度优先遍历的模板来进行复制,深度优先中,我们先访问第一个结点,接着访问第一个邻接点,再访问邻节点的邻节点。。。。
class Solution:
# @param node, a undirected graph node
# @return a ...
分类:
编程语言 时间:
2014-07-18 23:11:00
阅读次数:
331
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 devis...
分类:
其他好文 时间:
2014-07-18 22:33:04
阅读次数:
328
This is the same asSPOJ #453. The best way to understand DP1A code:class Solution {public: int minimumTotal(vector > &triangle) { fo...
分类:
其他好文 时间:
2014-07-18 21:19:47
阅读次数:
234
class Solution {public: void sortColors(int A[], int n) { int cnt[3] = {0}; for (int i = 0; i 0) start += cnt[i-1]; for (...
分类:
其他好文 时间:
2014-07-18 16:15:56
阅读次数:
166
class Solution {private: int queen_num; int total;public: int totalNQueens(int n) { queen_num = n; total = 0; vector h(n...
分类:
其他好文 时间:
2014-07-18 16:12:55
阅读次数:
210
The key of this problem is about details especially boundary conditions.class Solution {public: ListNode *deleteDuplicates(ListNode *head) { ...
分类:
其他好文 时间:
2014-07-18 14:36:54
阅读次数:
189
Given two integersnandk, return all possible combinations ofknumbers out of 1 ...n.For example,Ifn= 4 andk= 2, a solution is:[ [2,4], [3,4], [2,3],...
分类:
其他好文 时间:
2014-07-18 09:37:55
阅读次数:
161
class Solution {private: vector sum; vector step; set can;public: int jump(int A[], int n) { step.clear(), sum.clear(), can.clear()...
分类:
其他好文 时间:
2014-07-17 18:36:56
阅读次数:
221
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 ...
分类:
其他好文 时间:
2014-07-17 17:32:50
阅读次数:
246
A typical DFS usage, no big deal:class Solution {public: int dfs(TreeNode *p, int pval) { if( !p->left && !p->right ) { ...
分类:
其他好文 时间:
2014-07-17 09:55:25
阅读次数:
171