数据结构
酷小川
研究方向:嵌入式
展开
-
C++ map遍历删除的正确写法
map<string,int> testMap;for(auto it = testMap.begin(); it != testMap.end(); ++it){ if(it->second == xxx) { testMap.erase(it); //这里会出问题 }} 错误原因:it指针被erase之后会失效,for循环中对it操作其结果都是不可预料的,可能造成程序崩溃。修改后的正确写法如下:map<stri.原创 2021-03-18 14:47:36 · 9247 阅读 · 1 评论 -
C++ 链表的交点
方法一:利用set容器struct ListNode{ int val; ListNode *next; ListNode(int x):val(x),next(NULL){}};ListNode* getInstersectionNode(ListNode *headA, ListNode *headB){ std::set<ListNode*> node_set; while (headA) { node_set.insert(headA); head原创 2020-10-27 00:36:19 · 237 阅读 · 0 评论 -
C++ 链表反转与链表区域反转
// test1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。//#include "pch.h"#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <string>#include <vector>#include <vector>#include <stdio.h>#include <string.h>#include.原创 2020-10-20 14:00:31 · 236 阅读 · 0 评论 -
C++复杂链表的复制
输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针random指向一个随机节点),请对此链表进行深拷贝,并返回拷贝后的头结点。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)/*struct RandomListNode { int label; struct RandomListNode *next, *random; RandomListNode(int x) : label(x),原创 2020-08-12 23:45:40 · 668 阅读 · 0 评论