cs106x-lecture14(Autumn 2017)-SPL实现

打卡cs106x(Autumn 2017)-lecture14

(以下皆使用SPL实现,非STL库,后续课程结束会使用STL实现)

1、min

Write a function named min that accepts a pointer to a ListNode representing the front of a linked list. Your function should return the minimum value in the linked list of integers. If the list is empty, you should throw a string exception.

Constraints: Do not construct any new ListNode objects in solving this problem (though you may create as many ListNode* pointer variables as you like). Do not use any auxiliary data structures to solve this problem (no array, vector, stack, queue, string, etc). Your function should not modify the linked list's state; the state of the list should remain constant with respect to your function.

Assume that you are using the ListNode structure as defined below:

解答:

int min(ListNode* front) {int m;if (front == nullptr) {throw std::string("");} else {m = front->data;ListNode* current = front;while (current->next != nullptr) {current = current->next;if (m > current->data) {m = current->data;}}}return m;
}

 

2、countDuplicates

Write a function named countDuplicates that accepts a pointer to a ListNode representing the front of a linked list. Your function should return the number of duplicates in a sorted list. Your code should assume that the list's elements will be in sorted order, so that all duplicates will be grouped together. For example, if a variable named front points to the front of the following sequence of values, the call of countDuplicates(front) should return 7 because there are 2 duplicates of 1, 1 duplicate of 3, 1 duplicate of 15, 2 duplicates of 23 and 1 duplicate of 40:

{1, 1, 1, 3, 3, 6, 9, 15, 15, 23, 23, 23, 40, 40}

Constraints: Do not construct any new ListNode objects in solving this problem (though you may create as many ListNode* pointer variables as you like). Do not use any auxiliary data structures to solve this problem (no array, vector, stack, queue, string, etc). Your function should not modify the linked list's state; the state of the list should remain constant with respect to your function. You should declare the function to indicate this to the caller.

Assume that you are using the ListNode structure as defined below:

解答:

int countDuplicates(ListNode* front) {int count = 0;if (front == nullptr) {return count;} else {ListNode* current = front;while (current->next != nullptr) {if (current->data == current->next->data) {count++;}current = current->next;}}return count;
}

 

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

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

相关文章

【HeadFirst系列之HeadFirstJava】第4天之理解对象的行为:方法操作实例变量

理解对象的行为:方法操作实例变量 在《Head First Java》的第四章节中,作者深入探讨了对象的行为,即方法如何操作实例变量。这一章节的核心思想是:对象的行为由其方法定义,而方法通过操作实例变量来实现这些行为。理解…

自注意力机制和CNN的区别

CNN:一种只能在固定感受野范围内进行关注的自注意力机制。​CNN是自注意力的简化版本。自注意力:具有可学习感受野的CNN。自注意力是CNN的复杂形态,是更灵活的CNN,经过某些设计就可以变为CNN。 越灵活、越大的模型,需要…

网络运维学习笔记 012网工初级(HCIA-Datacom与CCNA-EI)某机构新增:GRE隧道与EBGP实施

