方式一:将字符串反向输出来,不改变内存(递归实现)
void reverse_string(char *str)
{
/*遇到'\0'什么也不做,函数结束*/
if(*str == '\0')
;
else
{
/*输出下一个*/
reverse_string(str + 1);
cout<<*str;
}
}方式二:改变内存(交换法)
/*非递归实现:操作内存*/...
分类:
其他好文 时间:
2015-05-10 18:58:07
阅读次数:
177
递归实现是最常想到的方法,代码如下:
//递归方式
long Fibonacci(unsigned n)
{
if (n==0)
{
return 0;
}
else if (n==1)
{
return 1;
}
else
{
return Fibonacci(n-1)+Fibonacci(n-2);
}
}
显然递归并不是最好的方法,当n较大时效率将非常低下。
...
分类:
其他好文 时间:
2015-05-10 12:59:56
阅读次数:
159
【题目】
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
【分析】
不要忘记先判断两个链表是否有空链表。其余的使用递归和非递归...
分类:
其他好文 时间:
2015-05-08 14:58:11
阅读次数:
97
#include
using namespace std;
template
T power(T x,Integer n)
{
if(n == 0) return 1;
if(n == 1) return x;
while((n&1) == 0)
{
x = x*x;
n >>= 1;
}
T result = x...
分类:
其他好文 时间:
2015-05-06 13:18:36
阅读次数:
125
1. 先序遍历public void preorder(TreeNode root) { if(root == null) return; Stack stack = new Stack(); while(true) { if(root...
分类:
其他好文 时间:
2015-05-05 18:51:09
阅读次数:
108
problem:
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
2
/
3
return [3,2,1].
Note: R...
分类:
其他好文 时间:
2015-05-05 12:45:27
阅读次数:
123
题目
思路
翻转单向链表,这里题目要求用递归和非递归实现,具体思路见代码。代码
a)非递归struct ListNode* reverseList(struct ListNode* head) {
struct ListNode * Before = NULL;
struct ListNode * OriPresent = head;
while (OriPresent...
分类:
其他好文 时间:
2015-05-05 10:40:13
阅读次数:
103
problem:
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
2
/
3
return [1,2,3].
Note: Re...
分类:
其他好文 时间:
2015-05-05 10:36:19
阅读次数:
126
链表反转是数据结构的基本功,主要有递归和非递归两种实现方式。我们一一介绍如下:
1. 非递归实现
主要包括如下4步:
1)如果head为空,或者只有head这一个节点,return head即可;
2)从头到尾遍历链表,把reversedHead赋值给当前节点的next;
3)当前节点赋值给reversedHead;
4)遍历结束,ret...
分类:
其他好文 时间:
2015-05-05 00:05:46
阅读次数:
107
1.问题描述 将马随机放在国际象棋的Board[0~7][0~7]的某个方格中,马按走棋规则进行移动,走遍棋盘上全部64个方格。编制非递归程序,求出马的行走路线,并按求出的行走路线,将数字1,2,…,64依次填入一个8×8的方阵,输出之。2.matlab代码clear allclcchessboa....
分类:
编程语言 时间:
2015-05-04 21:45:18
阅读次数:
199