Algorithm and Data Struct
cc509a
这个作者很懒,什么都没留下…
展开
-
bubble sort use c++
int bsort (int *a ,int first,int last){for (int i = first; i < last; i++){for (int j = i+1; j< last; j++){if (a[i] > a[j]){int temp = a[i];a[i] = a[j];a[j] = temp;}}}return 0;}int mai原创 2014-11-19 16:53:05 · 409 阅读 · 0 评论 -
lintcode Binary Search
For a given sorted array (ascending order) and a target number, find the first index of this number in O(log n) time complexity.If the target number does not exist in the array, return -1.原创 2015-08-03 18:09:35 · 653 阅读 · 0 评论 -
LintCode 二叉树的遍历 (非递归)
前序:class Solution {public: /** * @param root: The root of binary tree. * @return: Preorder in vector which contains node values. */ vector preorderTraversal(TreeNode *root) {原创 2015-07-01 14:48:55 · 629 阅读 · 1 评论 -
MySQL数据库中索引原理与数据结构
MySQL中的索引分为3种:1,主键索引:即用主键当唯一索引2,常规索引:实现方式为B树和哈希表3,全文索引:实现原理类似倒排索引,常用来查询字段中包含关键字下面复习下B-TREE和hash-table1 B-treeB树是一种多路查找平衡多叉树,具有以下属性:1,如果根节点不是叶节点原创 2015-04-06 21:18:20 · 738 阅读 · 0 评论 -
判断一个单链表是否有环
用快慢指针法,慢指针一次走一步,快指针一次走两步,如果有环必定相遇这里给出C++和Python 版Python:"""Definition of ListNodeclass ListNode(object): def __init__(self, val, next=None): self.val = val self.next = n原创 2015-04-19 14:01:25 · 2399 阅读 · 0 评论 -
A*算法
适应性f:fitness 目标g:goal 启发式h:heuristicg为从起始节点到该节点的的代价,h为从该节点到目标节点的估计代价f为g和h的和,f代表队这条对这条路线的猜测和估计,越小越好另外A*保持着两个表:open和closed表,open代表未考察的节点,closed代表以考查的节点,伪代码:1 另P=起始节点2把 f,g和h的赋给p3 将p原创 2015-04-12 12:16:00 · 582 阅读 · 0 评论 -
倒排索引原理
这里介绍一个基础的搜索引擎技术假如有两段文字1,books and friends should be few but good2,A good book is a good friend假如我们忽略掉大小写和复数,可以整理出一张单词表,显示哪个单词再哪段文字,英文单词文章编号a2and1be1bo原创 2015-03-29 21:16:55 · 895 阅读 · 0 评论 -
Dynamic Programming longest common subsequence(LCS) 动态规划之最长公共子序列
问题描述在生物学应用中,经常要比较两个DNA的相似度,一个DNA可看做是一个串。一种衡量方法是,我们说这两个DNA相似可以看做S1是S2的子串(字符串匹配问题)。第二种方法是找出第三个串S3,S3中的每一个元素都出现在S1和S2中,这就是一个求最长公共子序列的应用。例如:S1 =ABCD ; S2 = BECD; 那么LCS S3= BCD;解决一群LCS的问题属于NP-h原创 2014-12-06 14:11:27 · 975 阅读 · 0 评论 -
Dynamic Programming 0-1 knapsacks 动态规划之01背包问题
动态规划解题步骤:1:描述最优解的结构2:递归定义最优解的值3:按自底向上的方式计算机最优解的值4:由计算机出的结果构造出一个最优解最优子结构:1:问题的一个解可以是一个选择。2:假设对一个给定的问题,已知的是一个可以导致最优解的选择。3:在已知这个解后,要确定哪些问题是会随之发生,以及如何最好的描述原创 2014-12-05 23:19:23 · 653 阅读 · 0 评论 -
分治法找出数组中第二大数
#include using namespace std;class Res{public:int max;int secmax;};Res maxnum(int *a,int first,int last){int mid;Res res;if (last-first==1){if (a[first] > a[last]){res.max = a[first原创 2014-11-19 16:50:40 · 2766 阅读 · 0 评论 -
快速排序
int swap(int &a,int &b){int temp;temp = a;a = b;b = temp;return 0;}int RandomInRange(int start, int end){if(end > start){srand(time(NULL));return start + rand()%(end-start)原创 2014-11-19 16:38:24 · 460 阅读 · 0 评论 -
插入排序用递归实现
int insertsort (int *a ,int first,int last){for (int i = first +1 ; i < last; i++){int key = a[i];int j = i-1;while (j >= first && a[j] > key){a[j+1] = a[j];j--;}a[j+1] = key;}return 0;}原创 2014-11-19 16:58:48 · 824 阅读 · 0 评论 -
merges sort use c++
归并排序int merges (int *a ,int first,int mid,int last){int n1 = mid-first+1;int n2 = last-mid;int left[n1];int right[n2];for( int i = 0; i < n1;i++){left[i] = a[first+i];}for (int j =0; j < n原创 2014-11-19 16:55:08 · 457 阅读 · 0 评论 -
string 简单实现
namespace ss{ class string { friend ostream& operator <<(ostream&, const string&); char *_str; public: string():_str(new char[1]){ _str[0] = '\0'; }原创 2015-07-24 16:03:57 · 586 阅读 · 0 评论