当前位置: 首页 > article >正文

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.kler.cn/a/557835.html

相关文章:

  • Python Matplotlib图形美化指南
  • C#DevExpress使用GridLookUpEdit实现订单明细选择商品
  • vue3 文件类型传Form Data数据格式给后端
  • 《[含文档+PPT+源码等]精品基于Python实现的Django中药材在线学习系统的设计与实现
  • 和平之翼代码生成器 SHCEU 版 4.0.0RC6 千年隼介绍二
  • 通过wifi无线方式,通过adb命令连接手机,用来传输文件和安装app
  • Open WebUI选择模型为空,解决办法(for DeepSeek)
  • Ubuntu 下 nginx-1.24.0 源码分析 - ngx_atoi 函数
  • 嵌入式之宏定义编程机制
  • 代码随想录算法训练营第十一天|150. 逆波兰表达式求值|239. 滑动窗口最大值|347.前 K 个高频元素
  • 在WPS中设置word的页码不从第一页开始,从指定页开始插入页码
  • Ops 详解:从 DevOps 到 SecOps,探索网络安全与运维的核心概念
  • [STM32 - 野火] - - - 固件库学习笔记 - - - 十六.在SRAM中调试代码
  • 跟着 Lua 5.1 官方参考文档学习 Lua (6)
  • 使用Docker Desktop部署GitLab
  • CUDA兼容NVIDA版本关系
  • 《Keras 2 :使用 RetinaNet 进行对象检测》:此文为AI自动翻译
  • LLaMA-Factory|微调大语言模型初探索(3),qlora微调deepseek记录
  • 标准解读|汽车信息安全领域发布三项强制性国家标准,汽车测评领域新变革
  • Hutool - Log:自动识别日志实现的日志门面