LeetCode开心刷题十二天——23Merge k Sorted Lists

忘是亡心i 2023-08-17 15:14 166阅读 0赞

特殊问题:

1.有时候编译器会提一些无理取闹的错误,很有可能是基本的地方出了问题,比如int main()后面的()可能没带,最好的方法是,新建一个代码,对比看有哪些基本的地方没做好。这种基本都是因为在本身的代码改了很久或者用的别的软件过来的代码

2.还可以用网上的在线编译查看,说的会比codeblocks里的编译器清楚一些

知识拓展:

1.模板 template

用法:不对变量类型进行限制,有点类似C11的auto

举例:比较两个值的大小,值可能是int,float,double不确定,所以需要在编写比较函数时,用模板的定义代替变量类型

template 这个是定义模板的固定格式,规定了的..模板应该可以理解到它的意思吧.. 比如你想求2个int float 或double型变量的值,只需要定义这么一个函数就可以了,假如不用模板的话,你就必须针对每种类型都定义一个sum函数..int sum(int, int);float sum(float, float);double sum(double, double);

  1. 1.因为T是一个模版实例化时才知道的类型,所以编译器更对T不知所云,为了通知编译器T是一个合法的类型,使用typename语句可以避免编译器报错。
  2. 2.template < typename var_name > class class_name; 表示var_name是一个类型, 在模版实例化时可以替换任意类型,不仅包括内置类型(int等),也包括自定义类型class 换句话说,在template<typename Y>和template<class Y>中,
  3. typenameclass的意义完全一样。

代码:

  1. <pre name="code" class="cpp">// TemplateTest.cpp : 定义控制台应用程序的入口点。
  2. ///<summary>
  3. ///测试C++中的template(模板)的使用方法 最新整理时间2016.5.21
  4. ///</summary>
  5. ///<remarks>1:template的使用是为了简化不同类型的函数和类的重复定义.
  6. ///<remarks>2:char类型变量c,d输入的都是字母,不是数字,如输入32则c=3,d=2.
  7. #include "stdafx.h"
  8. #include <iostream>
  9. #include<vector>
  10. using namespace std;
  11. template <typename T>
  12. T mmax(T a,T b)
  13. {
  14. return a>b?a:b;
  15. }
  16. int _tmain(int argc, _TCHAR* argv[])
  17. {
  18. cout<<"Please enter the value of a and b:"<<endl;
  19. int a,b;
  20. cin>>a>>b;
  21. cout<<mmax(a,b)<<endl;
  22. cout<<"Please enter the value of c and d:"<<endl;
  23. char c,d;
  24. cin>>c>>d;
  25. cout<<mmax(c,d)<<endl;
  26. cout<<"Please enter the value of f and g:"<<endl;
  27. double f,g;
  28. cin>>f>>g;
  29. cout<<mmax(f,g)<<endl;
  30. while(1);
  31. return 0;
  32. }

唯一注意是,这个是在studio中跑的,在codeblocks中要更改

2.堆heap 《=》priority_queue

heap is a special tool in STL can help you make queue(or other containers in order automatically)

usage:

1005283-20190706005308716-152955549.png

Code Sample:

  1. #include <functional>
  2. #include <queue>
  3. #include <vector>
  4. #include <iostream>
  5. //this code show the usage of template and priority_queue in different situation
  6. template<typename T> void print_queue(T& q) {
  7. while(!q.empty()) {
  8. std::cout << q.top() << " ";
  9. q.pop();
  10. }
  11. std::cout << '\n';
  12. }
  13. int main() {
  14.   //normal definition
  15. std::priority_queue<int> q;
  16. for(int n : {
  17. 1,8,5,6,3,4,0,9,7,2})
  18. q.push(n);
  19. print_queue(q);
  20.   //specify parameter
  21. std::priority_queue<int, std::vector<int>, std::greater<int> > q2;
  22. for(int n : {
  23. 1,8,5,6,3,4,0,9,7,2})
  24. q2.push(n);
  25. print_queue(q2);
  26. //use lambda specify compare function
  27. // 用 lambda 比较元素。
  28. auto cmp = [](int left, int right) { return (left ^ 1) < (right ^ 1);};
  29. std::priority_queue<int, std::vector<int>, decltype(cmp)> q3(cmp);
  30. for(int n : {
  31. 1,8,5,6,3,4,0,9,7,2})
  32. q3.push(n);
  33. print_queue(q3);
  34. }

