https://game.hullqin.cn/uno/1111 C++栈 基础代码
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int st[1001];
int TOP=0;
void push(int x){ //入栈
++TOP;
st[TOP]=x;
}
void pop(){ //出栈
--TOP;
}
bool empty(){ //判断栈是否为空(空=true 不是空=false)
return TOP==0;
}
int top(){ //访问栈顶
return st[TOP];
}
int main(){ return 0; }
#include<iostream>
#include<bits/stdc++.h>
#include<stack>
using namespace std;
int main(){
stack<int>s;
// 栈名.top(); 访问栈顶元素
// 栈名.push(进入元素); 入栈
// 栈名.pop(); 出栈
// 栈名.empty(); 判断栈是否为空(空=true 不是空=false)
// 栈名.size(); 元素的个数
return 0;
}C++队列 基础代码
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int q[1001];
int head=0;
int tail=0;
void push(int t){ //入队
++tail;
q[tail]=t;
}
void pop(){ //出队
++head;
}
int front(){ //访问队首
return q[head+1];
}
int back(){ //访问队尾
return q[tail];
}
int size(){ //队列大小(元素的个数)
return tail-head;
}
int empty(){ //判断队列是否为空(空=true 不是空=false)
return tail-head==0;
}
int main(){
return 0;
}#include<iostream>
#include<bits/stdc++.h>
#include<stack>
#include<queue>
using namespace std;
int main(){
queue<int> s,s1;
// 队名.push(加入元素); 入队
// 队名.pop(); 出队
// 队名.empty(); 判断队列是否为空(空=true 不是空=false)
// 队名.size(); 元素的个数
// 队名.front(); 访问队首
// 队名.back(); 访问队尾
return 0;
}以C++运行:C字符数组 类型转换
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){
char b[10]="3.14";
double a=atof(b); //字符串数组char转double
//cout<<a+0.1<<endl;
char b1[10]="114514";
int c=atoi(b1); //字符串数组char转int
//cout<<c-4<<endl;
char b2[20]="12345678912345";
long long d=atoll(b2); //字符串数组char转long long
//cout<<d+1<<endl;
char b3[20]="114514";
char *p;
int e=strtoll(b3, &p, 10);
//cout<<e;
return 0;
}以C++运行:C字符数组 基础操作
#include<iostream>
#include<bits/stdc++.h>
#include<cstring>
using namespace std;
int main(){
char a[10]="abc";
char b[10]="123";
strcpy(b, a); //复制
//cout<<b<<endl;
strcat(b, a); //拼接
//cout<<b<<endl;
//cout<<strcmp(a, b); //比较(a==b返回0 a>b返回1 a<b返回-1)
strlen(a); //获取长度
return 0
}C++字符数组 基础操作
#include<iostream>
#include<cstring>
using namespace std;
int main(){
string s;
cin>>s; //不能获取空格
getline(cin, s); //能获取带空格的字符串
s.length(); //获取长度
cout < s.substr(3, 4); //截取子串 参数1:字串起始位置,参数2:字串的长度
cout << s.find("12"); //查找字串,输出字串的起始位置
s.clear(); // 清空字符串
return 0;
}大小顶堆:
priority_queue<int> qmax; //相当于大顶堆
priority_queue<int,vector<int>,greater<int>> qmin; //相当于小顶堆 if(qmin.empty()||a>=qmin.top())
{
qmin.push(a);
}
else
{
qmax.push(a);
}
//2.如果小顶堆是空的,或者说当前元素大于小顶堆的堆顶元素,那么就将当前元素存入小顶堆,否则就存入大顶堆
3.大、小根堆的维护,当我们小根堆的元素,不满k个,就将大根堆的堆顶元素弹入小根堆,如果我们当前小根堆的元素超过了k个,就将小根堆的堆顶元素摊入大根堆
```cpp
while(qmin.size()>k)
{
qmax.push(qmin.top());
qmin.pop();
}
while(qmin.size()<k&&!qmax.empty())
{
qmin.push(qmax.top());
qmax.pop();
}4.查询第k大的元素,访问小根堆的堆顶元素
cout<<qmin.top()<<"\n";```cpp
class Solution {
public:
// 小顶堆
class mycomparison {
public:
bool operator()(const pair<int, int>& lhs, const pair<int, int>& rhs) {
return lhs.second > rhs.second;
}
};
vector<int> topKFrequent(vector<int>& nums, int k) {
// 要统计元素出现频率
unordered_map<int, int> map; // map<nums[i],对应出现的次数>
for (int i = 0; i < nums.size(); i++) {
map[nums[i]]++;
}
// 对频率排序
// 定义一个小顶堆,大小为k
priority_queue<pair<int, int>, vector<pair<int, int>>, mycomparison> pri_que;
// 用固定大小为k的小顶堆,扫面所有频率的数值
for (unordered_map<int, int>::iterator it = map.begin(); it != map.end(); it++) {
pri_que.push(*it);
if (pri_que.size() > k) { // 如果堆的大小大于了K,则队列弹出,保证堆的大小一直为k
pri_que.pop();
}
}
// 找出前K个高频元素,因为小顶堆先弹出的是最小的,所以倒序来输出到数组
vector<int> result(k);
for (int i = k - 1; i >= 0; i--) {
result[i] = pri_que.top().first;
pri_que.pop();
}
return result;
}
};其实我是摸鱼人 喵喵喵 图形编辑器https://csacademy.com/app/graph_editor/ https://www.crazygames.com/game/ev-io https://www.crazygames.com/game/kirka-io https://www.crazygames.com/game/air-wars-2 https://deepmess.com/namerena/ https://www.mazegenerator.net/ https://dimit.me/Fe26/index_zh_CN.html