随笔分类 - 算法学习
摘要:2012/3/7 百田:1. 求100~99999的水仙花数2. 将一个排序好的数组乱序 http://www.cnblogs.com/wouldguan/archive/2013/03/07/2947780.html3. 计算最少能剩多少个单牌, 可以出两张、三张、四张,五张顺子。2012/3/6 溢信1. 最长对称字串, 如“google”是“goog” http://www.cnblogs.com/wouldguan/archive/2013/03/06/2946066.html2. 求二叉树的任意2个节点的公共根节点3. 求给定数组的最长等差数列4. 3个盆(4, 4, 1.5)把8k
阅读全文
摘要:引用: http://blog.csdn.net/zzqkillyou/article/details/7388690//Javaimport java.util.*;public class Main{ public static int[] shuffle(int[] arr) { int count = arr.length; int[] arr2 = new int[count]; int randCount = 0; //统计已抽取的个数 int position = 0; //随机到的索引 int k = 0; //目标...
阅读全文
摘要:引用:http://blog.csdn.net/hackbuteer1/article/details/6686263题目:输入一个字符串,输出该字符串中对称的子字符串的最大长度。比如输入字符串“google”,由于该字符串里最长的对称子字符串是“goog”,因此输出4。算法一:O(n^3)判断字串是否对称是从外到里, O(n)#include <stdio.h>#include <string.h>/* *判断起始指针,到结束指针的字符串是否对称 */int IsSymmetrical(char* pBegin, char* pEnd){ if(pBegin == N
阅读全文
摘要:引用:http://www.cppblog.com/guyuecanhui/articles/76443.html问题描述:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列,求最后一个出列人的编号。代码:#include <stdio.h>int josephus(int n, int m, int start){ int k = 1; for(int i=2; i<=n; i++) k = (k+m-1)%i + 1; .
阅读全文
摘要:Depth-first search:访问顺序: A, B, D, F, E, C, G.PseudocodeInput: A graph G and a vertex v of GOutput: A labeling of the edges in the connected component of v as discovery edges and back edges1 procedure DFS(G,v):2 label v as explored3 for all edges e in G.adjacentEdges(v) do4 if edg...
阅读全文
摘要:javapublic class Hello{ public static int binarySearch(int[] a, int key) { int lo=0, hi = a.length-1; while(lo <= hi) { int mid = lo + (hi - lo) / 2; if(key < a[mid]) hi = mid-1; else if(key > a[mid]) lo = mid+1; else return mid; } return -1; } public static vo...
阅读全文
摘要:出处:http://www.dewen.org/q/620#include <stdio.h>int judge(int num){ int min = 1; if(num < 0) { return 0; } else if(num == 0 || num == 1) { return 1; } else { int current = 2; while(1) { if(current * current < num) { min = current; current = min * 2; } ...
阅读全文
摘要:出处: http://www.cnblogs.com/pkuoliver/archive/2010/10/27/Convert-m-number-to-n-number.htmlC语言版:#include <stdio.h>void m2n(int m, char* mNum, int n, char* nNum){ int i=0; char c, *p = nNum; while(*mNum!='\0') i = i*m + *mNum++ - '0'; while(i) { *p++ = i%n + '0'; i /= n; }
阅读全文
摘要:参考地址:http://blog.sina.com.cn/s/blog_6d50a05801016092.html问题:一个循环有序数组(如:3,4,5,6,7,8,9,0,1,2),不知道其最小值的位置,要查找任一数值的位置。要求算法时间复杂度为log2(n)。问题分析:我们可以把循环有序数组分为左右两部分(以mid = (low+high)/ 2为界),由循环有序数组的特点知,左右两部分必有一部分是有序的,我们可以找出有序的这部分,然后看所查找元素是否在有序部分,若在,则直接对有序部分二分查找,若不在,对无序部分递归调用查找函数#include <iostream>#inclu
阅读全文
摘要:#ifndef ST_CLASS#define ST_CLASS#include <iostream>using namespace std;class ST{ public: ST(int maxN) { head=0;} int search(int v) { return searchR(head, v); } int insert(int x) { insertR(head, x); } void show() ...
阅读全文
摘要:#include <stdio.h>//not find will return -1//find will return the indexint searchR(int a[], int l, int r, int key){ if(l>r) return -1; int m=(l+r)/2; if(a[m] == key) return m; if(l==r) return -1; if(key < a[m]) return searchR(a, l, m-1, key); else return searchR(a, m+...
阅读全文
摘要:#include <stdio.h>void exch(int& a, int &b){ int tmp = a; a = b; b = tmp;}void fixdown(int a[], int k, int N){ while(2*k <= N) { int j=k*2; if(j<N && a[j]<a[j+1]) j++; if(!(a[k]<a[j])) break; exch(a[k], a[j]); k = j; }}void heapsort(int a[], int l...
阅读全文
摘要:#include <stdio.h>void exch(int& a, int &b){ int tmp = a; a = b; b = tmp;}void fixup(int a[], int k){ while(k>1 && a[k/2]<a[k]) { exch(a[k/2], a[k]); k/=2; }}void fixdown(int a[], int k, int N){ while(2*k <= N) { int j=k*2; if(j<N && a[j]<a[j+1]) j...
阅读全文
摘要:void exch(int& a, int &b){ int tmp = a; a = b; b = tmp;}void fixup(int a[], int k){ while(k>1 && a[k/2]<a[k]) { exch(a[k/2], a[k]); k/=2; }}void fixdown(int a[], int k, int N){ while(2*k <= N) { int j=k*2; if(k<N && a[j]<a[j+1]) j++; if(!(a[...
阅读全文
摘要:#include <stdio.h>#include <stack>using namespace std;void exch(int& a, int &b){ int tmp = a; a = b; b = tmp;}int partition(int a[], int l, int r){ int i=l-1; int j=r; int val = a[r]; while(1) { while(val > a[++i]); while(val < a[--j]) if(j == l) brea...
阅读全文
摘要:#include <stdio.h>#include <stack>using namespace std;void exch(int& a, int &b){ int tmp = a; a = b; b = tmp;}int partition(int a[], int l, int r){ int i=l-1; int j=r; int val = a[r]; while(1) { while(val > a[++i]); while(val < a[--j]) if(j == l) brea...
阅读全文
摘要:#include <stdio.h>void exch(int& a, int &b){ int tmp = a; a = b; b = tmp;}int partition(int a[], int l, int r){ int i=l-1; int j=r; int val = a[r]; while(1) { while(val > a[++i]); while(val < a[--j]) if(j == l) break; if(i >= j) break; e...
阅读全文
摘要:#include <stdio.h>#include <time.h>void shellsort(int a[], int l, int r){ int h; //step 1 4 13 40 121 ... for(h=1; h<=(r-l)/9; h=h*3+1); for(; h>0; h=h/3) { for(int i=l+h; i<=r; i++) { int j=i; int v = a[i]; while(j>=l+h && v<a[j-h]) ...
阅读全文
摘要:#include <stdio.h>int max(const int a[], int l, int r){ if(l == r) return a[l]; int mid = (l+r) / 2; int u = max(a, l, mid); int v = max(a, mid+1, r); return u>v? u : v;}int main(){ int a[5] = {4,2,6,4,8}; printf("%d", max(a, 0, 4)); return 0;}
阅读全文
摘要:/* * description: 图的欧拉路径搜索 * writeby: Nick * date: 2012-10-25 23:32 * */#include <iostream>#include <vector>#include <stack>using namespace std;struct Edge{ int v, w; Edge(int v=-1, int w=-1) : v(v), w(w) {}};class Graph{ private: int vcount, ecount; ...
阅读全文
浙公网安备 33010602011771号