字符串之判断重复字符串
1.去掉重复字符串时间复杂度为O(n)
#include <iostream>
using namespace std;
int main()
{
char str[] = "bdsjiseftftftfyrzsesese";
int length = strlen(str);
char *p = str;
char hashTable[256] = {0};
for (int i = 0;i <length;++i)
{
if (hashTable[str[i]]==0)
{
*p = str[i];
hashTable[*p] =1;
p++;
}
}
*p ='\0';
cout << str <<endl;
return 0;
}
2.第一次只出现一次的字符
#include <iostream>
using namespace std;
int main()
{
char *str = "edsjiseftftftfyrzsesese";
char *p = str;
char hashTable[256] = {0};
while(*p)
{
hashTable[*p]++;
p++;
}
p = str;
while(*p)
{
if(hashTable[*p] == 1)
{
break;
}
p++;
}
cout << *p <<endl;
return 0;
}
3.删除出现次数最少的字符
有三个关键字 次数 最少 删除
需要遍历三次:
void reverse(char *str)
{
char hashtable[26] = {0};
char *p = str;
while (*p != '\0')
{
hashtable[*p -'a']++;
p++;
}
p = str;
int min = hashtable[*p -'a'];
while(*p != '\0')
{
if (hashtable[*p - 'a'] != 0)
{
if(hashtable[*p - 'a'] < min)
{
min = hashtable[*p -'a'];
}
}
p++;
}
p = str;
for (int i = 0 ;i < strlen(str);i++)
{
if (hashtable[str[i] - 'a'] != min)
{
*p = str[i];
p++;
}
}
}
一切源于对计算机的热爱

浙公网安备 33010602011771号