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

备赛蓝桥杯--算法题目(1)

1. 链表求和

. - 力扣(LeetCode)

class Solution {
public:ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {ListNode *head = nullptr, *tail = nullptr;int carry = 0;while (l1 || l2) {int n1 = l1 ? l1->val: 0;int n2 = l2 ? l2->val: 0;int sum = n1 + n2 + carry;if (!head) {head = tail = new ListNode(sum % 10);} else {tail->next = new ListNode(sum % 10);tail = tail->next;}carry = sum / 10;if (l1) {l1 = l1->next;}if (l2) {l2 = l2->next;}}if (carry > 0) {tail->next = new ListNode(carry);}return head;}
};
2. 分割链表 

. - 力扣(LeetCode)

class Solution {
public:ListNode* partition(ListNode* head, int x) {ListNode* shead=new ListNode;ListNode* srear=shead;ListNode* bhead=new ListNode;ListNode* brear=bhead;ListNode* tmp=head;while(tmp){if((tmp->val)>=x){if(bhead==nullptr){bhead=brear=tmp;}else{brear->next=tmp;brear=brear->next;}}else{if(shead==nullptr){shead=srear=tmp;}else{srear->next=tmp;srear=srear->next;}}tmp=tmp->next;}brear->next=nullptr;srear->next=bhead->next;return shead->next;}
};
3. 最小栈

. - 力扣(LeetCode)

class MinStack {
public:/** initialize your data structure here. */MinStack() {s1=new stack<int>;s2=new stack<int>;}void push(int x) {s1->push(x);if(s2->empty()){s2->push(x);}else{if(x<=s2->top()){s2->push(x);}}}void pop() {if(s1->top()==s2->top()){s2->pop();}s1->pop();}int top() {return s1->top();}int getMin() {return s2->top();}
private:stack<int>* s1;stack<int>* s2;
};
4. 二叉树的前序,中序,后序遍历

. - 力扣(LeetCode)

#include<stack>
class Solution {
public:vector<int> preorderTraversal(TreeNode* root) {vector<int> tmp;if(root!=nullptr){TreeNode* head=root;stack<TreeNode*> s;s.push(head);while(!s.empty()){head=s.top();tmp.push_back(head->val);s.pop();if(head->right!=nullptr){s.push(head->right);}if(head->left!=nullptr){s.push(head->left);}}}return tmp;}
};

. - 力扣(LeetCode)

class Solution {
public:vector<int> inorderTraversal(TreeNode* root) {vector<int> tmp;stack<TreeNode*> s;TreeNode* head=root;while(!s.empty()||head!=nullptr){while(head!=nullptr){s.push(head);head=head->left;}head=s.top();tmp.push_back(head->val);s.pop();head=head->right;}return tmp;}
};

. - 力扣(LeetCode)

class Solution {
public:vector<int> postorderTraversal(TreeNode* root) {vector<int> tmp;stack<TreeNode*> s;TreeNode* head=root;TreeNode* ptr=root;while(!s.empty()||head!=nullptr){while(head!=nullptr){s.push(head);head=head->left;}head=s.top();s.pop();if(head->right==nullptr||head->right==ptr){tmp.push_back(head->val);ptr=head;head=nullptr;}else{s.push(head);head=head->right;}}return tmp;}
};
5. 设计循环队列

. - 力扣(LeetCode)

class MyCircularQueue {
private:int front;int rear;int capacity;vector<int> elements;public:MyCircularQueue(int k) {this->capacity = k + 1;this->elements = vector<int>(capacity);rear = front = 0;}bool enQueue(int value) {if (isFull()) {return false;}elements[rear] = value;rear = (rear + 1) % capacity;return true;}bool deQueue() {if (isEmpty()) {return false;}front = (front + 1) % capacity;return true;}int Front() {if (isEmpty()) {return -1;}return elements[front];}int Rear() {if (isEmpty()) {return -1;}return elements[(rear - 1 + capacity) % capacity];}bool isEmpty() {return rear == front;}bool isFull() {return ((rear + 1) % capacity) == front;}
};
6. 排序数组

. - 力扣(LeetCode)

