leetcode笔记
- string 和 int 相互转换:
int 转 string:string str = to_string(number);
string 转 int:int number = stoi(str.c_str());
使用时要注意stoi函数中参数str不能为空字符串,字符串也不能过大,需要能转换为 int 类型 - max 函数,定义与
中,可接受 两个参数 或 一个 initializer_list 对象:
cout << max(1,2) << endl;
initializer_list<int> ini = {1,2,4,5,-1};
cout << max(ini) << endl;
-
c++ 各类型最大最小值宏定义:
头文件:#include <climits>
INT_MAX,INT_MIN,UINT_MAX,LONG_MAX,LONG_MIN,ULONG_MAX,LLONG_MAX,LLONG_MIN,ULLONG_MAX
c++各类型最大最小值宏定义 -
使用 string::find(),返回找到的第一个下标(size_type类型)。如果没找到,返回 string::npos (leetcode 1143.)
需要注意的是,string::find()返回的是下标,因此如果想用到返回的位置的话,应如下:
string s = "abc def";
auto x = s.find(" ");
s = string(s.begin() + x + 1, s.end());
cout << s << endl;
输出:
def
或者使用substr函数:
std::string str="abc def";
size_t pos = str.find(" ");
std::string str1 = str.substr(0, pos);
std::string str2 = str.substr(pos + 1);
输出:
# str1
abc
# str2
def
- pair 结构体在
#include <utility>中 #include<bits/stdc++.h>包含所有的头文件- 一些关于字符串操作的函数:
strchr,strrchr,strpbrk,strspn - dp 中背包问题首先考虑的递归公式推法是:如果容量为 j 的背包装得下/装不下 nums[i]
tuple使用:
// 创建一个包含一个char字符,两个int变量的tuple
tuple<char, int, int> t1 = make_tuple('a', 1, 1);
// 获取tuple t1 的第 1 个元素
char c = get<0>(t1);

浙公网安备 33010602011771号