力扣hot100——链表

160. 相交链表

class Solution {
public:ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {set<ListNode*> s;ListNode* h = headA;while (h != NULL) {s.insert(h);h = h->next;}h = headB;while (h != NULL){if (s.find(h) != s.end()) {return h;}h = h->next;}return NULL;}
};

set存地址,模拟

 206. 反转链表

class Solution {
public:ListNode* ans;void dfs(ListNode* pre, ListNode* now) {if (now->next == NULL) {now->next = pre;ans = now;return ;}dfs(now, now -> next);now->next = pre;}ListNode* reverseList(ListNode* head) {if (head == NULL) return NULL;if (head -> next == NULL) return head;dfs(head, head->next);head->next = NULL;return ans;}
};

 递归模拟,注意head->next 要修改成 NULL 避免死循环

 234. 回文链表

class Solution {
public:bool isPalindrome(ListNode* head) {vector<int> a;while (head != NULL) {a.push_back(head->val);head = head->next;}for (int i = 0, j = a.size() - 1; i < j; i++, j--) {if (a[i] != a[j]) return false;}return true;}
};

 模拟

 141. 环形链表

class Solution {
public:bool hasCycle(ListNode* head) {while (head != NULL) {if (head->val == 100001) return true;head->val = 100001;head = head->next;}return false;}
};

修改val做标记

142. 环形链表 II

class Solution {
public:ListNode* detectCycle(ListNode* head) {set<ListNode*> s;while (head != NULL) {if (s.count(head)) return head;s.insert(head);head = head->next;}return NULL;}
};

 set模拟

21. 合并两个有序链表

class Solution {
public:ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {if (list1 == NULL) return list2;if (list2 == NULL) return list1;ListNode* ans = new ListNode();if (list1->val <= list2->val) ans = list1;else ans = list2;ListNode* h = new ListNode();while (list1 || list2) {if (!list1) {h->next = list2;list2 = list2->next;}else if (!list2) {h->next = list1;list1 = list1->next;}else {if (list1->val <= list2->val) {h->next = list1;list1 = list1->next;}else {h->next = list2;list2 = list2->next;}}h = h->next;}return ans;}
};

双指针模拟

 2. 两数相加

class Solution {
public:ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {ListNode* h = new ListNode(0);ListNode* ans = h;int t = 0;while (l1 || l2) {ListNode* now = new ListNode(0);if (!l1) {int x = l2->val + t;now->val = x % 10;t = x / 10;l2 = l2->next;}else if (!l2) {int x = l1->val + t;now->val = x % 10;t = x / 10;l1 = l1->next;}else {int x = l1->val + l2->val + t;now->val = x % 10;t = x / 10;l1 = l1->next;l2 = l2->next;}h->next = now;h = h->next;}if (t) {h->next = new ListNode(t);}return ans->next;}
};

 双指针模拟

19. 删除链表的倒数第 N 个结点

class Solution {
public:int dfs(ListNode* now, int n) {if (now == NULL) return 0;int v = dfs(now->next, n) + 1;if (v == n + 1) {now->next = now->next->next;}return v;}ListNode* removeNthFromEnd(ListNode* head, int n) {int v = dfs(head, n);if (v == n) {head = head->next;}return head;}
};

dfs求链表层数,模拟

 24. 两两交换链表中的节点

class Solution {
public:ListNode* swapPairs(ListNode* head) {if (head == NULL || head->next == NULL) return head;ListNode* ans = head->next;ListNode* l1 = head, * l2 = head->next;ListNode* pre = new ListNode(0, l1);while (l1 && l2) {pre->next = l2;l1->next = l2->next;l2->next = l1;swap(l1, l2);pre = l2;l1 = l2->next;if (l2->next == NULL) break;l2 = l2->next->next;}return ans;}
};

 模拟

25. K 个一组翻转链表

class Solution {
public:ListNode* reverseKGroup(ListNode* head, int k) {ListNode* dummy = new ListNode(0, head);ListNode* groupPre = dummy;while (true) {int kth = k;ListNode* cur = groupPre;while (kth && cur) {kth--;cur = cur->next;}if (!cur) break;ListNode* groupNext = cur->next;ListNode* pre = groupNext;cur = groupPre->next;ListNode* tmp = NULL;while (cur != groupNext) {tmp = cur->next;cur->next = pre;pre = cur;cur = tmp;}tmp = groupPre->next;groupPre->next = pre;groupPre = tmp;}return dummy->next;}
};

 四个指针,groupPre、groupNext、pre、cur模拟

 138. 随机链表的复制

class Solution {
public:Node* copyRandomList(Node* head) {Node* tmp = head;Node* dummy = new Node(0);dummy->next = head;Node* pre = dummy;map<Node*, Node*> mp;while (head) {Node* cur = new Node(head->val);pre->next = cur;mp[head] = cur;pre = cur;head = head->next;}head = tmp;tmp = dummy->next;while (head) {tmp->random = mp[head->random];tmp = tmp->next;head = head->next;}return dummy->next;}
};

 map从源地址映射到新地址

148. 排序链表

