设计一个算法移除字符串中的重复字符,并写出测试用例。

栏目: 编程工具 · 发布时间: 7年前

内容简介:设计一个算法移除字符串中的重复字符,并写出测试用例。解决思想: 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);


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Design and Analysis of Distributed Algorithms (Wiley Series on P

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》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

html转js在线工具
html转js在线工具

html转js在线工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试