remove the duplicate(重复) characters from a string
来源:互联网 发布:mac os 10.10下载地址 编辑:程序博客网 时间:2024/05/17 13:14
/*Design an algorithm and write code to remove the duplicate(重复) characters in a string without using any additional buffer. NOTE: One or two additional variablesare fine.An extra copy of the array is not */#include <iostream>#include <string>using namespace std;void removeDuplicates(string &str){int len = str.length();if(len < 2)return;//new_end为无重复元素数组末元素下一位置的下标int new_end = 1;for (int i = 1;i < len;++i){int j;//判断str[0...new_end-1]和str[i...len-1]是否有相等元素for (j = 0;j < new_end;++j){//str[i]与str[0...new_end-1]中元素相等,则跳出for循环,接着判断str[i+1]if (str[j] == str[i])break;//str[i]不与str[0...new_end-1]中元素相等,则令str[new_end]等于str[i]}if(j == new_end){str[new_end] = str[i];++new_end;}}str[new_end] = '\0';}void print_c_str(string str) { for (int i = 0;i < str.length();++i){if(str.at(i) == '\0')break;cout << str[i];}cout << endl;}int main(){string s1 = "";string s2 = "aaaa";string s3 = "aaaabbbb";string s4 = "abababab";removeDuplicates(s1);removeDuplicates(s2);removeDuplicates(s3);removeDuplicates(s4);print_c_str(s1);print_c_str(s2);print_c_str(s3);print_c_str(s4);return 0;}
- remove the duplicate(重复) characters from a string
- 【3】Remove the duplicate characters in a string
- [CrackCode] 1.3 Remove the duplicate characters in a string
- How do you remove the duplicate characters in a given string without using any additional buffer.
- How to remove space or newline characters from a string?
- remove the duplicate node from unsorted list
- How to remove a block from the particular page?
- P20 (*) Remove the K'th element from a list.
- php 怎样去掉一个string 后面的一个或多个字符(how to remove the last n chars from a string)?
- leetcode之 Remove Duplicate from Sorted Array
- remove duplicate from sort array 2
- LeetCode 26 remove Duplicate from sorted array
- C# Tips: How to remove white-space characters inside a string?
- CH1-3: remove duplicate char in a string, with/without additional buffer
- How to remove the duplicated value in a String array
- Type string trivially inferred from a string literal, remove type annotation (no-inferrable-types)
- Unique Characters of a String
- //Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the in
- 考试评分系统 数据库表 备份一下
- UVaOJ548---Tree
- 函数指针
- 《学习OpenCV》练习题第三章第三题
- 苏一辰之8皇后问题
- remove the duplicate(重复) characters from a string
- 配置网络文件系统NFS服务 及其自启动
- UVaOJ297---Quadtrees
- ruby on rails demo 样例
- ios 用xcode4.2开发 访问web service的功能
- MFC程序启动过程
- UVaOJ712---S-Trees
- 《学习OpenCV》练习题第三章第四题
- 今天起,正式开始编程之路