这次天梯还凑合吧只能说,25分的题目自己基本都会相关的算法,运气好一些,35分的题就不会了
这里补一下链表去重的那个题
题目大意:
给予头指针和相关的链表数目,然后求出绝对值不同的链表,然后再求出需要删除的链表
解题思路:
- 这题一开始想复杂了,想怎么判断下一个(甚至是下下个)是否出现,导致写的很乱
- 最后改成了首先判断当前的指针的值是否出现,如果出现,将头指针存入删除的数组中,并继续,如果未出现,存入输出的数组中,然后继续即可。
- 最终要判断下知否存在y值(网上代码有错的)
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <unordered_map>
#include <stack>
#include <cmath>
#include <deque>
using namespace std;
typedef long long ll;
const int N = 1000010, M = 200010;
const int mod = 1e9 + 7;
bool st[N];
int c[N],b[N];
struct node
{
int sc,end;
}a[N];
int main(){
int first, n;
scanf("%d %d",&first,&n);
for (int i = 1; i <= n; i++){
int x, y , z;
scanf("%d%d%d",&x,&y,&z);
a[x].sc = y;
a[x].end = z;
}
int x = 0, y = 0;
while(first != -1){
int t = fabs(a[first].sc);
if (!st[t]){
st[t] = true;
b[x++] = first;
}
else{
c[y ++] = first;
}
first = a[first].end;
}
for (int i = 0; i < x - 1; i ++){
printf("%05d %d %05d\n",b[i],a[b[i]].sc,b[i + 1]);
}
printf("%05d %d -1\n",b[x - 1],a[b[x - 1]].sc);
for (int i = 0; i < y - 1; i ++){
printf("%05d %d %05d\n",c[i],a[c[i]].sc,c[i + 1]);
}
if (y) printf("%05d %d -1\n",c[y - 1],a[c[y - 1]].sc);
return 0;
}