static int first,last;
class Solution {vector<int> tmp;void mergeSort(vector<int>& nums, int l, int r) {if (l >= r) return;int mid = (l + r) >> 1;mergeSort(nums, l, mid);mergeSort(nums, mid + 1, r);int i = l, j = mid + 1;int cnt = 0;while (i <= mid && j <= r) {if (nums[i] <= nums[j]) {tmp[cnt++] = nums[i++];}else {tmp[cnt++] = nums[j++];}}while (i <= mid) {tmp[cnt++] = nums[i++];}while (j <= r) {tmp[cnt++] = nums[j++];}for (int i = 0; i < r - l + 1; ++i) {nums[i + l] = tmp[i];}}void randomized_quicksort(vector<int>& nums, int l, int r) {if (l < r) {int j = rand() % (r - l + 1) + l;first=l,last=r;int i=l,x=nums[j];while(i<=last){if(nums[i]==x){i++;}else if(nums[i]<x){swap(nums[first++],nums[i++]);}else{swap(nums[last--],nums[i]);}}int left=first,right=last;randomized_quicksort(nums, l, left - 1);randomized_quicksort(nums, right + 1, r);}}
public:vector<int> sortArray(vector<int>& nums) {srand((unsigned)time(NULL));tmp.resize((int)nums.size(), 0);//mergeSort(nums, 0, (int)nums.size() - 1);randomized_quicksort(nums,0,(int)nums.size() - 1);return nums;}
};
7. 求数组第k个最大元素

. - 力扣(LeetCode)

static int first,last;
class Solution {
public:int findKthLargest(vector<int>& nums, int k) {int m=nums.size()-k;srand((unsigned)time(NULL));return test(nums,m);}int test(vector<int>& nums,int i){int ans = 0;for (int l = 0, r =nums.size() - 1; l <= r;) {partition(nums, l, r, nums[l + rand()%(r-l+1)]);if (i < first) {r = first - 1;} else if (i > last) {l = last + 1;} else {ans = nums[i];break;}}return ans;}void partition(vector<int>& nums, int l, int r, int x) {first = l;last = r;int i = l;while (i <= last) {if (nums[i] == x) {i++;} else if (nums[i] < x) {swap(nums[first++],nums[i++]);} else {swap(nums[last--],nums[i]);}}}
};

相关文章:

备赛蓝桥杯--算法题目(1)

1. 链表求和 . - 力扣&#xff08;LeetCode&#xff09; class Solution { public:ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {ListNode *head nullptr, *tail nullptr;int carry 0;while (l1 || l2) {int n1 l1 ? l1->val: 0;int n2 l2 ? l2->val:…...

机器学习100道经典面试题库(二)

机器学习100道经典面试题库&#xff08;31-60&#xff09; 在大规模的语料中&#xff0c;挖掘词的相关性是一个重要的问题。以下哪一个信息不能用于确定两个词的相关性。 A、互信息 B、最大熵 C、卡方检验 D、最大似然比 答案&#xff1a;B 解析&#xff1a;最大熵代表了…...

Unet++改进37:添加KACNConvNDLayer(2024最新改进方法)

本文内容:添加KACNConvNDLayer 目录 论文简介 1.步骤一 2.步骤二 3.步骤三 4.步骤四 论文简介 1.步骤一 新建block/kacn_conv.py文件,添加如下代码: import torch import torch.nn as nn##源码地址:https://github.com/SynodicMonth/ChebyKAN class KACNConvNDLaye…...

基于 Levenberg - Marquardt 法的 BP 网络学习改进算法详解

基于 Levenberg - Marquardt 法的 BP 网络学习改进算法详解 一、引言 BP&#xff08;Back Propagation&#xff09;神经网络在众多领域有着广泛应用&#xff0c;但传统 BP 算法存在收敛速度慢、易陷入局部最优等问题。Levenberg - Marquardt&#xff08;LM&#xff09;算法作…...

MySQL 8.0与PostgreSQL 15.8的性能对比

根据搜索结果&#xff0c;以下是MySQL 8.0与PostgreSQL 15.8的性能对比&#xff1a; MySQL 8.0性能特点&#xff1a; MySQL在处理大量读操作时表现出色&#xff0c;其存储引擎InnoDB提供了行级锁定和高效的事务处理&#xff0c;适用于并发读取的场景。MySQL通过查询缓存来提高读…...

qt连接postgres数据库时 setConnectOptions函数用法

连接选项&#xff0c;而这些选项没有直接的方法对应&#xff0c;你可能需要采用以下策略之一&#xff1a; 由于Qt SQL API的限制&#xff0c;你可能需要采用一些变通方法或查阅相关文档和社区资源以获取最新的信息和最佳实践。如果你确实需要设置特定的连接选项&#xff0c;并且…...

MySQL45讲 第二十七讲 主库故障应对:从库切换策略与 GTID 详解——阅读总结

文章目录 MySQL45讲 第二十七讲 主库故障应对&#xff1a;从库切换策略与 GTID 详解一、一主多从架构与主备切换的挑战&#xff08;一&#xff09;一主多从基本结构&#xff08;二&#xff09;主备切换的复杂性 二、基于位点的主备切换&#xff08;一&#xff09;同步位点的概念…...

JavaWeb笔记整理——Spring Task、WebSocket

目录 SpringTask ​cron表达式 WebSocket SpringTask cron表达式 WebSocket...

基于SpringBoot+RabbitMQ完成应⽤通信

前言&#xff1a; 经过上面俩章学习&#xff0c;我们已经知道Rabbit的使用方式RabbitMQ 七种工作模式介绍_rabbitmq 工作模式-CSDN博客 RabbitMQ的工作队列在Spring Boot中实现&#xff08;详解常⽤的⼯作模式&#xff09;-CSDN博客作为⼀个消息队列,RabbitMQ也可以⽤作应⽤程…...

Flutter踩坑记录(一)debug运行生成的项目,不能手动点击运行

问题 IOS14设备&#xff0c;切后台划掉&#xff0c;二次启动崩溃。 原因 IOS14以上 flutter 不支持debugger模式下的二次启动 。 要二次启动需要以release方式编译工程安装至手机。 操作步骤 清理项目&#xff1a;在命令行中运行flutter clean来清理之前的构建文件。重新构…...

React的hook✅

为什么hook必须在组件内的顶层声明&#xff1f; 这是为了确保每次组件渲染时&#xff0c;Hooks 的调用顺序保持一致。React利用 hook 的调用顺序来跟踪各个 hook 的状态。每当一个函数组件被渲染时&#xff0c;所有的 hook 调用都是按照从上到下的顺序依次执行的。React 内部会…...

2024.5 AAAiGLaM:通过邻域分区和生成子图编码对领域知识图谱对齐的大型语言模型进行微调

GLaM: Fine-Tuning Large Language Models for Domain Knowledge Graph Alignment via Neighborhood Partitioning and Generative Subgraph Encoding 问题 如何将特定领域知识图谱直接整合进大语言模型&#xff08;LLM&#xff09;的表示中&#xff0c;以提高其在图数据上自…...

从熟练Python到入门学习C++(record 6)

基础之基础之最后一节-结构体 1.结构体的定义 结构体相对于自定义的一种新的变量类型。 四种定义方式&#xff0c;推荐第一种&#xff1b;第四种适合大量定义&#xff0c;也适合查找&#xff1b; #include <iostream> using namespace std; #include <string.h>…...

jenkins的安装(War包安装)

‌Jenkins是一个开源的持续集成工具&#xff0c;基于Java开发&#xff0c;主要用于监控持续的软件版本发布和测试项目。‌ 它提供了一个开放易用的平台&#xff0c;使软件项目能够实现持续集成。Jenkins的功能包括持续的软件版本发布和测试项目&#xff0c;以及监控外部调用执行…...

WPS 加载项开发说明wpsjs

wpsjs几个常用的CMD命令&#xff1a; 1.打开cmd输入命令测试版本号 npm -v 2.首次安装nodejs&#xff0c;npm默认国外镜像&#xff0c;包下载较慢时&#xff0c;可切换到国内镜像 //下载速度较慢时可切换国内镜像 npm config set registry https://registry.npmmirror.com …...

【Anomaly Detection论文阅读记录】PaDiM与PatchCore模型的区别与联系

PaDiM与PatchCore模型的区别与联系 背景介绍 PADIM(Pretrained Anomaly Detection via Image Matching)和 PatchCore 都是基于深度学习的异常检测方法,主要用于图像异常检测,尤其是在无监督学习设置下。 PADIM 是一种通过利用预训练的视觉模型(例如,ImageNet预训练的卷…...

uni-app Vue3语法实现微信小程序样式穿透uview-plus框架

1 问题描述 我在用 uni-app vue3 语法开发微信小程序时&#xff0c;在项目中使用了 uview-plus 这一开源 UI 框架。在使用 up-text 组件时&#xff0c;想要给它添加一些样式&#xff0c;之前了解到微信小程序存在样式隔离的问题&#xff0c;也在uview-plus官网-注意事项中找到…...

K8S基础概念和环境搭建

K8S的基础概念 1. 什么是K8S K8S的全称是Kubernetes K8S是一个开源的容器编排平台&#xff0c;用于自动化部署、扩缩、管理容器化应用程序。 2. 集群和节点 集群&#xff1a;K8S将多个机器统筹和管理起来&#xff0c;彼此保持通讯&#xff0c;这样的关系称之为集群。 节点…...

[服务器] 腾讯云服务器免费体验,成功部署网站

文章目录 概要整体架构流程概要 腾讯云服务器免费体验一个月。 整体架构流程 腾讯云服务器体验一个月, 选择预装 CentOS 7.5 首要最重要的是: 添加阿里云镜像。 不然国外源速度慢, 且容易失败。 yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/li…...

vue中el-select 模糊查询下拉两种方式

第一种&#xff1a;先获取所有下拉数据再模糊查询&#xff0c;效果如下 1&#xff0c;页面代码&#xff1a;speciesList是种类列表List, speciesId 是speciesList里面对应的id&#xff0c;filterable是过滤查询标签 <el-form-item label"种类" prop"species…...

内存分配函数malloc kmalloc vmalloc

内存分配函数malloc kmalloc vmalloc malloc实现步骤: 1)请求大小调整:首先,malloc 需要调整用户请求的大小,以适应内部数据结构(例如,可能需要存储额外的元数据)。通常,这包括对齐调整,确保分配的内存地址满足特定硬件要求(如对齐到8字节或16字节边界)。 2)空闲…...