特殊问题:

1.有时候编译器会提一些无理取闹的错误,很有可能是基本的地方出了问题,比如int main()后面的()可能没带,最好的方法是,新建一个代码,对比看有哪些基本的地方没做好。这种基本都是因为在本身的代码改了很久或者用的别的软件过来的代码

2.还可以用网上的在线编译查看,说的会比codeblocks里的编译器清楚一些

知识拓展:

1.模板 template

用法:不对变量类型进行限制,有点类似C11的auto

举例:比较两个值的大小,值可能是int,float,double不确定,所以需要在编写比较函数时,用模板的定义代替变量类型

template 这个是定义模板的固定格式,规定了的..模板应该可以理解到它的意思吧.. 比如你想求2个int float 或double型变量的值,只需要定义这么一个函数就可以了,假如不用模板的话,你就必须针对每种类型都定义一个sum函数..int sum(int, int);float sum(float, float);double sum(double, double);

  1. 1.因为T是一个模版实例化时才知道的类型,所以编译器更对T不知所云,为了通知编译器T是一个合法的类型,使用typename语句可以避免编译器报错。
  2. 2.template < typename var_name > class class_name; 表示var_name是一个类型, 在模版实例化时可以替换任意类型,不仅包括内置类型(int等),也包括自定义类型class 换句话说,在template<typename Y>和template<class Y>中,
  3. typenameclass的意义完全一样。

代码:

  1. <pre name="code" class="cpp">// TemplateTest.cpp : 定义控制台应用程序的入口点。
  2. ///<summary>
  3. ///测试C++中的template(模板)的使用方法 最新整理时间2016.5.21
  4. ///</summary>
  5. ///<remarks>1:template的使用是为了简化不同类型的函数和类的重复定义.
  6. ///<remarks>2:char类型变量c,d输入的都是字母,不是数字,如输入32则c=3,d=2.
  7. #include "stdafx.h"
  8. #include <iostream>
  9. #include<vector>
  10. using namespace std;
  11. template <typename T>
  12. T mmax(T a,T b)
  13. {
  14. return a>b?a:b;
  15. }
  16. int _tmain(int argc, _TCHAR* argv[])
  17. {
  18. cout<<"Please enter the value of a and b:"<<endl;
  19. int a,b;
  20. cin>>a>>b;
  21. cout<<mmax(a,b)<<endl;
  22. cout<<"Please enter the value of c and d:"<<endl;
  23. char c,d;
  24. cin>>c>>d;
  25. cout<<mmax(c,d)<<endl;
  26. cout<<"Please enter the value of f and g:"<<endl;
  27. double f,g;
  28. cin>>f>>g;
  29. cout<<mmax(f,g)<<endl;
  30. while(1);
  31. return 0;
  32. }

唯一注意是,这个是在studio中跑的,在codeblocks中要更改

2.堆heap 《=》priority_queue

heap is a special tool in STL can help you make queue(or other containers in order automatically)

usage:

1005283-20190706005308716-152955549.png

