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

栏目: 编程工具 · 发布时间: 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);


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

查看所有标签

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

Software Engineering for Internet Applications

Software Engineering for Internet Applications

Eve Andersson、Philip Greenspun、Andrew Grumet / The MIT Press / 2006-03-06 / USD 35.00

After completing this self-contained course on server-based Internet applications software, students who start with only the knowledge of how to write and debug a computer program will have learned ho......一起来看看 《Software Engineering for Internet Applications》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

SHA 加密
SHA 加密

SHA 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器