C++双指针算法例题

有序数组去重

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
long long a[10000010];
int n,s=0;
int main()
{
	
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
		
	}
	for(int f=0;f<n;f++){
		if(a[s]!=a[f]){
			a[++s]=a[f];
		}
	}
	s++;
	for(int i=0;i<s;i++){
		cout<<a[i];
	}
	return 0;
}

移除元素

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
long long a[10000010];
int n,s=0,k;
int main()
{
	
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
		
	}
	cin>>k;
	for(int f=0;f<n;f++){
		if(k!=a[f]){
			a[s++]=a[f];
		}
	}
	for(int i=0;i<s;i++){
		cout<<a[i];
	}
	cout<<endl;
	return 0;
}

无重复字符最长字串

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
int cnt[130]={0};
bool ifcf();
int main()
{
	string a;
	cin>>a;
	int n=a.size();
	int ma=-1;
	string mas="";
	int s=0,f=0;
	while(true){
		while(true){
			cnt[a[f]]++;
			
			if(cnt[a[f]]>1) break;
			else ma=max(ma,f-s+1);
			f++;
		}
		while(true){
			cnt[a[s]]--;
			s++;
			if(ifcf()==true) break;
		}
	}
	
	return 0;
}
bool ifcf(){
	for(int i=97;i<122;i++){
		if(cnt[i]>1) return false;
	}
	return true;
}
相关推荐
rannn_1117 小时前
【力扣hot100】链表专题|160、206、234、141、142
java·算法·leetcode·链表·面试·开发
数模竞赛Paid answer7 小时前
2026年华东杯数学建模B题医药物流安排问题解题全过程文档及程序
算法·数学建模·数据分析·华东杯
不会代码的小猴8 小时前
21. 泛型编程上
开发语言·c++·笔记·算法
青瓦梦滋8 小时前
传输层UDP/TCP协议
linux·网络·c++·网络协议·tcp/ip·udp
AI备案指南-满满9 小时前
大模型与算法备案全流程详解:从零到通过的完整指南
人工智能·算法·备案·大模型备案·算法备案
江畔柳前堤9 小时前
LLM 训练核心机制深度解析:Warmup、Cosine Decay 与 Perplexity 的完整知识体系
网络·人工智能·深度学习·算法·机器学习·语音识别
一只旭宝9 小时前
细讲C加加【9】C++ std::function与std::bind详解|仿函数、绑定器、类成员绑定、占位符、成员偏移指针
开发语言·c++·算法
良木林9 小时前
子串 - LeetCode hot 100
算法·leetcode·职场和发展
陌诺曦.10 小时前
Python扩展小练习
算法