comparator 类

vector 里面的

lintcode 391 飞机起降问题

  1. static bool cmp(const vector<int> o1,vector<int> o2){
  2. if(o1[0]!=o2[0]){
  3. return o1[0]< o2[0];
  4. }
  5. return o1[1]<o2[1];// 小于号默认升序
  6. }

要加static,因为sort是个静态函数,在类外需要可以调用,所以调用只能用静态(如果写在类里)

  1. class Nodes {
  2. public:
  3. int value;
  4. int pos;
  5. Nodes(int value, int pos) {
  6. this->value = value;
  7. this->pos = pos;
  8. }
  9. };
  10. class Solution {
  11. public:
  12. /*
  13. * @param nums: A list of integers
  14. * @return: A list of integers includes the index of the first number and the index of the last number
  15. */
  16. static bool cmp(const Nodes* a,const Nodes* b) {
  17. return a->value < b->value;
  18. }
  19. vector<int> subarraySumClosest(vector<int>& nums) {
  20. // write your code here
  21. vector <Nodes *> s;
  22. vector <int> res(2);
  23. Nodes* temp= new Nodes(0, -1);
  24. s.push_back(temp);
  25. int sum = 0, len = nums.size();
  26. for (int i = 0; i < len; i++) {
  27. sum += nums[i];
  28. Nodes* temp= new Nodes(sum, i);
  29. s.push_back(temp);
  30. }
  31. sort(s.begin(), s.end(), cmp);
  32. len = s.size();
  33. int ans = INT_MAX;
  34. for (int i = 0; i < len - 1; i++) {
  35. if ((s[i + 1]->value - s[i]->value) < ans) {
  36. ans = s[i + 1]->value - s[i]->value;
  37. //cout << ans;
  38. res[0] = min(s[i]->pos, s[i + 1]->pos) + 1;
  39. res[1] = max(s[i]->pos, s[i + 1]->pos);
  40. }
  41. }
  42. return res;
  43. }
  44. };

queue 里面

structure cmp

  1. struct cmp{
  2. bool operator()(const node& a, const node& b){
  3. return a.key>b.key;// 大于号默认小顶堆
  4. }
  5. }

image.pngxi

string 和 int 相互转化

  1. int temp = num;
  2. vector <int> tempRes;
  3. string str = to_string(num);
  4. int intStr = atoi(str.c_str());

vector知识点

某一些项放在另外一项之后
res.insert(res.end(), temp.begin(), temp.end());