码迷,mamicode.com
首页 >  
搜索关键字:题意    ( 33207个结果
Leetcode 树 Binary Tree Zigzag Level Order Traversal
题意:给定一棵二叉树,返回按zigzag层次遍历的结果 思路: 还是跟前面的Binary Tree Level Order Traversal的思路一样 即从上往下按层遍历二叉树,将每一层的节点存放到该层对应的数组中 最后将得到的总数组中奇数层(从0层开始计数)的子数组reverse一下就可以了 复杂度:时间O(n),空间O(n)...
分类:其他好文   时间:2014-05-15 15:15:50    阅读次数:374
Leetcode 树 Binary Tree Level Order Traversal
题意:给定一棵二叉树,返回按层遍历的结果 思路1:bfs,定义一个新的struct,记录指针向节点的指针和每个节点所在的层 复杂度1:时间O(n),空间O(n) 思路2:dfs 递归函数: void levelOrder(TreeNode *root, int level, vector<vector >&result) 表示把根为root的树按层存放在result中,其中level表示当前的层数 复杂度2:时间O(n),空间O(n) 相关题目:...
分类:其他好文   时间:2014-05-15 14:46:06    阅读次数:355
Leetcode 树 Populating Next Right Pointers in Each Node
题意:给定一棵perfect binary tree,将它每一个节点的next指针都指向该节点右边的节点 思路:dfs 在connect一棵树的时候,需要知道这棵树的根节点和它右边的节点 1.将树的根节点和它右边的节点连接起来 2.递归地将左子树connect起来,需要知道左子树节点和右子树节点 3.递归地将右子树connect起来,需要知道右子树节点和根右边的节点的左子树节点 递归函数为: void connect(TreeLinkNode *root, TreeLinkNode *sibling) 表...
分类:其他好文   时间:2014-05-15 07:14:04    阅读次数:289
Leetcode 线性表 Linked List Cycle
题意:判断一个链表中是否有环 思路:快慢指针,如果有环,最终快慢指针会在非NULL相遇 注:用到fast->next前先要确保fast非NULL,要用fast->next->next前先要确保fast,fast->next非NULL 复杂度:时间O(n), 空间O(1) 相关题目:Linked List CycleII...
分类:其他好文   时间:2014-05-15 07:01:57    阅读次数:219
Leetcode 树 Binary Tree Level Order Traversal II
题意:从底往上按层遍历二叉树 思路: 思路和Binary Tree Level Order Traveral 一样, 即从上往下按层遍历二叉树,将每一层的节点存放到该层对应的数组中 最后将得到的数组倒转一下就可以了 按层遍历二叉树可用bfs,也可用dfs,但都要记录节点所在的层 复杂度:时间O(n), 空间O(n)...
分类:其他好文   时间:2014-05-15 06:32:14    阅读次数:278
Leetcode 树 Binary Tree Inorder Traversal
题意:中序遍历 思路:采用递归实现。因为函数声明是返回一个vector,所以每个子树返回的是该子树的中序遍历的结果 按照 左、根、右的次序把根和左右子树的vector合并起来就可以了...
分类:其他好文   时间:2014-05-15 06:21:09    阅读次数:255
poj3737(三分搜索)
题意:给出一个圆锥的表面积(侧面积+底面积),求圆锥的最大体积。 解法:三分半径。左边界随便取个极小的数,右边界可以假定这个圆锥是平的,高是0.这是底面积的二倍是表面积。 代码:/****************************************************** * author:xiefubao ***************************...
分类:其他好文   时间:2014-05-15 04:58:12    阅读次数:234
leetCode-002 Median of Two Sorted Arrays
leetCode-002 Median of Two Sorted Arrays 【题目】 There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 【题意】 有两个有序的数组,找出这两数组整合后的中位数,要求时间复杂度O(nlogn)...
分类:其他好文   时间:2014-05-15 04:42:05    阅读次数:274
UVa 10308 Roads in the North 树的直径
题目来源:UVa 10308 Roads in the North 题意:求距离最远的2点之间的距离 思路:裸的树的直径 或者树形DP #include #include #include using namespace std; const int maxn = 100010; struct node { int to, w; node(){} node(int to, int...
分类:其他好文   时间:2014-05-15 03:37:18    阅读次数:295
LeetCode-005 Longest Palindromic Substrin
【题目】 Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 【题意】 题意是找出字符串S中最长回文子串,S最长为1000,保证有唯一解 【思路】 原字符串用特殊字符#间隔,如下所示: #a...
分类:其他好文   时间:2014-05-15 03:31:25    阅读次数:299
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!