1.unordered_set 无序集合
在leetcode575 分糖果中可以使用,加快解题速度
class Solution {
public:
int distributeCandies(vector<int>& candyType) {
//哈希数组
unordered_set<int> sums;
for(auto i:candyType)
{
sums.insert(i);
}
return min(sums.size(),candyType.size()/2);
}
};
2.sort排序
sort(envelopes.begin(),envelopes.end(),[](const auto& a,const auto& b)
{
return a[0]<b[0] || (a[0]==b[0] &&a[1]>b[1]);
});//此时按照a升序,b降序来进行排序
3.单调栈
stack<int> s;//单调栈保存数组下标
int n=nums.size();
vector<int> ans(n,-1);
for(int i=0;i<2*n-1;i++)
{
while(!s.empty() && nums[i%n]>nums[s.top()])
{
ans[s.top()]=nums[i%n];
s.pop();
}
s.push(i%n);
}
return ans;
4.判断回文子串
f.assign(n,vector<int>(n,true));
for(int i=n-1;i>=0;i--){
for(int j=i+1;j<n;j++)
{
f[i][j] = (s[i]==s[j]) && (f[i+1][j-1]);
}
}//for循环来判断是否是回文子串
5.用bitset来判断32位数中的1的个数
class Solution {
public:
int hammingWeight(uint32_t n) {
bitset<32> b(n);
int ans=0;
for(int i=0;i<32;i++)
{
if(b[i])
ans++;
}
return ans;
}
};
6.用upper_bound以及binary_search来实现二分查找
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
auto row = upper_bound (matrix.begin(),matrix.end(),target , [] (const int b,const vector<int> & a)
{
return b<a[0];
}
);
if(row == matrix.begin())
return false;
row--;
return binary_search(row->begin(),row->end(),target);
}
};
7.KMP算法
class Solution {
public:
int strStr(string s, string p) {
int n = s.size(), m = p.size();
if(m == 0) return 0;
//设置哨兵
s.insert(s.begin(),' ');
p.insert(p.begin(),' ');
vector<int> next(m + 1); //设计一个next的数组操作
//预处理next数组
for(int i = 2, j = 0; i <= m; i++){
while(j and p[i] != p[j + 1]) j = next[j];
if(p[i] == p[j + 1]) j++;
next[i] = j;
}
//匹配过程
for(int i = 1, j = 0; i <= n; i++){
while(j and s[i] != p[j + 1]) j = next[j];
if(s[i] == p[j + 1]) j++;
if(j == m) return i - m;
}
return -1;
}
};
8.数字转换为字符串
string src = to_string(num);
while(num>0)
{
int temp = num % 10;
nums.push_back(temp);
num=num/10;
}
reverse(nums.begin(),nums.end());
9.排序
sort(g.begin(),g.end(),greater<int>());
sort(g.begin(),g.end(),less<int>());
10.字典序的写法(677)
struct treeNode{
int val;
treeNode *next[26];
treeNode(){
this->val=0;
for(int i=0;i<26;i++)
{
this->next[i]=nullptr;
}
}
};
class MapSum {
public:
MapSum() {
root = new treeNode();
}
void insert(string key, int val) {
int delay = val;
if(mp.count(key))
delay-=mp[key];
mp[key]=val;
treeNode *temp =root;
for(int i=0;i<key.size();i++)
{
int v = key[i]-'a';
if(temp->next[v]==nullptr){
temp->next[v]=new treeNode;
}
temp=temp->next[v];
temp->val +=delay;
}
}
int sum(string prefix) {
treeNode *temp =root;
int ans = 0;
for(int i=0;i<prefix.size();i++)
{
int v = prefix[i]-'a';
if(temp->next[v]==nullptr){
return 0;
}
temp=temp->next[v];
}
return temp->val;
}
private:
unordered_map<string,int> mp;
treeNode* root;
};
/**
* Your MapSum object will be instantiated and called as such:
* MapSum* obj = new MapSum();
* obj->insert(key,val);
* int param_2 = obj->sum(prefix);
*/
11.记忆化搜索
class Solution {
public:
unordered_map<int,int> map;
int integerReplacement(int n) {
if(n==1) return 0;
else if(map.count(n)) return map[n];
else if(n%2==0) return map[n] = 1 + integerReplacement(n/2);
else
{
return map[n] = 2 + min(integerReplacement(n/2),integerReplacement(n/2+1));
} //记忆化搜索,把记忆过的东西给存起来了
}
};
12.multiset
multiset它类似set,但是可以加入重复的元素,并且可以调用
multiset.begin()找到数组的最小值,
以及调用multiset.rbegin()找到数组的最大值。
class StockPrice {
public:
StockPrice() {
this->maxTimestamp = 0;
}
void update(int timestamp, int price) {
maxTimestamp = max(maxTimestamp, timestamp);
int prevPrice = timePriceMap.count(timestamp) ? timePriceMap[timestamp] : 0;
timePriceMap[timestamp] = price;
if (prevPrice > 0) {
auto it = prices.find(prevPrice);
if (it != prices.end()) {
prices.erase(it);
}
}
prices.emplace(price);
}
int current() {
return timePriceMap[maxTimestamp];
}
int maximum() {
return *prices.rbegin();
}
int minimum() {
return *prices.begin();
}
private:
int maxTimestamp;
unordered_map<int, int> timePriceMap;
multiset<int> prices;
};
13.stringstream来加快字符串的分割
class Solution {
public:
vector<string> uncommonFromSentences(string s1, string s2) {
unordered_map<string, int> freq;
auto insert = [&](const string& s) {
stringstream ss(s);
string word;
while (ss >> word) {
++freq[move(word)];
}
};
insert(s1);
insert(s2);
vector<string> ans;
for (const auto& [word, occ]: freq) {
if (occ == 1) {
ans.push_back(word);
}
}
return ans;
}
};
14.并查集模板
class UnionFind{
public:
vector<int> fa;
UnionFind(int n ){
fa.resize(n);
iota(fa.begin(),fa.end(),0);
}
int find(int x){
return x==fa[x] ? x:(fa[x]=find(fa[x]));
}
void Union(int x,int y){
fa[find(x)]=find(y);
}
};