双指针问题5(c++)

双指针问题5(c++)

概念

双指针,顾名思义,就是用两个指针解决问题

有些问题用单指针会出现超时等问题,这时就需要用到双指针

双指针由两个指针组成,一般是左右指针,或前后指针

通过两个指针配合变化,用更短的时间高效解决问题

题目

(续上一篇,如需了解上一篇题目,请移步主页观看)

合并有序数组

#include <bits/stdc++.h> #define ll long long using namespace std; int la,lb,lab; int a[20010],b[10010]; int main() { cin>>la; for(int i = 1;i<=la;i++) { cin>>a[i]; } cin>>lb; for(int i = 1;i<=lb;i++) { cin>>b[i]; } lab = la+lb; int ai = la+1,bi = lb+1,abi = lab+1; while(ai>1&&bi>1) { if(a[ai-1]>b[bi-1]) a[--abi] = a[--ai]; else a[--abi] = b[--bi]; } while(bi>1) a[--abi] = b[--bi]; for(int i = 1;i<=lab;i++) cout<<a[i]<<" "; return 0; }

完美数列

#include <bits/stdc++.h> #define ll long long using namespace std; int n,p; int a[10010]; int main() { cin>>n>>p; for(int i = 1;i<=n;i++) { cin>>a[i]; } sort(a+1,a+n+1); int fast = 0,slow = 1; int ma,mi; mi = a[slow]; int mama = -999999999; while(fast<n) { while(fast<n) { ma = a[++fast]; int x = mi*p; if(ma<=x) mama = max(mama,fast-slow+1); else break; } while(slow<fast) { mi = a[++slow]; int x = mi*p; if(ma<=x) { mama = max(mama,fast-slow+1); break; } } } cout<<mama; return 0; }

反转元音字母

#include <bits/stdc++.h> #define ll long long using namespace std; string a; char yy[] = {'a','e','i','o','u'}; int main() { cin>>a; int n = a.size(); int l = 0,r = n-1; while(l<r) { bool lf = false; bool rf = false; for(int i = 0;i<5;i++) { if(a[l]==yy[i]) lf = true; if(a[r]==yy[i]) rf = true; } if(lf==false) l++; if(rf==false) r--; if(lf==true&&rf==true) { swap(a[l],a[r]); l++; r--; } } cout<<a; return 0; }

验证回文串2

#include <bits/stdc++.h> #define ll long long using namespace std; string a; int n; bool chushi(); bool hou(int,int); int main() { cin>>a; n = a.size(); if(chushi()==true) cout<<"true"; else cout<<"false"; return 0; } bool chushi() { int l = 0,r = n-1; while(l<r) { if(a[l]==a[r]) l++,r--; else { if((hou(l+1,r)|hou(l,r-1))==true) return true; else return false; } } return true; } bool hou(int l,int r) { while(l<r) { if(a[l]==a[r]) l++,r--; else return false; } return true; }

最接近的三数之和

#include <bits/stdc++.h> #define ll long long using namespace std; int n,a[10010],target,misum,micha; int main() { micha = 999999999; cin>>n; for(int i = 1;i<=n;i++) { cin>>a[i]; } cin>>target; sort(a+1,a+n+1); for(int i = 1;i<=n-2;i++) { int l = i+1,r = n; while(l<r) { int sum = a[l]+a[i]+a[r]; int cha = sum-target; if(cha<0) cha = target-sum,l++; else r--; if(cha<micha) { micha = cha; misum = sum; } } } cout<<misum; return 0; }

三数之和的多种可能

#include <bits/stdc++.h> #define ll long long using namespace std; int n,a[10010],target; int cnt; int main() { cin>>n; for(int i = 1;i<=n;i++) { cin>>a[i]; } cin>>target; sort(a+1,a+n+1); for(int i = 1;i<=n-2;i++) { int l = i+1; int r = n; while(l<r) { int sum = a[i]+a[l]+a[r]; if(sum==target) { cnt++; // cout<<cnt<<" "<<i<<" "<<l<<" "<<r<<" "<<a[i]<<" "<<a[l]<<" "<<a[r]<<endl; int p = r-1; while(a[p]==a[r]&&p>l) { cnt++; p--; // cout<<cnt<<" "<<i<<" "<<l<<" "<<p<<" "<<a[i]<<" "<<a[l]<<" "<<a[p]<<endl; } l++; } if(sum>target) r--; if(sum<target) l++; } } cout<<cnt; return 0; }

