comparator 类
vector 里面的
lintcode 391 飞机起降问题
static bool cmp(const vector<int> o1,vector<int> o2){
if(o1[0]!=o2[0]){
return o1[0]< o2[0];
}
return o1[1]<o2[1];// 小于号默认升序
}
要加static,因为sort是个静态函数,在类外需要可以调用,所以调用只能用静态(如果写在类里)
class Nodes {
public:
int value;
int pos;
Nodes(int value, int pos) {
this->value = value;
this->pos = pos;
}
};
class Solution {
public:
/*
* @param nums: A list of integers
* @return: A list of integers includes the index of the first number and the index of the last number
*/
static bool cmp(const Nodes* a,const Nodes* b) {
return a->value < b->value;
}
vector<int> subarraySumClosest(vector<int>& nums) {
// write your code here
vector <Nodes *> s;
vector <int> res(2);
Nodes* temp= new Nodes(0, -1);
s.push_back(temp);
int sum = 0, len = nums.size();
for (int i = 0; i < len; i++) {
sum += nums[i];
Nodes* temp= new Nodes(sum, i);
s.push_back(temp);
}
sort(s.begin(), s.end(), cmp);
len = s.size();
int ans = INT_MAX;
for (int i = 0; i < len - 1; i++) {
if ((s[i + 1]->value - s[i]->value) < ans) {
ans = s[i + 1]->value - s[i]->value;
//cout << ans;
res[0] = min(s[i]->pos, s[i + 1]->pos) + 1;
res[1] = max(s[i]->pos, s[i + 1]->pos);
}
}
return res;
}
};
queue 里面
structure cmp
struct cmp{
bool operator()(const node& a, const node& b){
return a.key>b.key;// 大于号默认小顶堆
}
}
string 和 int 相互转化
int temp = num;
vector <int> tempRes;
string str = to_string(num);
int intStr = atoi(str.c_str());
vector知识点
某一些项放在另外一项之后
res.insert(res.end(), temp.begin(), temp.end());