内容简介:设计一个算法移除字符串中的重复字符,并写出测试用例。解决思想: 1:对于每个字符,检查在一发现字符集合中是否已经存在。2:若存在,则跳过,否则加入到已发现字符集合中。
设计一个算法移除字符串中的重复字符,并写出测试用例。
解决思想: 1:对于每个字符,检查在一发现字符集合中是否已经存在。
2:若存在,则跳过,否则加入到已发现字符集合中。
1 #include<iostream>
2
3 void removeDuplicates(char *str)
4 {
5 if(str==NULL)
6 return;
7 int len=strlen(str);
8 if(len<2)
9 return;
10 int tail=1;
11 for(int i=1;i<len;++i)
12 {
13 int j;
14 for(j=0;j<tail;++j)
15 {
16 if(str[i]==str[j])
17 break;
18 }
19 if(j==tail)
20 {
21 str[tail]=str[i];
22 ++tail;
23 }
24 }
25 str[tail]=0;
26 printf("%s\n",str);
27 }
28
29 int main()
30 {
31 char mystr[]="abababa";
32 removeDuplicates(mystr);
33 return 0;
34 }
测试用例:1:不含有重复字符的字符串,如“abcd”;
2: 含单一字符的字符串,如“aaaa”;
3: 空字符与空指针,如“”与NULL;
4:多个连续的字符串,如“aaabbb”;
5: 不连续重复的字符串,如“abababa”;
时间复杂度为O(n2);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 查找一个字符串中最长不含重复字符的子字符串,计算该最长子字符串的长度
- 字符串、字符处理总结
- 高频算法面试题(字符串)leetcode 387. 字符串中的第一个唯一字符
- php删除字符串最后一个字符
- (三)C语言之字符串与字符串函数
- 算法笔记字符串处理问题H:编排字符串(2064)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Design and Analysis of Distributed Algorithms (Wiley Series on P
Nicola Santoro / Wiley-Interscience / 2006-10-27 / USD 140.95
This text is based on a simple and fully reactive computational model that allows for intuitive comprehension and logical designs. The principles and techniques presented can be applied to any distrib......一起来看看 《Design and Analysis of Distributed Algorithms (Wiley Series on P》 这本书的介绍吧!