/*问题描述:n个人(编号0~(n1-1)),从0开始报数,报到(m-1)的退出,剩下的人继续从0开始报数。求胜利者的编号。
我们知道第一个人(编号一定是m%n-1) 出列之后,剩下的n1-1个人组成了一个新的约瑟夫环(以编号为k=m%n1的人开始):
k k+1 k+2 ... n1-2, n1-1, 0, 1, 2, ... k-2
并且从k开始报0。
现在我们把他们的...
分类:
其他好文 时间:
2014-12-02 10:40:18
阅读次数:
165
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
迭代 非递归版本
public class Solut...
分类:
其他好文 时间:
2014-12-01 22:35:09
阅读次数:
204
用一种算法来颠倒一个单链表的顺序,递归和非递归
#include
using namespace std;
struct Node
{
int data;
Node *next;
};
Node* createLinkList(int a[], int n)
{
if(a == NULL || n == 0)return NULL;
Node *L = NULL,*t = NUL...
分类:
其他好文 时间:
2014-12-01 16:00:08
阅读次数:
176
1 #include 2 #include 3 #define INIT_STACK_SIZE 100 4 #define STACKINCREMENT 10 5 6 //*****二叉树的二叉链表存...
分类:
编程语言 时间:
2014-11-30 22:57:43
阅读次数:
305
汉诺塔问题博大精深,我稍微搜集整理了一下,就得到如此多方法,还有好些方法一时不能理解,没有贴出来,请广大网友共同探讨,分享更多更好的方法。...
分类:
编程语言 时间:
2014-11-30 16:57:24
阅读次数:
332
问题:对一个二叉搜索树进行前序遍历,打印出每个结点的值,但是不能使用递归。
解题:
(1)递归可以用迭代来替代
(2)了解递归的前序遍历中发生了什么:①打印出根节点(或子树根节点)的值;②对左子树进行前序遍历;③对右子树进行前序遍历。
递归隐式地使用了一个数据结构栈来存放调用栈上的数据。实际上,递归调用用于隐式地在栈上存储右子树的地址,因此左子树遍历完后,可以继续遍历右...
分类:
其他好文 时间:
2014-11-30 11:30:09
阅读次数:
141
1 void levelOrder(Bitree* root){ 2 queue nodeQueue; 3 Node* pointer=root; 4 if(pointer){ 5 nodeQueue.push(pointer); 6 } 7 ...
分类:
其他好文 时间:
2014-11-26 22:28:44
阅读次数:
211
二叉树的几种递归和非递归式遍历:#include #include using namespace std;/* 后序遍历的非递归实现是三种遍历方式中最难的一种。因为在后序遍历中,要保证左孩子和右孩子都已被访问并且左孩子在右孩子前访问才能访问根结点,这就为流程的控制带来了难题。下面介绍两种思路...
分类:
其他好文 时间:
2014-11-25 22:43:55
阅读次数:
270
struct BinaryTreeNode{ int m_nValue; BinaryTreeNode *m_pLeft; BinaryTreeNode *m_pRight;};//递归实现二叉树的遍历。递归算法比较简洁易懂这一就不做解释void Preorder(BinaryTreeNode *p...
分类:
其他好文 时间:
2014-11-25 12:15:17
阅读次数:
151
二叉树的几种递归和非递归式遍历: 1 #include 2 #include 3 4 using namespace std; 5 6 /* 7 后序遍历的非递归实现是三种遍历方式中最难的一种。因为在后序遍历中,要保证左孩子和右孩子都已被访问并且左孩子在右孩子 8 前...
分类:
编程语言 时间:
2014-11-25 01:33:59
阅读次数:
207