算法与问题解决之道
文章平均质量分 81
YoungStunner
yeah
展开
-
最大公约数
求最大公约数是程序中经常需要使用的,大部分人都知道可以使用辗转相除法来求得,但是因为需要使用取模运算,开销较大,那么有没有更好的方法呢?答案是肯定的。一下是代码:#includeusing namespace std;/*1、int GreatestCommonDivisor(int x, int y){ return (!y) ? x : GreatestCommonDivisor(y原创 2015-12-30 17:49:53 · 353 阅读 · 0 评论 -
Bloom Filter概念和原理
Bloom Filter是一种空间效率很高的随机数据结构,它利用位数组很简洁地表示一个集合,并能判断一个元素是否属于这个集合。Bloom Filter的这种高效是有一定代价的:在判断一个元素是否属于某个集合时,有可能会把不属于这个集合的元素误认为属于这个集合(false positive)。因此,Bloom Filter不适合那些“零错误”的应用场合。而在能容忍低错误率的应用场合下,Bloom转载 2016-03-07 16:17:53 · 436 阅读 · 0 评论 -
对字符串数组进行排序,将变位词排在相邻位置
#include#include#include#include#include#includeusing namespace std;string sortChars(string s){ sort(s.begin(), s.end(), less()); return s;}void sort(vector& array){ hash_map> hash; lis原创 2016-01-26 17:44:41 · 658 阅读 · 0 评论 -
c++STL中的hash_map自定义类。
是的,hash_map是一个很方便的容器,有了STL确确实实给了C++developer很大方便,hash_map就是其中一种。他在数据少的时候,作用和基于RB-tree的map差不多,甚至不如,毕竟有hasher。但是在大量数据的时候,就很快捷了。我平时用hash_map都是用基本类型的,最多弄个string类,也是库里已经弄好了的。直接套模板就行。可是今天遇到一个问题就是当你需要把一个自定义类原创 2016-01-26 01:18:50 · 3374 阅读 · 0 评论 -
用n个宽d,高h,深d的箱子打出高度最高的箱子,下面的箱子的宽度,高度和深度必须大于上面的箱子
这个可写死我了,从晚上六点写到晚上11点,终于写对了,hash_map真的可以,先 放上代码,理论等会当独开一篇文章:#include#include#include#pragma warning(disable:4786)using namespace std;struct Box{public: int width; int depth; int height;原创 2016-01-25 23:52:25 · 1947 阅读 · 0 评论 -
打印n对括号的全部n对有效组合
#include#includeusing namespace std;int length(char* str){ if (str == 0) return 0; int count = 1; char* n = str; while (*n != '\0'){ count++; n++; } return count;}void addParen(vec原创 2016-01-24 16:18:06 · 474 阅读 · 0 评论 -
返回某集合的所有子集
#include#includeusing namespace std;vector> getSubsets(vector& set, int index){ vector> allsubsets; if ((int)set.size() == index){ vector s; allsubsets.push_back(s); } else{ allsubsets =原创 2016-01-24 00:24:18 · 528 阅读 · 0 评论 -
二位平面上,经过点数最多的线
#include#include#include#includeusing namespace std;class Point{public: double x; double y; Point() :x(0.0), y(0.0){} Point(double _x, double _y) :x(_x), y(_y){}};class Line{private: bo原创 2016-01-22 00:57:14 · 551 阅读 · 0 评论 -
集合栈-程序员面试金典
数据结构由多个栈组成,用链表连接,并且在前一个栈填满之时,新建一个栈,且push()方法和pop()方法和top()方法与普通栈的方法一样。且实现了一个popAt(int index)方法,指定子栈,并执行pop()操作,当一个非最后的栈pop时,用后续栈将其填满。为实现popAt()需要访问后一个栈的底部,所以不能使用Stack模板,而是使用deque。pop(),push(),top()都是通原创 2016-01-19 11:10:00 · 500 阅读 · 0 评论 -
移除未排序链表中的重复结点
#include#include using namespace std;struct ListNode{ int m_nValue; ListNode* m_pNext;};ListNode* CreateListNode(int value){ ListNode* pNode = new ListNode(); pNode->m_nValue = value; pN原创 2016-01-19 00:40:42 · 481 阅读 · 0 评论 -
用stack和面向对象的设计思想实现汉诺塔
#include#includeusing namespace std;class Tower{public: Tower(){} Tower(int i) :index(i){} ~Tower(){}; int getIndex(){ return index; } void add(int d){ if (!disks.empty() && disks.top() <=原创 2016-01-19 00:36:54 · 391 阅读 · 0 评论 -
求二进制中数1的个数
代码如下#includeusing namespace std;/*1、int Num1(int n){ int count = 0; while (n){ if (n % 2 == 1){ ++count; } n /= 2; } return count;}*//*2、int Num1(int n){ int count = 0; unsign原创 2015-12-31 22:48:50 · 424 阅读 · 0 评论 -
不用中间变量实现交换swap的问题
我们在实现交换的时候一般都是用一个中间变量来暂存:void swap(int& a, int& b) { int temp = a; //exchange a = b; b = temp;}类似于这样,然而也有不用中间变量的方法:void swap(int& a, int& b) { a += b; b = a - b; a -= b;}还有这样:void swap原创 2016-03-08 12:46:01 · 2039 阅读 · 1 评论