class Solution {
public:ListNode* sortList(ListNode* head) {auto length = [](ListNode* head)->int {int ans = 0;while (head) {ans++;head = head->next;}return ans;};auto spiltList = [](ListNode* head, int step) -> ListNode* {ListNode* cur = head;for (int i = 0; i < step - 1 && cur; i++) {cur = cur->next;}if (!cur || cur->next == NULL) {return NULL;}ListNode* next_head = cur->next;cur->next = NULL;return next_head;};auto mergeList = [](ListNode* head1, ListNode* head2) -> pair<ListNode*, ListNode*> {ListNode* dummy = new ListNode(0);ListNode* cur = dummy;while (head1 && head2) {if (head1->val <= head2->val) {cur->next = head1;head1 = head1->next;}else {cur->next = head2;head2 = head2->next;}cur = cur->next;}while (head1) {cur->next = head1;head1 = head1->next;cur = cur->next;}while (head2) {cur->next = head2;head2 = head2->next;cur = cur->next;}return { dummy->next, cur };};int n = length(head);ListNode* dummy = new ListNode(0, head);for (int i = 2; i <= n; i *= 2) {ListNode* new_list_tail = dummy;ListNode* cur = dummy->next;while (cur) {ListNode* head1 = cur;ListNode* head2 = spiltList(head1, i);cur = spiltList(head2, i);auto [head, tail] = mergeList(head1, head2);new_list_tail->next = head;new_list_tail = tail;}}return dummy->next;}
};

迭代法归并排序

spiltList是断开链表并返回它的头节点

mergeList是合并两个链表并返回新链表的头尾节点

 23. 合并 K 个升序链表

class Solution {
public:ListNode* mergeKLists(vector<ListNode*>& lists) {ListNode* dummy = new ListNode();multiset<pair<int, ListNode*>> s;for (auto list : lists) {if (list) s.insert({ list->val, list });}ListNode* cur = dummy;while (s.size()) {auto it = s.begin();auto [x, list] = *it;s.erase(it);cur->next = list;cur = cur->next;list = list->next;if (list) {s.insert({ list->val, list });}}return dummy->next;}
};

 优先队列模拟

146. LRU 缓存

class LRUCache {
public:int capacity = 0;int size = 0;unordered_map<int, int> mp;unordered_map<int, int> cnt;queue<int> q;LRUCache(int capacity) {this->capacity = capacity;}   int get(int key) {if (!cnt[key]) return -1;cnt[key]++;q.push(key);return mp[key];}void put(int key, int value) {if (size < capacity) {q.push(key);if (!cnt[key]) size++;cnt[key]++;}else {q.push(key);if (!cnt[key]) {while (true) {int x = q.front();q.pop();cnt[x]--;if (!cnt[x]) {break;}        }}cnt[key]++;}mp[key] = value;}
};

自己实现了一个链表结构,用双向链表维护

对于LRU队列:定义一个计时器,每加入或者访问一个元素,直接将其加入到队尾,将其计数+1。每删除一个元素,从队头开始一直删除,每次删除其对应计数-1,直到删除后它的计数变成0

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/66225.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Qt打包为exe文件

个人学习笔记 选择release 进入项目文件夹&#xff0c;查看releas生成的文件 releas文件路径 进入release看到exe文件&#xff0c;但是无法执行 将exe文件单独放到一个文件夹内 选择MinGW 用CD 进入存放exe文件的路径&#xff0c;输入下面指令 cd J:\C\Qt\test4-3-1 windeploy…

VScode怎么重启

原文链接&#xff1a;【vscode】vscode重新启动 键盘按下 Ctrl Shift p 打开命令行&#xff0c;如下图&#xff1a; 输入Reload Window&#xff0c;如下图&#xff1a;

小屏幕下通过css自动实现上下位置颠倒例子

<!DOCTYPE html> <html><head><meta charset"utf-8"><meta name"viewport" content"widthdevice-width, initial-scale1"><title>Demo</title><!-- 请勿在项目正式环境中引用该 layui.css 地址 --…

下列指标组合中,不能用于系统性评价缺陷识别模型精度的指标

19 下列指标组合中&#xff0c;不能用于系统性评价缺陷识别模型精度的指标为&#xff1a; A 检出率和准确率 B 检出率和误报比 C 平均精确率 D 准确率和误报比 对于评价一个缺陷识别模型的精度&#xff0c;检出率、准确率、检出率和误报比等指标常被用来系统性地评估模型的效…

Web安全 - “Referrer Policy“ Security 头值不安全

文章目录 概述原因分析风险说明Referrer-Policy 头配置选项1. 不安全的策略no-referrer-when-downgradeunsafe-url 2. 安全的策略no-referreroriginorigin-when-cross-originsame-originstrict-originstrict-origin-when-cross-origin 推荐配置Nginx 配置示例 在 Nginx 中配置 …

spring mvc源码学习笔记之六

pom.xml 内容如下 <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation"http://maven.apache.org/P…

Hyperbolic dynamics