Read more

02-mcp-server案例分享-Excel 表格秒变可视化图表 HTML 报告,就这么简单

02-mcp-server案例分享-Excel 表格秒变可视化图表 HTML 报告,就这么简单

1.前言 MCP Server(模型上下文协议服务器)是一种基于模型上下文协议(Model Context Protocol,简称MCP)构建的轻量级服务程序,旨在实现大型语言模型(LLM)与外部资源之间的高效、安全连接。MCP协议由Anthropic公司于2024年11月开源,其核心目标是解决AI应用中数据分散、接口不统一等问题,为开发者提供标准化的接口,使AI模型能够灵活访问本地资源和远程服务,从而提升AI助手的响应质量和工作效率。 MCP Server 的架构与工作原理 MCP Server 采用客户端-服务器(Client-Server)架构,其中客户端(MCP Client)负责与服务器建立连接,发起请求,而服务器端则处理请求并返回响应。这种架构确保了数据交互的高效性与安全性。例如,客户端可以向服务器发送请求,如“查询数据库中的某个记录”或“调用某个API”,而服务器则根据请求类型,调用相应的资源或工具,完成任务并返回结果。 MCP Server 支持动态发现和实时更新机制。例如,当新的资源或工具被添加到服务器时,

By Ne0inhk
将现有 REST API 转换为 MCP Server工具 -higress

将现有 REST API 转换为 MCP Server工具 -higress

Higress 是一款云原生 API 网关,集成了流量网关、微服务网关、安全网关和 AI 网关的功能。 它基于 Istio 和 Envoy 开发,支持使用 Go/Rust/JS 等语言编写 Wasm 插件。 提供了数十个通用插件和开箱即用的控制台。 Higress AI 网关支持多种 AI 服务提供商,如 OpenAI、DeepSeek、通义千问等,并具备令牌限流、消费者鉴权、WAF 防护、语义缓存等功能。 MCP Server 插件配置 higress 功能说明 * mcp-server 插件基于 Model Context Protocol (MCP),专为 AI 助手设计,

By Ne0inhk
MCP 工具速成:npx vs. uvx 全流程安装指南

MCP 工具速成:npx vs. uvx 全流程安装指南

在现代 AI 开发中,Model Context Protocol(MCP)允许通过外部进程扩展模型能力,而 npx(Node.js 生态)和 uvx(Python 生态)则是两种即装即用的客户端工具,帮助你快速下载并运行 MCP 服务器或工具包,无需全局安装。本文将从原理和对比入手,提供面向 Windows、macOS、Linux 的详细安装、验证及使用示例,确保你能在本地或 CI/CD 流程中无缝集成 MCP 服务器。 1. 工具简介 1.1 npx(Node.js/npm) npx 是 npm CLI(≥v5.2.0)

By Ne0inhk
如何在Cursor中使用MCP服务

如何在Cursor中使用MCP服务

前言 随着AI编程助手的普及,越来越多开发者选择在Cursor等智能IDE中进行高效开发。Cursor不仅支持代码补全、智能搜索,还能通过MCP(Multi-Cloud Platform)服务,轻松调用如高德地图API、数据库等多种外部服务,实现数据采集、处理和自动化办公。 本文以“北京一日游自动化攻略”为例,详细讲解如何在 Cursor 中使用 MCP 服务,完成数据采集、数据库操作、文件生成和前端页面展示的全流程。 学习视频:cursor中使用MCP服务 一、什么是MCP服务? MCP(Multi-Cloud Platform)是Cursor内置的多云服务接口,支持调用地图、数据库、文件系统等多种API。通过MCP,开发者无需手动写HTTP请求或繁琐配置,只需在对话中描述需求,AI助手即可自动调用相关服务,极大提升开发效率。 二、环境准备 2.1 cursor Cursor重置机器码-解决Too many free trials. 2.

By Ne0inhk