红桃J

用心写好每行完美的代码,远比写一堆更有价值

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

代码:这个代码是有问题的,问题的产生是map中不能存放相同的值。

  1 #include<iostream>
  2 #include<vector>
  3 #include<cmath>
  4 #include<map>
  5 
  6 using namespace std;
  7 
  8 typedef struct ListNode {
  9     int val;
 10     ListNode *next;
 11     ListNode(int x) : val(x), next(NULL) {}
 12 }ListNode;
 13 
 14 inline int left(int x)
 15 {
 16     return 2 * x + 1;
 17 }
 18 
 19 inline int right(int x)
 20 {
 21     return 2 * x + 2;
 22 }
 23 
 24 void swap(int &x, int &y)
 25 {
 26     int c = x;
 27     x = y;
 28     y = c;
 29 }
 30 
 31 int QQ;
 32 
 33 //    以i节点为根节点进行,堆的维护;假设i节点下面的节点都是堆了。
 34 void MaxHeap(int a[], int i, int k,int ok)
 35 {
 36     int l = left(i);
 37     int r = right(i);
 38     int smallest = i;
 39     if (l < k  && a[l] < a[i])
 40     {
 41         smallest = l;
 42     }
 43     if (r<k&&a[r] < a[smallest])
 44     {
 45         smallest = r;
 46     }
 47     if (i == smallest)
 48     {
 49         return;
 50     }
 51     else
 52     {
 53         swap(a[smallest], a[i]);
 54         return MaxHeap(a, smallest, k, 1);
 55     }
 56 }
 57 
 58 ListNode *mergeKLists(vector<ListNode *> &lists) {
 59     int k = lists.size();
 60     int *flag = new int[k];    //    标志位
 61     int *a = new int[k];
 62     ListNode *head;
 63     map<int, int> cmap;
 64     int n = k;
 65     ListNode **p = (ListNode**)malloc(sizeof(ListNode*)*k);
 66     for (int i = 0; i < k; i++)
 67     {
 68         p[i] = lists[i];
 69         a[i] = p[i]->val;
 70         cmap.insert(make_pair(a[i], i));
 71         if (p[i] == NULL)
 72         {
 73             flag[i] = 0;
 74         }
 75         else
 76         {
 77             flag[i] = 1;
 78         }
 79     }
 80     MaxHeap(a, 0, k,0);
 81     int t = cmap[a[0]];
 82     head = lists[t];
 83     ListNode *m = lists[t];
 84     p[t] = p[t]->next;
 85     while (n != 1)
 86     {
 87         n = k;
 88         for (int i = 0; i < k; i++)
 89         {
 90             if (p[i] == NULL)
 91             {
 92                 flag[i] = 0;
 93                 n--;
 94             }
 95         }
 96         if (flag[t] == 0)
 97         {
 98             a[0] = a[n];
 99             MaxHeap(a, 0, n, 0);
100             t = cmap[a[0]];
101             m->next = p[t];
102             m = m->next;
103             p[t] = p[t]->next;
104         }
105         else
106         {
107             a[0] = p[t]->val;
108             cmap.insert(make_pair(a[0], t));
109             MaxHeap(a, 0, n, 0);
110             t = cmap[a[0]];
111             m->next = p[t];
112             m = m->next;
113             p[t] = p[t]->next;
114         }
115     }
116     ListNode * last;
117     for (int i = 0; i < k; i++)
118     {
119         if (p[i] != NULL)
120         {
121             last = p[i];
122         }
123     }
124     m->next = last;
125     return head;
126 }
127 
128 int main()
129 {
130     /*int a[] = {3,1,4,2,3,5,6};
131     MaxHeap(a, 0, 7, 0);
132     cout << "标识"<<QQ << endl;
133     for (int i = 0; i < 7; i++)
134     {
135         cout << a[i] << endl;
136     }*/
137     int a[6] = {1,3,9,12, 37, 56};
138     int b[7] = {2,5, 8, 11, 35,66,77};
139     int c[4] = { 4, 7, 10, 34 };
140     vector<ListNode*> p;
141     
142     ListNode *temp = (ListNode *)malloc(sizeof(ListNode));
143     ListNode *head = temp;
144     for (int i = 0; i < 6; i++)
145     {
146         temp->val = a[i];
147         if (i != 5)
148             temp->next = (ListNode *)malloc(sizeof(ListNode));
149         else
150             temp->next = NULL;
151         temp = temp->next;
152     }
153     ListNode *temp1 = (ListNode *)malloc(sizeof(ListNode));
154     ListNode *head1 = temp1;
155     for (int i = 0; i < 7; i++)
156     {
157         temp1->val = b[i];
158         if (i != 6)
159             temp1->next = (ListNode *)malloc(sizeof(ListNode));
160         else
161             temp1->next = NULL;
162         temp1 = temp1->next;
163     }
164 
165     ListNode *temp2 = (ListNode *)malloc(sizeof(ListNode));
166     ListNode *head2 = temp2;
167     for (int i = 0; i < 4; i++)
168     {
169         temp2->val = c[i];
170         if (i != 3)
171             temp2->next = (ListNode *)malloc(sizeof(ListNode));
172         else
173             temp2->next = NULL;
174         temp2 = temp2->next;
175     }
176 
177     p.push_back(head);
178     p.push_back(head1);
179     p.push_back(head2);
180     for (ListNode * temp = head; temp != NULL; temp = temp->next)
181     {
182         cout << temp->val << endl;
183     }
184     for (ListNode * temp = mergeKLists(p); temp != NULL; temp = temp->next)
185     {
186     cout << temp->val << endl;
187     }
188 }

 

posted on 2015-04-17 09:32  红桃J  阅读(154)  评论(0编辑  收藏  举报