Code Sample:

  1. #include <functional>
  2. #include <queue>
  3. #include <vector>
  4. #include <iostream>
  5. //this code show the usage of template and priority_queue in different situation
  6. template<typename T> void print_queue(T& q) {
  7. while(!q.empty()) {
  8. std::cout << q.top() << " ";
  9. q.pop();
  10. }
  11. std::cout << '\n';
  12. }
  13. int main() {
  14.   //normal definition
  15. std::priority_queue<int> q;
  16. for(int n : {
  17. 1,8,5,6,3,4,0,9,7,2})
  18. q.push(n);
  19. print_queue(q);
  20.   //specify parameter
  21. std::priority_queue<int, std::vector<int>, std::greater<int> > q2;
  22. for(int n : {
  23. 1,8,5,6,3,4,0,9,7,2})
  24. q2.push(n);
  25. print_queue(q2);
  26. //use lambda specify compare function
  27. // 用 lambda 比较元素。
  28. auto cmp = [](int left, int right) { return (left ^ 1) < (right ^ 1);};
  29. std::priority_queue<int, std::vector<int>, decltype(cmp)> q3(cmp);
  30. for(int n : {
  31. 1,8,5,6,3,4,0,9,7,2})
  32. q3.push(n);
  33. print_queue(q3);
  34. }

当前函数有问题,main函数next构建值的方法有问题,导致无法按顺序输出

  1. #include<iostream>
  2. #include<string>
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<iomanip>
  6. #include<vector>
  7. #include<list>
  8. #include<queue>
  9. #include<algorithm>
  10. #include<stack>
  11. #include<map>
  12. using namespace std;
  13. struct ListNode {
  14. int val;
  15. ListNode *next;
  16. ListNode(int x) : val(x), next(NULL) {}
  17. };
  18. class Solution {
  19. public:
  20. ListNode* mergeKLists(vector<ListNode*>& lists) {
  21. ListNode res(0);
  22. ListNode *cur=&res;
  23. //this is a compare function but it's also a sentence so it's need ; to end it;
  24. auto comp = [](ListNode* a,ListNode* b){
  25. return a->val>b->val;
  26. };
  27. priority_queue<ListNode*,vector<ListNode*>,decltype(comp)> q(comp);
  28. for(ListNode* list:lists)
  29. {
  30. if(list)
  31. q.push(list);
  32. }
  33. //cout<<q.size()<<endl;
  34. while(!q.empty())
  35. {
  36. cur->next=q.top();q.pop();
  37. cur=cur->next;
  38. if(cur->next)
  39. {
  40. q.push(cur->next);
  41. }
  42. }
  43. while (res.next!=NULL)
  44. {
  45. cout << res.val << " ";
  46. res=res.next;
  47. }
  48. return res.next;
  49. }
  50. };
  51. void printList(ListNode* node)
  52. {
  53. while (node != NULL)
  54. {
  55. printf("%d ", node->val);
  56. node = node->next;
  57. }
  58. }
  59. // Utility function to create a new node.
  60. //ListNode *newNode(int data)
  61. //{
  62. // struct ListNode *temp = new ListNode(0);
  63. // temp->val = data;
  64. // temp->next = NULL;
  65. // return temp;
  66. //}
  67. // Driver program to test above functions
  68. int main()
  69. {
  70. Solution s;
  71. vector<ListNode*> lists;
  72. ListNode q1(37);
  73. ListNode *res=&q1;
  74. res->next=new ListNode(44);
  75. res=res->next;
  76. res->next=new ListNode(5);
  77. res=res->next;
  78. res->next=new ListNode(6);
  79. lists.push_back(&q1);
  80. ListNode* q2=new ListNode(31);
  81. q2->next=new ListNode(41);
  82. q2->next->next=new ListNode(4);
  83. q2->next->next->next=new ListNode(21);
  84. lists.push_back(q2);
  85. // Merge all lists
  86. ListNode* head = s.mergeKLists(lists);
  87. //printList(q2);
  88. return 0;
  89. }

转载于:https://www.cnblogs.com/Marigolci/p/11141568.html

发表评论

表情:
评论列表 (有 0 条评论,166人围观)

还没有评论,来说两句吧...

相关阅读