PPT|230页| 制造集团企业供应链端到端的数字化解决方案:从需求到结算的全链路业务闭环构建

制造业采购供应链管理是企业运营的核心环节&#xff0c;供应链协同管理在供应链上下游企业之间建立紧密的合作关系&#xff0c;通过信息共享、资源整合、业务协同等方式&#xff0c;实现供应链的全面管理和优化&#xff0c;提高供应链的效率和透明度&#xff0c;降低供应链的成…...

Linux相关概念和易错知识点(42)(TCP的连接管理、可靠性、面临复杂网络的处理)

目录 1.TCP的连接管理机制&#xff08;1&#xff09;三次握手①握手过程②对握手过程的理解 &#xff08;2&#xff09;四次挥手&#xff08;3&#xff09;握手和挥手的触发&#xff08;4&#xff09;状态切换①挥手过程中状态的切换②握手过程中状态的切换 2.TCP的可靠性&…...

C# 类和继承(抽象类)

抽象类 抽象类是指设计为被继承的类。抽象类只能被用作其他类的基类。 不能创建抽象类的实例。抽象类使用abstract修饰符声明。 抽象类可以包含抽象成员或普通的非抽象成员。抽象类的成员可以是抽象成员和普通带 实现的成员的任意组合。抽象类自己可以派生自另一个抽象类。例…...

