跟上次输出一个字符串的排列类似,http://zrj.me/archives/472,要求输出一个字符串的组合,思路也是递归
#include <stdio.h>
#include <string.h>
void combine(char str[], int str_index, char res_total, char result[], int res_index)
{
if (res_total == res_index)
{
for (int i=0; i<res_…… 阅读全文
Tag Archives: 数据结构
二叉树的遍历
参考 http://blog.csdn.net/acdnjjjdj… 写了二叉树的遍历,(基本上就是照抄的,囧),抄的时候感慨,stl 真是个好东西啊,另外,在三种次序遍历的实现上,递归的精巧发挥的淋漓尽致啊,还有就是非递归的遍历,设计真的好巧妙
#include <iostream>
#include <queue>
#include <stack>
using n…… 阅读全文
链表的反转
今天上午自己写了一个链表的反转
#include <stdio.h>
#include <stdlib.h>
typedef struct _node
{
int n;
_node *next;
} node;
int create_link(node **phead)
{
int x = 0;
printf("input nums, press Ctrl+Z(win) or Ctrl+D(linux) to endn");
if (scanf("%d", &x) <…… 阅读全文
算法复习之归并排序
今天接到电话面试的时候提到了一个归并排序,之前用的比较少,于是完了之后参照 http://blog.csdn.net/morewindo… 写了一个归并排序
#include <stdio.h>
#include <stdlib.h>
void merge(int n[], int first, int mid, int last, int temp[])
{
int i=first, j=mid+1, k=0;
while (i<=mid &…… 阅读全文
输出一个字符串的全排列
今天看到 http://blog.csdn.net/morewindo… 这里提出了一个问题
用C++写一个函数, 如 Foo(const char *str), 打印出 str 的全排列,
如 abc 的全排列: abc, acb, bca, dac, cab, cba
想了想(想了好久啊,好久没有碰数据结构和算法了),觉得可以 dfs,这样来搞:
#include <stdio.h>
#include <stdlib.h…… 阅读全文
算法复习之快速排序
快速排序这个自己想了一会,真的想不出来,还是查了资料,使用 C89 标准函数库有一个非常简单的写法,(维基上的不能跑,自己稍稍改了一下)
#include <stdio.h>
#include <stdlib.h>
static int cmp(int *a, int *b)
{
return *a-*b;
}
int main()
{
int arr[10]={5, 3, 7, 4, 1, 9, 8, 6, 2};
qsor…… 阅读全文
算法复习之冒泡排序与选择排序
基础丢的差不多了,是时候该重新捡起来,不然真的心虚啊,于是就从排序开始,上 poj 找基础的排序题居然没有,悲催,太低级了,最后在 hdu 上找到一个排序题,http://acm.hdu.edu.cn/showprob…
忘得差不多也有一个好处,就是可以自己从头再想,这次决定不看任何参考资料,就凭着模糊的印象,慢慢的把算法的思路先理…… 阅读全文