01 深度优先搜索迷宫路径
02 广度优先搜索迷宫路径
03 大数加减法
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
class BigInt {
public:
BigInt(string str) : strDigit(str) {}
private:
string strDigit;
friend ostream& operator << (ostream &out, const BigInt &src);
friend BigInt operator + (const BigInt &lhs, const BigInt &rhs);
friend BigInt operator - (const BigInt &lhs, const BigInt &rhs);
};
ostream& operator << (ostream &out, const BigInt &src) {
out << src.strDigit;
return out;
}
BigInt operator+(const BigInt &lhs, const BigInt &rhs) {
string result;
bool flag = false;
int i = lhs.strDigit.size() - 1;
int j = rhs.strDigit.size() - 1;
for (; i >= 0 && j >= 0; --i, --j) {
int ret = lhs.strDigit[i] - '0' + rhs.strDigit[j] - '0';
if (flag) {
ret += 1;
flag = false;
}
if (ret >= 10) {
ret %= 10;
flag = true;
}
result.push_back(ret + '0');
}
if (i >= 0) {
while (i >= 0) {
int ret = lhs.strDigit[i] - '0';
if (flag) {
ret += 1;
flag = false;
}
if (ret >= 10) {
ret %= 10;
flag = true;
}
result.push_back(ret + '0');
i--;
}
}
if (j >= 0) {
while (j >= 0) {
int ret = rhs.strDigit[j] - '0';
if (flag) {
ret += 1;
flag = false;
}
if (ret >= 10) {
ret %= 10;
flag = true;
}
result.push_back(ret + '0');
j--;
}
}
reverse(result.begin(), result.end());
return result;
}
BigInt operator- (const BigInt &lhs, const BigInt &rhs) {
string result;
bool flag = false;
bool minor = false;
string maxStr = lhs.strDigit;
string minStr = rhs.strDigit;
if (maxStr.length() < minStr.length()) {
maxStr = rhs.strDigit;
minStr = lhs.strDigit;
minor = true;
} else if (maxStr.length() == minStr.length()) {
if (maxStr < minStr) {
maxStr = rhs.strDigit;
minStr = lhs.strDigit;
minor = true;
} else if (maxStr == minStr) {
return string("0");
}
}
int i = maxStr.length() - 1;
int j = minStr.length() - 1;
for (; i >= 0 && j >= 0; --i, --j) {
int ret = maxStr[i] - minStr[j];
if (flag) {
ret -= 1;
flag = false;
}
if (ret < 0) {
ret += 10;
flag = true;
}
result.push_back(ret + '0');
}
while (i >= 0) {
int ret = maxStr[i] - '0';
if (flag) {
ret -= 1;
flag = false;
}
if (ret < 0) {
ret += 10;
flag = true;
}
result.push_back(ret + '0');
i--;
}
if (minor) {
result.push_back('-');
}
reverse(result.begin(), result.end());
return result;
}
int main() {
BigInt int1("98798787987978798789797");
BigInt int2("6151651651654164646546546546546");
BigInt int3("564654656454654");
BigInt int4("123");
BigInt int5("456");
cout << int4 + int5 << endl;
cout << int5 - int4 << endl;
system("pause");
return 0;
}
04 海量数据查重
哈希表
#include<iostream>
#include<unordered_set>
#include<vector>
using namespace std;
int main() {
vector<int> vec;
for (int i = 0; i < 20000; i++) {
vec.push_back(rand());
}
unordered_set<int> myset;
for (int val : vec) {
if (myset.find(val) != myset.end()) {
cout << "第一个重复的数据为 " << val << endl;
break;
} else {
myset.insert(val);
}
}
system("pause");
return 0;
}
位图法
#include<iostream>
#include<unordered_set>
#include<vector>
#include<memory>
using namespace std;
int main() {
vector<int> vec{12, 78, 90, 78, 123, 8, 9, 90};
int max = vec[0];
for (int i = 1; i < vec.size(); i++) {
if (vec[i] > max) {
max = vec[i];
}
}
int* bitmap = new int[max / 32 + 1]();
unique_ptr<int> ptr(bitmap);
for (auto key : vec) {
int index = key / 32;
int offset = key % 32;
if (0 == (bitmap[index] & (1 << offset))) {
bitmap[index] |= (1 << offset);
} else {
cout << key << "重复出现过!!!" << endl;
}
}
system("pause");
return 0;
}
布隆过滤器
#include<iostream>
#include<unordered_set>
#include<vector>
#include<memory>
#include "stringhash.h"
using namespace std;
class BloomFilter {
public:
BloomFilter(int bitSize = 1471)
: bitSize_(bitSize) {
bitMap_.resize(bitSize_ / 32 + 1);
}
public:
void setBit(const char* str) {
int idx1 = BKDRHash(str) % bitSize_;
int idx2 = RSHash(str) % bitSize_;
int idx3 = APHash(str) % bitSize_;
int index = 0;
int offset = 0;
index = idx1 / 32;
offset = idx1 % 32;
bitMap_[index] |= (1 << offset);
index = idx2 / 32;
offset = idx2 % 32;
bitMap_[index] |= (1 << offset);
index = idx3 / 32;
offset = idx3 % 32;
bitMap_[index] |= (1 << offset);
}
bool getBit(const char* str) {
int idx1 = BKDRHash(str) % bitSize_;
int idx2 = RSHash(str) % bitSize_;
int idx3 = APHash(str) % bitSize_;
int index = 0;
int offset = 0;
index = idx1 / 32;
offset = idx1 % 32;
if (0 == (bitMap_[index] & (1 << offset))) return false;
index = idx2 / 32;
offset = idx2 % 32;
if (0 == (bitMap_[index] & (1 << offset))) return false;
index = idx3 / 32;
offset = idx3 % 32;
if (0 == (bitMap_[index] & (1 << offset))) return false;
return true;
}
private:
int bitSize_;
vector<int> bitMap_;
};
class BlackList {
public:
void add(string url) {
blockList_.setBit(url.c_str());
}
bool query(string url) {
return blockList_.getBit(url.c_str());
}
private:
BloomFilter blockList_;
};
int main() {
BlackList list;
list.add("www.baidu.com");
list.add("www.360buy.com");
list.add("www.tmall.com");
string url = "www.ali.com";
cout << list.query(url) << endl;
system("pause");
return 0;
}
05 海量数据求topK
大小根堆
#include<iostream>
#include<vector>
#include<time.h>
#include<queue>
using namespace std;
int main() {
vector<int> vec;
srand(time(NULL));
for (int i = 0; i < 10; i++) {
vec.push_back(rand() % 10);
}
priority_queue<int> maxheap;
int k = 5;
for (int i = 0; i < k; i++) {
maxheap.push(vec[i]);
}
for (int i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
cout << endl;
for (int i = k; i < vec.size(); i++) {
if (maxheap.top() > vec[i]) {
maxheap.pop();
maxheap.push(vec[i]);
}
}
while (!maxheap.empty()) {
cout << maxheap.top() << " ";
maxheap.pop();
}
cout << endl;
system("pause");
return 0;
}
快排分割
#include<iostream>
#include<vector>
#include<time.h>
#include<queue>
#include<functional>
#include<unordered_map>
using namespace std;
void SelectTopK(vector<int>& vec, int begin, int end, int k) {
int val = vec[begin];
int i = begin;
int j = end;
while (i < j) {
while (i < j && vec[j] > val) j--;
if (i < j) {
vec[i] = vec[j];
i++;
}
while (i < j && vec[i] < val) i++;
if (i < j) {
vec[j] = vec[i];
j--;
}
}
vec[i] = val;
if (i == k - 1) {
return;
} else if (i > k - 1) {
SelectTopK(vec, begin, i - 1, k);
} else if (i < k - 1) {
SelectTopK(vec, i + 1, end, k);
}
}
int main() {
vector<int> vec = {64, 45, 52, 80, 66, 68, 0, 2, 18, 75};
int size = vec.size();
int k = 3;
SelectTopK(vec, 0, size - 1, k);
for (int i = 0; i < k; i++) {
cout << vec[i] << " ";
}
cout << endl;
system("pause");
return 0;
}