
算法与数据结构
文章平均质量分 80
王子力
星星之火,可以燎原
展开
-
hyperloglog算法思路简介
算法没详细看,有兴趣的可以读原始论文:http://algo.inria.fr/flajolet/Publications/FlFuGaMe07.pdf, 不过最顶层的思路get了首先这个算法的目的是求一堆元素当中不重复元素的数量,例如, a, b, a, c中不重复的元素只有a, b,c 3个,那么结果就是3.在面对巨大数据量时,这个问题的常规解法会变得非常消耗内存,即使用计算机存储的...原创 2019-10-04 17:13:32 · 280 阅读 · 0 评论 -
leetcode之图片(矩阵)旋转
题目:You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?思路:题目的关键在in-place,否则就太容易了,为了达到in-place只能原创 2014-12-24 22:22:25 · 858 阅读 · 0 评论 -
leetcode之LRU实现
题目:Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.get(key) - Get the value (will always be positive) of th原创 2014-12-22 23:00:39 · 902 阅读 · 0 评论 -
插入排序算法&二路归并排序算法java实现
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package sortalgos;import java.io.BufferedInputStream;import java.io.BufferedReader;import java.i原创 2012-04-17 19:56:34 · 1003 阅读 · 0 评论 -
由二叉树的各种遍历还原二叉树
http://hi.baidu.com/11magic/blog/item/b618ed13d4fdcc74cb80c481.html转载 2012-04-16 17:07:57 · 543 阅读 · 0 评论 -
leetcode之找光棍数
题目:Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without原创 2014-12-25 23:04:47 · 758 阅读 · 0 评论 -
leetcode之倒转一句话单词
题目:Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".click to show clarification.Clarification:What co原创 2014-12-25 21:56:56 · 870 阅读 · 0 评论 -
判断一个自然数是否为2的非负整数次方(power of 2)
public class IsPowerof2Demo { public static boolean isPoweroftwo(int x){ if(x<1){ return false; } int andVal = x&(x-1); int xorVal = x^(x-1);...原创 2018-12-26 22:01:31 · 347 阅读 · 0 评论 -
根号2的计算方法(Java实现)
出处:http://www.fengchang.cc/post/129读《西方哲学史》古希腊早期数学与天文学一章,看到一个有趣的求解根号2的方法,之前未曾见过。 思路如下:构造一个数对序列,初始值为(1,1),然后对该数对依照如下规则进行演化:下一个数对中的第一个数为前一个数对中两个数之和,记为A+B,第二个数为2*A+B(A代表前个数对的第一个数,B代表前个数对的第二个数)...原创 2018-12-12 22:33:44 · 11225 阅读 · 1 评论 -
leetcode 676之Implement Magic Dictionary
出处:http://www.fengchang.cc/post/123 leetcode地址:https://leetcode.com/problems/implement-magic-dictionary/ 题干我用中文复述一遍。 给定一个单词集合例如{"hello", "world"},和一个单词例如“hhllo",求是否能对给定单词仅做一位变换就能变成给定单词列表中的...原创 2018-10-28 22:11:47 · 210 阅读 · 0 评论 -
leetcode 672之Bulb Switcher II
出处:http://www.fengchang.cc/post/121 原题:https://leetcode.com/problems/bulb-switcher-ii/description/ 我用中文简述一下题干: 上面的四个方块是四个灯泡开关,下面的一系列椭圆是一组灯泡,初始状态全亮。 上面四个按钮的作用分别是:全:按一下则所有灯...原创 2018-10-22 08:53:25 · 312 阅读 · 1 评论 -
算法intuition之-给定一个整形数组,找出其中两个数之和为指定数
http://www.fengchang.cc/post/55 这题本没什么难度,然而看到某书上说: 这种证明是不对的,因为完全没有考虑错过的情况,是不严谨或者胡乱做假设前提的论证方式,故出一篇勘误。 intuition1: 在无序的情况下,必须两两比较才能确定最终结果,那么结果必然是O(n2)的时间复杂,这个不好。intuition2:在有序的情况下,可以用两个指...原创 2018-08-20 23:05:20 · 1190 阅读 · 0 评论 -
马尔科夫决策过程MDP
出处:http://www.fengchang.cc/post/11参考这里和 这里A Markov Decision Process (MDP) model contains:A set of possible world states S. A set of Models. A set of possible actions A. A real valued reward...原创 2018-07-17 13:38:45 · 1393 阅读 · 0 评论 -
家谱(特殊的层级人物关系)数据结构与自动排版算法的一种实现
github源代码(家谱海本地私有版):https://github.com/fengchangfight/familytreesea出处:http://www.fengchang.cc/post/24家谱的数据结构并不复杂,逻辑上可以抽象成一种图,节点为人物,边为人物关系,关系粗略分为两类,一类是跨层级的亲子关系(如父子,父女,母子,母女),另一类为同层级的夫妻关系(其实如果要加上更多的...原创 2018-05-09 11:45:43 · 12064 阅读 · 6 评论 -
leetcode之判断是否BST二分搜索树
题目:Validate Binary Search TreeGiven a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nod原创 2014-12-13 22:00:44 · 1311 阅读 · 0 评论 -
leetcode之判断两链表首次交汇节点
题目:Intersection of Two Linked ListsWrite a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A:原创 2014-12-13 22:09:31 · 572 阅读 · 0 评论 -
leetcode之有随机指针的链表深拷贝
题目:Copy List with Random PointerA linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of原创 2014-12-13 11:51:55 · 3326 阅读 · 0 评论 -
leetcode之数组中找两数和为指定值
题目:Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the ta原创 2014-12-25 22:53:13 · 1115 阅读 · 0 评论 -
不使用额外空间,将 A,B两链表的元素交叉归并
#includeusing namespace std;typedef struct Node{ int value; struct Node* next;} *List;void print(List list){ int i = 0; while(list != NULL) { cout value << " ";转载 2012-04-16 16:50:22 · 845 阅读 · 0 评论 -
大数乘法实现代码
#include #include using namespace std;void multiply(const char *a,const char *b);int main(){ //cout<<"hicjiajia"<<endl; string num1,num2; // 初始状态用string来存储大数 cout<<"Now, give m转载 2012-04-16 15:18:45 · 734 阅读 · 0 评论 -
四对括号可以有多少种匹配排列方式?比如两对括号可以有两种:()()和(())
#include#includeusing namespace std;int howMany1s(unsigned int num){ int count=0; for(int i=0;i<8;i++) { if((num&0x1)==1) count++; num=num>>1; } ret原创 2012-03-30 19:31:27 · 1236 阅读 · 0 评论 -
fibonacci数列的矩阵算法
#include #include ///////////////////////////////////////////////////////////////////////// A 2 by 2 matrix///////////////////////////////////////////////////////////////////////struct Matrix2By原创 2012-03-30 19:37:22 · 760 阅读 · 0 评论 -
二分搜索树排序binary search sorting的c++实现
#include #include #include using namespace std;class BST{ private: double value; BST * leftNode; BST * rightNode; public: BST(double v) { this->value=v; thi原创 2011-06-24 11:04:00 · 612 阅读 · 0 评论 -
算法题之两个数组和之差最小化
题目:有两个序列a,b,大小都为n,序列元素的值任意整数,无序;要求:通过交换a,b中的元素,使[序列a元素的和]与[序列b元素的和]之间的差最小。例如: var a=[100,99,98,1,2, 3];var b=[1, 2, 3, 4,5,40];源码:#include#includeusing namespace std;int myfabs(int a){原创 2012-03-30 19:35:37 · 1248 阅读 · 0 评论 -
逆序一段文本算法
#include #include #include #include struct stackNode{ stackNode(std::string s) { this->val=s; next=NULL; } std::string val; stackNode * next;};struct strin原创 2012-03-30 19:42:26 · 375 阅读 · 0 评论 -
快速排序和堆排序为什么快排更常用
简单总结:虽然平均时间复杂度都是O(n),但是并不意味着一样快,快排具有更好的本地连续性,而堆排序总是跳来跳去,所以更慢。但是如果考虑到最坏时间复杂度,则宁可选择堆排序靠谱一点。http://stackoverflow.com/questions/1853208/quicksort-superiority-over-heap-sort转载 2012-04-25 10:58:15 · 3201 阅读 · 0 评论 -
求二叉树中最远两个节点距离
http://blog.163.com/shi_shun/blog/static/2370784920109264337177/原创 2012-04-24 13:02:08 · 435 阅读 · 0 评论 -
合并两个字符串A、B,A的后几个字节同B的前几个字节
/*给出一个函数来合并两个字符串A和B。字符串A的后几个字节和字符串B的前几个字节重叠。//*/#include #include #include using namespace std;void copystr(char pachar[], char pbchar[], int sza, int szb, char* &result);int main(){转载 2012-04-06 12:18:06 · 1608 阅读 · 0 评论 -
判断单链表中是否存在环
思路:设置两个指针p1、p2,其实位置都为头指针,然后循环遍历,p1每次前进一步,p2每次前进两步,遇null而止,若在此之前,出现p1==p2,则说明存在环。原创 2012-04-05 22:16:43 · 397 阅读 · 0 评论 -
毒酒老鼠问题
题目:有1000桶酒,其中1桶有毒。而一旦吃了,毒性会在1周后发作。现在我们用小老鼠做实验,要在1周内找出那桶毒酒,问最少需要多少老鼠。解答:二进制解法,酒从0——999标号,不过不用十进制,而用二进制表示。由于表示1000个数需要十位二进制,故二进制统一用十位数表示,不满十位的前面充零,对每个酒桶标号做如下处理:若第i为为1,则给第i只老鼠喝酒,则一个礼拜后,死亡的老鼠标号就能还原创 2012-04-05 21:58:45 · 1826 阅读 · 0 评论 -
计算机笔试题:写一个函数,检查字符是否是整数,如果是,返回其整数值。(或者:怎样只用4行代码编写出一个从字符串到长整型的函数)
//#include "stdafx.h"#include #include #include #include using namespace std;long strtoint(char *str,int length);int main(int argc, char* argv[]){ int i=0; char str[100转载 2012-04-04 21:55:32 · 1665 阅读 · 0 评论 -
堆排序算法
#include #include #include #include void siftDown(int * str, int start, int end);void heapify(int * str,int count);void heapSort(int * str, int count);void printStr(int * str, int count);voi原创 2012-03-30 19:38:39 · 359 阅读 · 0 评论 -
C语言strcpy函数实现
char * mystrcpy2( char *strDest, const char *strSrc ){assert( (strDest != NULL) && (strSrc != NULL) );char *address = strDest;while( (*strDest++ = * strSrc++) != '\0');return address;}原创 2012-03-30 19:43:48 · 572 阅读 · 1 评论