文章目录 GRE隧道(通用路由封装,Generic Routing Encapsulation)协议号47实验:思科:开始实施: 华为:开始实施: eBGP实施思科:华为: GRE隧道(通用路…

Mac m1 连接公司内网

1、创建VPN 1、在系统偏好设置 2、选择网络 3、进行添加 2、添加设置 1、选择VPN 2、类型选择L2TP/IPSec 3、填写服务器IP和账号 4、点击认证设置-填写密码 。然后应用 3、进行特殊配置 网上说苹果系统的问题。 1、创建命令 sudo vim /etc/ppp/options 2、添加内容-主要别…

【多模态处理篇七】【DeepSeek传感器融合:IMU+视觉SLAM方案】

大家好,今天我们来聊聊一个非常酷的技术——DeepSeek传感器融合:IMU+视觉SLAM方案。这个技术在现代机器人、自动驾驶、AR/VR等领域都有广泛的应用。我们将从基础概念开始,逐步深入到核心原理和实现细节,力求让大家对这个技术有一个全面而深入的理解。 1. 什么是SLAM? 首…

医疗报销系统的设计与实现(代码+数据库+LW)

摘 要 传统信息的管理大部分依赖于管理人员的手工登记与管理,然而,随着近些年信息技术的迅猛发展,让许多比较老套的信息管理模式进行了更新迭代,报销单信息因为其管理内容繁杂,管理数量繁多导致手工进行处理不能满足…

LangChain 技术入门指南:探索语言模型的无限可能

在当今的技术领域,LangChain 正逐渐崭露头角,成为开发语言模型应用的强大工具。如果你渴望深入了解并掌握这一技术,那么就跟随本文一起开启 LangChain 的入门之旅吧! (后续将持续输出关于LangChain的技术文章,有兴趣的同学可以关注…

windows服务器本地部署deepseek并训练

1.安装python3.10 下载地址 Index of python-local/3.10.9 安装64位windows版本 2.下载到LLaMA-Factory github地址: GitHub - hiyouga/LLaMA-Factory: Unified Efficient Fine-Tuning of 100 LLMs & VLMs (ACL 2024) 3. 修改pip镜像源 -- 将pip使用镜像源更…

孜然单授权系统V2.0PHP授权系统

孜然单授权V1.0系统,延续了2022年开发的孜然多应用授权系统V2.0 变更:多应用变单系统,去除没用的垃圾代码,从0开发,去除了一些没用的功能 完善了开发文档,之前那套是我写着玩的屎山代码,V1.0将展…

智慧校园系统在学生学习与生活中的应用

随着科技的快速发展,智慧校园系统逐渐成为现代教育不可或缺的一部分。它整合了先进的信息技术、物联网技术以及人工智能等,旨在构建一个全面、智能、个性化的学习与生活环境。对于学生而言,这一系统不仅能够极大地提高学习效率,还…

论文笔记-WWWCompanion2024-LLM as Data Augmenters for Cold-Start Item Recommendation

论文笔记-WWW Companion 2024-Large Language Models as Data Augmenters for Cold-Start Item Recommendation 大语言模型作为冷启动项目推荐的数据增强器摘要1.引言2.前言3.LLMs作为数据增强3.1增强数据生成3.2成对比较损失 4.实验4.1实验设置4.2结果和分析4.3超参数实验 5.总…

antv G6绘制流程图

效果图&#xff08;优点&#xff1a;可以自定义每一条折线的颜色&#xff0c;可以自定义节点的颜色&#xff0c;以及折线的计算样式等&#xff09;&#xff1a; 代码&#xff1a; <!-- 流程图组件 --> <template><div id"container"></div>…

(三)趣学设计模式 之 抽象工厂模式!

目录 一、 啥是抽象工厂模式&#xff1f;二、 为什么要用抽象工厂模式&#xff1f;三、 抽象工厂模式怎么实现&#xff1f;四、 抽象工厂模式的应用场景五、 抽象工厂模式的优点和缺点六、 抽象工厂模式与工厂方法模式的区别七、 总结 &#x1f31f;我的其他文章也讲解的比较有…

【GPU驱动】OpenGLES图形管线渲染机制

OpenGLES图形管线渲染机制 OpenGL/ES 的渲染管线也是一个典型的图形流水线&#xff08;Graphics Pipeline&#xff09;&#xff0c;包括多个阶段&#xff0c;每个阶段都负责对图形数据进行处理。管线的核心目标是将图形数据转换为最终的图像&#xff0c;这些图像可以显示在屏幕…

vscode软件中引入vant组件

一、vant简介 Vant 是一个轻量、可靠的移动端组件库&#xff0c;于 2017 年开源。 目前 Vant 官方提供了 Vue 2 版本、Vue 3 版本和微信小程序版本&#xff0c;并由社区团队维护 React 版本和支付宝小程序版本。 官网&#xff1a;介绍 - Vant Weapp 里面的快速上手的教程&a…

Meterpreter之getsystem命令提权原理详解

在 Windows 渗透测试过程中&#xff0c;获取系统的最高权限是攻击者的最终目标。getsystem 是 Metasploit 中用于提权的命令&#xff0c;旨在将当前的 Meterpreter 会话 提升至 NT AUTHORITY\SYSTEM 权限。需要特别注意的是&#xff0c;getsystem 并不是 Metasploit 框架&#…

深入理解HttpSecurity的设计

一、HttpSecurity的应用 在前章节的介绍中我们讲解了基于配置文件的使用方式,也就是如下的使用。 也就是在配置文件中通过 security:http 等标签来定义了认证需要的相关信息,但是在SpringBoot项目中,我们慢慢脱离了xml配置文件的方式,在SpringSecurity中提供了HttpSecurity…

软件集成测试的技术要求

文章目录 一、软件集成测试的概念二、测试对象三、测试目的四、进入条件五、测试内容六、测试环境七、测试实施方一、软件集成测试的概念 软件集成测试(Software Integration Testing),也称部件测试,一种旨在暴露接口以及集成组件间交互时存在的缺陷的测试。集成测试是灰盒…

STM32-有关内存堆栈、map文件

STM32堆栈空间大小设置_stm32堆栈分配大小-CSDN博客 STM32堆栈的大小及内存四&#xff08;五&#xff09;区的分析 - 天街小雨润地狠 - 博客园 .map文件的位置

java方法学习

java 方法 在Java中&#xff0c;方法是类&#xff08;或对象&#xff09;的行为或功能的实现。&#xff08;一起实现一个功能&#xff09;java的方法类似于其他语言的函数&#xff0c;是一段用来完成特定功能的代码片段。 方法是解决一类问题步骤的有序结合。 方法包含于类或…