刷题常用api汇总
1.vector
1. 初始化定义 头文件:
#include<vector>
2. 定义
vector<int> a;//初始化定义
vector<int> a(105);//固定vector的大小
3. 插入
a.push_back();//括号里面填写插入的值
4. 删除
std::vector<int> data(100, 99);// Contains 100 elements initialized to 99
data.clear(); // Remove all elements
std::vector<int> data(100, 99); // Contains 100 elements initialized to 99
data.pop_back(); // Remove the last element
std::swap(std::begin(data)+1,std::end(data)-1);
// Interchange 2nd element with the last data.pop_back(); // Remove the last element
5. 排序
- 包含头文件 #include,然后using namespace std;
- 假如你定义的vector变量为vector num,则如下:
** sort(num.begin(), num.end(), sortFun);
例如用最少的箭引爆气球
class Solution {
public:
static bool cmp(vector<int> p1, vector<int> p2) {
return p1[1] < p2[1];
}
int findMinArrowShots(vector<vector<int>>& points) {
int sz = points.size();
if (sz < 1) return 0;
sort(points.begin(), points.end(), cmp);
int tail = points[0][1];
int cnt = 1;
for (int i = 1; i < sz; i++) {
if (points[i][0] > tail) {
tail = points[i][1];
cnt++;
}
}
return cnt;
}
};
6. 反转
reverse(d.begin(),d.end()); ///不是d.reverse()
7. 寻找特定的字符
it=find(temp.begin(),temp.end(),3);///返回的是地址
8. 累加
为了对vector中的所有元素求和,我们利用accmulate这个函数。在调用这个函数之前我们必须包含头文件#include 。切记,一定包含,不然就出错。
int arr[]={10,20,30,40,50};
vector<int> va(&arr[0],&arr[5]);
int suum=accumulate(va.begin(),va.end(),0);
2.string
1. 字符串插入
string temp;
char a = 's';
temp = temp + a;
2. int->string
int a=15;
string res=to_string(a);
3. string ->int
int a=s[i]-'0';//s表示为字符串string类型
4. replace,替换单个字符
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int main()
{
string str="123/421657/abcd///456789";
cout << str << endl;
replace(str.begin(),str.end(),'/',' ');
cout << str << endl;
return 0;
}
3.map
1. map的基本操作函数:
C++ maps是一种关联式容器,包含“关键字/值”对
begin() 返回指向map头部的迭代器
clear() 删除所有元素
count() 返回指定元素出现的次数
empty() 如果map为空则返回true
end() 返回指向map末尾的迭代器
equal_range() 返回特殊条目的迭代器对
erase() 删除一个元素
find() 查找一个元素
get_allocator() 返回map的配置器
insert() 插入元素
key_comp() 返回比较元素key的函数
lower_bound() 返回键值>=给定元素的第一个位置
max_size() 返回可以容纳的最大元素个数
rbegin() 返回一个指向map尾部的逆向迭代器
rend() 返回一个指向map头部的逆向迭代器
size() 返回map中元素的个数
swap() 交换两个map
upper_bound() 返回键值>给定元素的第一个位置
value_comp() 返回比较元素value的函数
4.赋最大值最小值的初值
#include <iostream>
#include <limits>
using namespace std;
int main()
{
int intMax = numeric_limits<int>::max();
int intMin = numeric_limits<int>::min();
float floatMax = numeric_limits<float>::max();
float floatMin = numeric_limits<float>::min();
cout << intMax << " " << intMin << endl;
cout << floatMax << " " << floatMin << endl;
return 0;
}
输出结果
5.优先队列
example:
leetcode 23 题
合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
if ( lists.size() == 0) return NULL;
auto head = ListNode(0);
auto comp = [](ListNode* const &a, ListNode* const &b){return a->val > b->val;};
priority_queue<ListNode*, vector<ListNode*>, decltype(comp)> q(comp);
for(auto &h:lists) if(h!=nullptr) q.push(h);//将vector里面的数,全部放进优先队列中
auto p = &head;
while(!q.empty()){
p->next=q.top();
p=p->next;
q.pop();
if(p->next != nullptr) q.push(p->next);//pop之后又push进了那个数
}
return head.next;
}
};