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;
}
相关推荐
hnjzsyjyj3 小时前
东方博宜OJ 2190:树的重心 ← 邻接表 or 链式前向星
数据结构·链式前向星·树的重心
yaoh.wang5 小时前
力扣(LeetCode) 13: 罗马数字转整数 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·跳槽
ChoSeitaku5 小时前
NO15数据结构选择题考点|线性表|栈和队列|串
数据结构
T1ssy5 小时前
布隆过滤器:用概率换空间的奇妙数据结构
算法·哈希算法
hetao17338376 小时前
2025-12-12~14 hetao1733837的刷题笔记
数据结构·c++·笔记·算法
椰子今天很可爱6 小时前
五种I/O模型与多路转接
linux·c语言·c++
一直都在5726 小时前
数据结构入门:时间复杂度与排序和查找
数据结构
程序员zgh7 小时前
C++ 互斥锁、读写锁、原子操作、条件变量
c语言·开发语言·jvm·c++
鲨莎分不晴7 小时前
强化学习第五课 —— A2C & A3C:并行化是如何杀死经验回放
网络·算法·机器学习
搞科研的小刘选手8 小时前
【ISSN/ISBN双刊号】第三届电力电子与人工智能国际学术会议(PEAI 2026)
图像处理·人工智能·算法·电力电子·学术会议