http://www.scholarpedia.org/article/Hyperbolic_dynamics#:~:textAmong%20smooth%20dynamical%20systems%2C%20hyperbolic%20dynamics%20is%20characterized,semilocal%20or%20even%20global%20information%20about%20the%20dynamics. 什么是双曲动力系统&#xff1f; A hy…

vue3组件化开发优势劣势分析,及一个案例

Vue 3 组件化开发的优势和劣势 优势 可复用性&#xff1a; 组件可以重复使用&#xff0c;减少代码冗余&#xff0c;提高开发效率。 可以在不同的项目中复用组件&#xff0c;提升开发速度。 可维护性&#xff1a; 组件化开发使得代码结构清晰&#xff0c;易于维护。 每个…

基于SpringBoot在线竞拍平台系统功能实现十五

一、前言介绍&#xff1a; 1.1 项目摘要 随着网络技术的飞速发展和电子商务的普及&#xff0c;竞拍系统作为一种新型的在线交易方式&#xff0c;已经逐渐深入到人们的日常生活中。传统的拍卖活动需要耗费大量的人力、物力和时间&#xff0c;从组织拍卖、宣传、报名、竞拍到成…

Tomcat性能优化与负载均衡实现

在现代互联网应用中&#xff0c;Apache Tomcat作为一个广泛使用的Java Web应用服务器&#xff0c;扮演着至关重要的角色。随着用户数量的不断增加和业务的不断扩展&#xff0c;如何提升Tomcat的性能和实现高可用性成为了开发者们关注的焦点。本文将介绍Tomcat的性能优化技巧以及…

Ubuntu 搭建SVN服务

目录 ​ 1、安装SVN服务端 2、创建SVN版本库 3、修改SVN配置svnserve.conf 3.1 配置文件介绍 3.2 svnserve.conf配置 3.3 authz配置设置用户读写权限 3.4 passwd配置 用户名密码 4、启动SVN服务 4.1 配置开机启动 1、安装SVN服务端 sudo apt-get install subversion…

DataV数据可视化

阿里云 DataV 是一个强大的数据可视化工具&#xff0c;可以帮助用户通过创建丰富的图表、仪表盘、地图和互动视图&#xff0c;将复杂的数据转化为易于理解和分析的可视化信息。DataV主要用于大数据和实时数据的展示&#xff0c;可以帮助企业和个人更直观地理解数据背后的含义&a…

电子电气架构 --- 整车整车网络管理浅析

我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 所谓鸡汤,要么蛊惑你认命,要么怂恿你拼命,但都是回避问题的根源,以现象替代逻辑,以情绪代替思考,把消极接受现实的懦弱,伪装成乐观面对不幸的…

基于 Python 大数据的购物车智能推荐与分析系统

标题:基于 Python 大数据的购物车智能推荐与分析系统 内容:1.摘要 随着电子商务的快速发展&#xff0c;购物车智能推荐与分析系统成为了提高用户购物体验和促进销售的重要手段。本系统基于 Python 大数据技术&#xff0c;通过对用户购物行为数据的分析&#xff0c;为用户提供个…

neo4j学习笔记

图数据库 图数据库是基于图论实现的一种NoSQL数据库&#xff0c;其数据存储结构和数据查询方式都是图论为基础的&#xff0c;图数据库主要用于存储更多的连接数据。 图论&#xff08;GraphTheory&#xff09;是数学的一个分支。图论以图为研究对象&#xff0c;图论的图是由若干…

REMARK-LLM:用于生成大型语言模型的稳健且高效的水印框架

REMARK-LLM:用于生成大型语言模型的稳健且高效的水印框架 前言 提出这一模型的初衷为了应对大量计算资源和数据集出现伴随的知识产权问题。使用LLM合成类似人类的内容容易受到恶意利用,包括垃圾邮件和抄袭。 ChatGPT等大语言模型LLM的开发取得的进展标志着人机对话交互的范式…

面试题解,Java中的“对象”剖析

一、说一说JVM中对象的内存布局&#xff1f;new一个对象到底占多大内存&#xff1f; 话不多说&#xff0c;看下图&#xff0c;对象的内存布局图 一个对象的内存布局主要由三部分组成&#xff1a;对象头&#xff08;Object Header&#xff09;、实例数据&#xff08;Instance D…

DVWA 命令注入写shell记录

payload 127.0.0.1;echo "<?php eval($_POST["md"]);?>" > md.php 成功写入&#xff0c;访问查看 成功解析

MySQL(五)MySQL图形化工具-Navicat

1. MySQL图形化工具-Navicat Navicat是一套快速、可靠的数据库管理工具&#xff0c;Navicat是以直觉化的图形用户界面而建的&#xff0c;可以兼容多种数据库&#xff0c;支持多种操作系统。   Navicat for MySQL是一款强大的 MySQL 数据库管理和开发工具&#xff0c;它为专业…

union的实际使用

记录一下&#xff0c;免得忘记&#xff1a; 1、定义一个共用体变量 这里定义一个64位变量 i2creg_rev&#xff0c;然后通过共用体定义两个位变量bits和bits_reverse&#xff0c;通过bit可以访问指定位的值大小&#xff0c;不需要自己再左移右移转换。 bits_reverse是bits的对…