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;
}
相关推荐
hcnaisd23 小时前
深入理解C++内存模型
开发语言·c++·算法
李老师讲编程4 小时前
C++信息学奥赛练习题-杨辉三角
数据结构·c++·算法·青少年编程·信息学奥赛
zxsz_com_cn4 小时前
设备预测性维护算法核心功能有哪些?六大模块拆解智能运维的“技术骨架”
运维·算法
期末考复习中,蓝桥杯都没时间学了4 小时前
力扣刷题13
数据结构·算法·leetcode
qq_296544654 小时前
短视频下载教程,抖音B站视频下载
c++
2201_756989094 小时前
C++中的事件驱动编程
开发语言·c++·算法
多米Domi0114 小时前
0x3f 第48天 面向实习的八股背诵第五天 + 堆一题 背了JUC的题,java.util.Concurrency
开发语言·数据结构·python·算法·leetcode·面试
2301_822377654 小时前
模板元编程调试方法
开发语言·c++·算法
啟明起鸣4 小时前
【C++ 性能提升技巧】C++ 的引用、值类型、构造函数、移动语义与 noexcept 特性,可扩容的容器
开发语言·c++
故以往之不谏4 小时前
函数--值传递
开发语言·数据结构·c++·算法·学习方法