【RockeMQ】第2节|RocketMQ快速实战以及核⼼概念详解(二)

升级Dledger高可用集群 一、主从架构的不足与Dledger的定位 主从架构缺陷 数据备份依赖Slave节点&#xff0c;但无自动故障转移能力&#xff0c;Master宕机后需人工切换&#xff0c;期间消息可能无法读取。Slave仅存储数据&#xff0c;无法主动升级为Master响应请求&#xff…...

Spring AI与Spring Modulith核心技术解析

Spring AI核心架构解析 Spring AI&#xff08;https://spring.io/projects/spring-ai&#xff09;作为Spring生态中的AI集成框架&#xff0c;其核心设计理念是通过模块化架构降低AI应用的开发复杂度。与Python生态中的LangChain/LlamaIndex等工具类似&#xff0c;但特别为多语…...

初学 pytest 记录

安装 pip install pytest用例可以是函数也可以是类中的方法 def test_func():print()class TestAdd: # def __init__(self): 在 pytest 中不可以使用__init__方法 # self.cc 12345 pytest.mark.api def test_str(self):res add(1, 2)assert res 12def test_int(self):r…...

React---day11

14.4 react-redux第三方库 提供connect、thunk之类的函数 以获取一个banner数据为例子 store&#xff1a; 我们在使用异步的时候理应是要使用中间件的&#xff0c;但是configureStore 已经自动集成了 redux-thunk&#xff0c;注意action里面要返回函数 import { configureS…...

HarmonyOS运动开发:如何用mpchart绘制运动配速图表

##鸿蒙核心技术##运动开发##Sensor Service Kit&#xff08;传感器服务&#xff09;# 前言 在运动类应用中&#xff0c;运动数据的可视化是提升用户体验的重要环节。通过直观的图表展示运动过程中的关键数据&#xff0c;如配速、距离、卡路里消耗等&#xff0c;用户可以更清晰…...

论文笔记——相干体技术在裂缝预测中的应用研究

目录 相关地震知识补充地震数据的认识地震几何属性 相干体算法定义基本原理第一代相干体技术&#xff1a;基于互相关的相干体技术&#xff08;Correlation&#xff09;第二代相干体技术&#xff1a;基于相似的相干体技术&#xff08;Semblance&#xff09;基于多道相似的相干体…...