cs106x-lecture14(Autumn 2017)-SPL实现

news/2025/2/23 14:55:28

打卡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.niftyadmin.cn/n/5863500.html

相关文章

基于ffmpeg+openGL ES实现的视频编辑工具-添加转场(九)

在视频编辑的广阔领域中,转场效果无疑是提升视频流畅性与观赏性的关键要素。巧妙运用转场,能够让不同视频片段之间的衔接更为自然,同时赋予视频独特的创意魅力。本文将深入探讨如何借助 ffmpeg 和 openGL ES 技术,在视频编辑工具中实现丰富多样的转场效果。 一、转场技术原…

DeepSeek最新开源动态:核心技术公布

2月21日午间,DeepSeek在社交平台X发文称,从下周开始,他们将开源5个代码库,以完全透明的方式与全球开发者社区分享他们的研究进展。并将这一计划定义为“Open Source Week”。 DeepSeek表示,即将开源的代码库是他们在线…

使用LangChain构建第一个ReAct Agent

使用LangChain构建第一个ReAct Agent 准备环境 使用Anaconda 安装python 3.10 安装langchain、langchain_openai、langchain_community (安装命令 pip install XXX) 申请DeepSeek API:https://platform.deepseek.com/api_keys(也…

[ComfyUI]Recraft贴图开源方案,实现服装印花自由

一、介绍 今天发现了一个简单又好用的插件,可以实现类似Recraft的贴图功能,这是一个作者开发的ComfyUI插件,叫做Comfyui-Transform 这个插件比我们简单的图像覆盖多了一些可控参数,形状、透明度、倾斜、拉升和混合模式等诸多可控…

前端八股——JS+ES6

前端八股:JSES6 说明:个人总结,用于个人复习回顾,将持续改正创作,已在语雀公开,欢迎评论改正。

CSS按钮点击效果实战:scale(0.95) 与10个交互动画优化指南

[TOC](CSS按钮点击效果实战:scale(0.95) 与10个交互动画优化指南) 导语 在现代 Web 开发中,细腻的交互效果是提升用户体验的关键。通过简单的 CSS 动画(如 transform: scale(0.95)),无需 JavaScript 即可实现高效、流…

解析多模态、Agent与Code模型的演进

引言:AI大模型的技术分化与融合 随着大模型技术的爆发,AI领域正在经历从“单一模态专用”到“多模态通用智能”的进化。**多模态模型(Multimodal Models)**彻底打破了人类感知与表达的界限,Agent模型赋予了AI自主决策…

Github开源AI LLM大语言模型WebUI框架推荐

大型语言模型(LLM)已经成为了一股不可忽视的力量。从对话系统到文本生成,LLM 的应用场景日益广泛。而为了更好地利用这些强大的工具,开发者们需要一个易于使用的界面来与这些模型进行交互。今天,我们就为大家带来了几个…