知道是知道,笔试里怎么都写不出来orz
愿天堂没有需要解决输入输出的笔试
以leetcode23题为例子
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
vector<ListNode*> lists;
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2){
ListNode *head = new ListNode(-1);
ListNode *cur = head;
while (l1 && l2) {
if (l1->val < l2->val) {
cur->next = l1;
l1 = l1->next;
} else {
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
if (l1) cur->next = l1;
if (l2) cur->next = l2;
return head->next;
}
ListNode* mergeKLists(vector<ListNode*>& lists)
{
if (lists.size() == 0) return NULL;
int n = lists.size();
while (n > 1) {
int k = (n + 1) / 2;
for (int i = 0; i < n / 2; ++i) {
lists[i] = mergeTwoLists(lists[i], lists[i + k]);
}
n = k;
}
return lists[0];
}
int main()
{
int num;
int m;
char c;
cin>>m;
for(int i=0;i<m;i++)
{
cin>>num;
ListNode *p=new ListNode(-1);
ListNode *head =new ListNode(num);
p=head;
while((c = getchar()) != '\n'){
cin>>num;
ListNode *cur=new ListNode(num);
p->next=cur;
p=cur;
}
lists.push_back(head);
}
printf("**********\n");
ListNode *ans=mergeKLists(lists);
while(ans)
{
printf("%d ",ans->val);
ans=ans->next;
}
}
//一点基操
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct node{
int data;
node* next;
};
node* create(int Array[])
{
node*p,*head,*pre;
head= new node;
head->next=NULL;
pre=head;
for(int i=0;i<5;i++)
{
p=new node;
p->data=Array[i];
p->next=NULL;
pre->next=p;
pre=p;
}
return head;
}
int search(node* head,int x)
{
int count=0;
node *p=head->next;
while(p)
{
if(p->data==x)
count++;
p=p->next;
}
return count;
}
void insert(node*head,int pos,int x)
{
node * p=head;
for(int i=0;i<pos-1;i++)
p=p->next;
node *q=new node;
q->data=x;
q->next=p->next;
p->next=q;
}
void del(node*head,int x)
{
node*pre=head;
node*p=head->next;
while(p)
{
if(p->data==x)
{
pre->next=p->next;
delete(p);
p=pre->next;
}else
{
pre=p;
p=p->next;
}
}
}
int main()
{
int Array[5]={1,2,3,4,5};
node* L=create(Array);
L=L->next;
while(L)
{
printf("%d ",L->data);
L=L->next;
}
return 0;
}