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;
}
相关推荐
羑悻的小杀马特7 小时前
ProtoBuf语法揭秘:探秘编译魔法与性能优化策略,解锁多层级选项配置的底层奥秘
c++·编程·protobuf
fpcc7 小时前
C++编程实践——eventFD
linux·c++
仟濹7 小时前
「经典数字题」集合 | C/C++
c语言·开发语言·c++
Skrrapper7 小时前
【STL】set、multiset、unordered_set、unordered_multiset 的区别
c++·算法·哈希算法
SunnyKriSmile7 小时前
函数递归求最大值
c语言·算法·函数递归
傻啦嘿哟8 小时前
爬虫数据去重:BloomFilter算法实现指南
爬虫·算法
立志成为大牛的小牛8 小时前
数据结构——三十六、拓扑排序(王道408)
数据结构·学习·程序人生·考研·算法
冷崖8 小时前
QML-Model-View
javascript·c++
懒羊羊不懒@8 小时前
JavaSe—List集合系列
java·开发语言·数据结构·人工智能·windows