
算法
tinglu
这个作者很懒,什么都没留下…
展开
-
编辑距离
距离:插入:1删除:1替换:2,(1也行,改下代码变2为1)#include using namespace std;const int N = 100;int d[N][N];char s1[N], s2[N];inline int three_min(int x, int y, int z){ int a = x<y?x:y; return a<原创 2013-09-05 13:31:38 · 463 阅读 · 0 评论 -
约瑟夫环
72个人,从第9个人开始报数,报到11的人退出,下一位继续从1开始报数,问最后一个人编号。#includeusing namespace std;const int NUM = 72;struct node{ int value; node* next;};void func(){ int i; node* head = new node;原创 2013-09-02 21:33:55 · 482 阅读 · 0 评论 -
输出递增数组中和为sum的两个数
算法本身不复杂,但是提供了一个跟有序数组相关的一个思路:记录首尾指针,由两边向中间缩进。#include using namespace std;bool findPairWithSum(int* a, int len, int sum, int& n1, int& n2){ if(a==NULL || lena[len-1]+a[len-2]) return false;原创 2013-10-05 23:18:08 · 1141 阅读 · 1 评论 -
数组中的逆序对
比归并排序只多了一句输出。#include using namespace std;void getInversePair(int* a, int* b, int low, int mid, int high){ int l=low, m=mid+1, k=low, j; while(l<=mid && m<=high) { if(a[l]>a[m]) { f原创 2013-10-05 14:18:53 · 591 阅读 · 0 评论 -
第一个只出现一次的字符
#include using namespace std;#define min(x, y) (x)<(y)?(x):(y) char getFirstSingleChar(char* s){ if(s==NULL || *s==0) return 0; int len = strlen(s); unsigned int h_table[256] = {0}; int i原创 2013-10-05 13:46:56 · 586 阅读 · 0 评论 -
链表翻转,每k个进行翻转
链表翻转。给出一个链表和一个数k,比如链表1→2→3→4→5→6,k=2,则翻转后2→1→4→3→6→5,若k=3,翻转后3→2→1→6→5→4,若k=4,翻转后4→3→2→1→5→6#include using namespace std;struct node{ int data; node* next;};node* init(int n){ n原创 2013-09-19 22:49:28 · 666 阅读 · 0 评论 -
堆排序
#include #include using namespace std;void heap_adjust(int *a, int i, int n){ int l, r, big; while(n>=2*i+1) { big = i; l = 2*i+1; if(a[l]>a[i]) b原创 2013-09-02 21:31:43 · 495 阅读 · 0 评论 -
剑指offer-33:把数组排成最小的数
#include using namespace std;const int N = 20;char s[N][N];char com1[2*N+1];char com2[2*N+1];int comp(const void *v1, const void *v2){ strcpy(com1, (char*)v1); strcat(com1, (char*)v2); str原创 2013-10-03 15:37:04 · 592 阅读 · 0 评论 -
利用二级指针删除节点
#include using namespace std;struct node{ int data; node* next;};void remove(node** head, int value){ if(head == NULL) return ; node** n = head; node* cur; while原创 2013-09-18 17:29:39 · 508 阅读 · 0 评论 -
句子反转,单词不反转
#include using namespace std;void reverse(char* s,int left, int right){ while(left<right) { swap(s[left], s[right]); left++; right--; }}void reverse_sentence原创 2013-09-15 20:10:35 · 786 阅读 · 0 评论 -
丑数,只包含因子2、3、5的数
#include using namespace std;#define min(x, y) (x)<(y)?(x):(y) int getUglyNumber(int index){ if(index<0) return 1; int* nums = new int[index]; nums[0] = 1; int next_id = 1; int* ugly2原创 2013-10-05 13:30:26 · 811 阅读 · 0 评论 -
百度三面:找出数组中所有这样的数,大于等于左边的所有数,小于等于右边的所有的数
#include using namespace std;void func(int *a, int len, int &b1, int &b2){ int a1, a2; b1 = a1 = 0; b2 = a2 = len-1; int l_min, r_max; l_min = a[a1]; r_max = a[a2]; while(a1<a2) { if(a[原创 2013-10-16 20:43:51 · 693 阅读 · 0 评论 -
最长回文:动态规划
#include using namespace std;void huiwen(char* a){ if(a==NULL) return ; int len = strlen(a); bool table[100][100] = {false}; int max, max_id; int i,j; for(i=0; i<len; i++) table[i][i] =原创 2013-09-28 23:37:26 · 569 阅读 · 0 评论 -
百度笔试:最长回文(中心扩展法)
#include using namespace std;void huiwen(char* a, int& max, int& max_id){ if(a==NULL || *a=='\0') { max = 0; max_id = 0; return ; } int len = strlen(a); int i, k1, k2; max = 1; max_id原创 2013-09-28 23:23:40 · 755 阅读 · 0 评论 -
组合
#include #include using namespace std; //函数功能 : 从一个字符串中选m个元素//函数参数 : pStr为字符串, m为选的元素个数, result为选中的//返回值 : 无void Combination_m(char *pStr, int m, vector &result){ if(pStr == NULL ||原创 2013-09-27 20:21:42 · 565 阅读 · 0 评论 -
将二叉搜索树转换成双向链表
#include #include using namespace std; struct node{ int data; node* lchild; node* rchild;}; node* buildBTree(int* a, int &i){ if(a==NULL || a[i]==-1)原创 2013-09-27 18:56:57 · 599 阅读 · 0 评论 -
打印1到最大的n位数
剑指offer用的是字符串,还要苦逼的字符串比较,看是否进位。直接用整型数组来保存。#include #include using namespace std; //一个int保存几位数const int int_num = 2;//每位数最大值,超过这个要进位(不包括最高位)const int int_max = 99; void func(){原创 2013-09-26 17:00:31 · 447 阅读 · 0 评论 -
已知先序,中序,求后序
#include using namespace std;void func(int* pre, int* mid, int pre_left, int pre_right, int mid_left, int mid_right){ if(pre_left == pre_right) { cout<<pre[pre_left原创 2013-09-18 16:18:10 · 658 阅读 · 0 评论 -
n后问题
关键问题:不在同行,同列,斜线上。即对任意两行,!(x[r1]==x[r] || abs(r1-r)==abs(x[r1]-x[r]))。用回溯法,通过此条件进行减枝。#include using namespace std;void print(int *x, int n){ int i, j; for(i=0; i<n; i++) { for(j=0; j<n; j原创 2013-10-06 12:57:06 · 669 阅读 · 0 评论 -
1~n,1出现的次数
主要思想,分别考虑每位(个位,十位,不是二进制的位)为1的情况,特别考虑最高位是1和不是1的情况。使用递归,如 f(253)=f(200)+f(50)+f(3)#include #include using namespace std;int f1(int n){ int i, j, k; int num_1=0; for(i=0; i<=n; i++)原创 2013-09-11 13:00:54 · 619 阅读 · 0 评论 -
判断平衡二叉树,创建二叉树,先序遍历二叉树
#include using namespace std;struct node{ int data; node* lchild; node* rchild;};bool is_banance_tree(node* r, int& height){ if(r == NULL) { height = 0; retu原创 2013-09-17 23:58:14 · 523 阅读 · 0 评论 -
二叉树中和为某一值的路径
#include #include using namespace std; struct node{ int data; node* lchild; node* rchild;};void find_path(node* r, int exceptedSum, vector &path, int& curSu原创 2013-09-27 13:33:31 · 501 阅读 · 0 评论 -
最大子数组和
#include using namespace std;void f(){ int a[] = {-6,-8, -5, -9}; int len = sizeof(a)/sizeof(a[0]); int sum = a[0] ; int d = a[0]; int i; for(i=1; i<len; i++) {原创 2013-09-10 21:35:12 · 577 阅读 · 0 评论 -
链表反转,递归,迭代
#include using namespace std;struct node{ int data; node* next;};node* reverse_link(node* head){ if(head==NULL || head->next==NULL) return head; node* pre; node原创 2013-09-11 01:39:53 · 684 阅读 · 0 评论 -
1~3000所有包含5,6的数(如56,526,1635)的和
#includeusing namespace std;const int contain_num[] = {5,6};const int num_len = sizeof(contain_num)/sizeof(contain_num[0]);const int MAX = 3000;bool b_has[10] = {false};bool if_contain(int n);原创 2013-09-02 21:38:02 · 594 阅读 · 0 评论 -
质因数分解
#includeusing namespace std;void func(int n){ int i; for(i=2; i<n; ) { if(n%i == 0) { cout<<i<<"*"; n /= i; } else i原创 2013-09-02 21:35:00 · 477 阅读 · 0 评论 -
链表逆序
递归与非递归实现#includeusing namespace std;struct node{ int value; node* next;};//非递归node* func(node* head){ if(head==NULL || head->next==NULL) return head; node* p1原创 2013-09-02 20:53:57 · 526 阅读 · 0 评论 -
找出只出现一次的数,其他数都出现了k次
#include using namespace std;int k=3;int find_single(int* a, int n){ int i, j; int single = 0; int temp[32]; for(i=0; i<32; i++) temp[i] = 0; for(i=0; i<n; i原创 2013-09-08 12:15:15 · 707 阅读 · 0 评论 -
最短路径 Dijkstra
#include #include using namespace std;const int N = 20;const int MAX_DIS = 10000;int dis[N]; //v0到i的最短路径长度int pre[N]; //节点i的前驱int dd[N][N]; //节点之间距离int sort_id[N]; //记录dis[n]排序后的下标,关键int n原创 2013-09-07 22:23:49 · 517 阅读 · 0 评论 -
字符串排序
后缀数组中,需要排序字符串:#include #include using namespace std;int comp(const void* p1, const void* p2){ return strcmp((char*)p1, (char*)p2);}int main(){ char s[5][6] = {"abc", "abcd", "bcde",原创 2013-09-07 13:05:38 · 555 阅读 · 0 评论 -
去重全排列
#include using namespace std;bool is_swap(char* a, int i, int j){ while(i<j) { if(a[i] == a[j]) return false; i++; } return true;}void print_a(char*原创 2013-09-03 13:30:28 · 505 阅读 · 0 评论 -
最长重复子串——后缀数组
#include #include using namespace std;#define N 100char s[N];char suf[N][N];int max_same = 0;int comp(const void *s1, const void* s2){ return strcmp((char*)s1, (char*)s2);}int ge原创 2013-09-10 20:29:42 · 536 阅读 · 0 评论 -
链表 快排
注意指向指针的指针的应用。#include #include using namespace std;struct node{ int data; node* next;};node* partion(node** head, node* end){ // coutdata<<" "; // if(end==NULL) //原创 2013-09-14 01:41:41 · 688 阅读 · 0 评论 -
拷贝构造函数,赋值运算符重载,友元输出重载
#include using namespace std;class T{public: T():buf(NULL){} T(char *s) { buf = new char[strlen(s) + 1]; strcpy(buf, s); } T(const T& t) { char* t_原创 2013-09-16 14:01:01 · 648 阅读 · 0 评论 -
大整数乘法
#include using namespace std;int main(){ char a[] = "453453236478236783462786"; char b[] = "98928342347896248326"; int lenA = strlen(a); int lenB = strlen(b); int lenR = lenA+lenB; int* r原创 2013-08-17 17:06:37 · 477 阅读 · 0 评论 -
KMP
#include using namespace std;void set_next(int* next, char *s){ int k, j; int len = strlen(s); next[0] = -1; for(k=1; k<len; k++) { j = k-1; while(1) {原创 2013-09-16 11:27:23 · 545 阅读 · 0 评论 -
百度笔试:求大于n的最小的不重复数,不重复数是指相邻两个数不相同
#include using namespace std;int next_norepeat(unsigned int n){ int pow, i, j; i=++n; pow=0; while(i) { pow++; i/=10; } int *a = new int[pow+1]; a[0] = 0; for(i=pow, j=n; i>0; i--, j原创 2013-09-28 22:27:29 · 1151 阅读 · 2 评论 -
字符串循环移位
#include using namespace std;void reverse_str(char* s, int l, int h){ while(l<h) { swap(s[l++], s[h--]); }}void left_rotate_reverse(char* s, int k, int n){ reverse_str(原创 2013-09-05 14:47:44 · 400 阅读 · 0 评论 -
C++ 继承,虚表内容
#include using namespace std; class D{ public: virtual void d(){cout<<"D::d"<<endl;} // int dd; };class ClassA : public virtual D{public:virtual ~ ClassA(){};virtual void FunctionA(){cout原创 2013-09-10 13:08:23 · 577 阅读 · 0 评论 -
最长递增子序列
#include using namespace std;#define N 1000int max_len = 0;int max_right_id = 0;char s[N];int c[N];int pre[N];void print(int id){ if(pre[id] == -1) { cout<<max_len<<": "<<原创 2013-09-10 21:14:08 · 556 阅读 · 0 评论