当前位置:网站首页>Group programming ladder race - exercise set l2-002 linked list de duplication
Group programming ladder race - exercise set l2-002 linked list de duplication
2022-07-04 07:59:00 【Cod_ ing】
characteristic : use Hash surface
#include<iostream>
#include<unordered_map>
#include<map>
#include<string>
#include<vector>
using namespace std;
struct ListNode {
string addr;
string next;
int data;
};
int f_abs(int x) {
if (x > 0) return x;
return -x;
}
int main() {
unordered_map<string, ListNode> LinkList; //Hash surface , Store the original linked list
vector<ListNode> Deleted; // Store the deleted linked list nodes
map<int, int> nums; // Mark whether this number exists
string head;
int n;
cin >> head >> n;
string addr, next;
int data;
int cur = 0;
for (int i = 0; i < n; i++) {
cin >> addr >> data >> next;
LinkList[addr].addr = addr;
LinkList[addr].data = data;
LinkList[addr].next = next;
}
LinkList["-1"]; //address=-1 As the termination condition of iteration
string pre_node = head;
string node = LinkList[head].next;
nums[f_abs(LinkList[pre_node].data)] = 1; // First store the number of header nodes
while (node!= "-1") {
int x = f_abs(LinkList[node].data);
nums[x]++;
if (nums[x]>1) {
// This number has appeared before
if (cur > 0) {
// The previous node of the node in the deleted node linked list needs special treatment , Change next value
Deleted[cur - 1].next = node;
}
Deleted.push_back({
node, "-1", LinkList[node].data }); // Inserted nodes next The assignment is -1
cur++;
LinkList[pre_node].next = LinkList[node].next;
node = LinkList[node].next;
}
else {
pre_node = node;
node = LinkList[node].next;
}
}
//cout << endl;
node = head;
while (node != "-1") {
cout << node << " " << LinkList[node].data << " " << LinkList[node].next<<endl;
node = LinkList[node].next;
}
//cout << endl; Output delete node
for (ListNode x : Deleted) {
cout << x.addr << " " << x.data << " " << x.next << endl;
}
return 0;
}
边栏推荐
猜你喜欢
University stage summary
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
zabbix監控系統自定義監控內容
L2-013 red alarm (C language) and relevant knowledge of parallel search
zabbix监控系统自定义监控内容
I was pressed for the draft, so let's talk about how long links can be as efficient as short links in the development of mobile terminals
谷歌官方回应:我们没有放弃TensorFlow,未来与JAX并肩发展
Take you to master the formatter of visual studio code
Unity opens the explorer from the inspector interface, selects and records the file path
[test de performance] lire jmeter
随机推荐
人生规划(Flag)
The text box displays the word (prompt text) by default, and the text disappears after clicking.
Need help resetting PHP counters - PHP
In the era of low code development, is it still needed?
ZABBIX monitoring system deployment
zabbix 5.0监控客户端
Activiti common operation data table relationship
Leetcode 23. Merge K ascending linked lists
Sports [running 01] a programmer's half horse challenge: preparation before running + adjustment during running + recovery after running (experience sharing)
Project 1 household accounting software (goal + demand description + code explanation + basic fund and revenue and expenditure details record + realization of keyboard access)
Mysql database - function constraint multi table query transaction
Common components of flask
运动【跑步 01】一个程序员的半马挑战:跑前准备+跑中调整+跑后恢复(经验分享)
WordPress get_ Users() returns all users with comparison queries - PHP
L1-027 rental (20 points)
ZABBIX 5.0 monitoring client
论文学习——基于极值点特征的时间序列相似性查询方法
zabbix监控系统自定义监控内容
【性能测试】一文读懂Jmeter
How to get bytes containing null terminators from a string- c#