CH1-3: remove duplicate char in a string, with/without additional buffer
2013-12-29 05:59
555 查看
感谢Hawstein的答案,然后再整理一边,make sure自己完全理解了。
所有代码在http://www.compileonline.com/compile_cpp11_online.php 上面测试,方便快捷,同时在本地和csdn git上面备份。
1. 记录各个字符出现情况的bool型scoreboard可以保存下标大于size的吗?
其实没关系,因为ASCII的decimal范围是0~127(其实还有128个,那些是extended ASCII,所以需要8位来保存全部的ascii码)。所以所有值都在0~255;
eg:
但是就算是定义了一个size为1的array:a[1],也可以定义a[300],a[301],a[302]...不知道为什么。。。
2种情况的T(n)会不一样:
#include
#include
using namespace std;
void a0(char* s){
int len = strlen(s);
int p = 0;
if (len < 2) return;
for (int i = 0; i
1. 如果不可以用另外的array的话,就要In place 的修改,也就是见到一个char就和他后面的所有字符比较一遍,当发现duplicate的情况,把那个字符置为一个flag的值,好让后面s[p++]=s[i]的时候的判定是根据(s[i]!=flag). Hawstein的flag是‘\0’,这样就不用想一个不是ASCII的字符了。不过这样就不会复制最后一个真正的结束符号,所以在循环完之后要s[p] = '\0'. 记得K&R的解答也用了这个方法。
当然,用这个方法的话,T(n)=n+(n-1)+(n-2)+...+1 = O(n^2);
2. 如果可以用additional buffer,则只需要1次循环,而modify一遍scoreboard需要O(n)的时间复杂度。
在这里的2个method都是针对输入是char型字符串:char ss1[] = "abcde";
而如果是string的话,就要用string的method,修改的话 需要用str+=,而不能s[p++].
所有代码在http://www.compileonline.com/compile_cpp11_online.php 上面测试,方便快捷,同时在本地和csdn git上面备份。
1. 记录各个字符出现情况的bool型scoreboard可以保存下标大于size的吗?
其实没关系,因为ASCII的decimal范围是0~127(其实还有128个,那些是extended ASCII,所以需要8位来保存全部的ascii码)。所以所有值都在0~255;
eg:
bool a[256]; //cuz it stored the ASCII memset(a,0,sizeof(a)); void block(char* s){ const int* curptr = s; for (int i = 0; i<len; ++i){ int v = *curptr; curptr++; a[v] = true; // eg: A的ASCII值是65,a的是97 } }
但是就算是定义了一个size为1的array:a[1],也可以定义a[300],a[301],a[302]...不知道为什么。。。
#include #include using namespace std; double atest(double a){ cout << a << endl; a = a+3.2; cout << a << endl; } void st(char *s){ int l = strlen(s); cout << "\nl::" << l << endl; } //http://www.cplusplus.com/doc/tutorial/pointers/ void sj(char *s){ int len = strlen(s); const char *current = s; for (int i=0; i
2种情况的T(n)会不一样:
#include
#include
using namespace std;
void a0(char* s){
int len = strlen(s);
int p = 0;
if (len < 2) return;
for (int i = 0; i
1. 如果不可以用另外的array的话,就要In place 的修改,也就是见到一个char就和他后面的所有字符比较一遍,当发现duplicate的情况,把那个字符置为一个flag的值,好让后面s[p++]=s[i]的时候的判定是根据(s[i]!=flag). Hawstein的flag是‘\0’,这样就不用想一个不是ASCII的字符了。不过这样就不会复制最后一个真正的结束符号,所以在循环完之后要s[p] = '\0'. 记得K&R的解答也用了这个方法。
当然,用这个方法的话,T(n)=n+(n-1)+(n-2)+...+1 = O(n^2);
2. 如果可以用additional buffer,则只需要1次循环,而modify一遍scoreboard需要O(n)的时间复杂度。
在这里的2个method都是针对输入是char型字符串:char ss1[] = "abcde";
而如果是string的话,就要用string的method,修改的话 需要用str+=,而不能s[p++].
相关文章推荐
- How do you remove the duplicate characters in a given string without using any additional buffer.
- Ch2-1: de-duplicate a linked-list with/without additional buffer
- Ch1-5: Write a method to replace all spaces in a string with ‘%20’.
- 【3】Remove the duplicate characters in a string
- [CrackCode] 1.3 Remove the duplicate characters in a string
- Search char in an exat string with pointer
- char * strtrim(char* s ) to remove any space character(such as '\t' , ' ' ...) in a string
- Powershell: Read in file without converting into string array
- Remove duplicate in sorted linked list 1
- How to convert char array to String in Java
- remove duplicated chars in a string
- error C2664: “ATL::CStringT<BaseType,StringTraits>::Remove”: 不能将参数 1 从“const char [2]”转换为“char”
- StringBuffer is dangerous when dealing with lonnnng Strings!!!
- Avoiding Buffer Overruns with String Safety
- 【LeetCode with Python】 Reverse Words in a String
- Android: setPreviewCallbackWithBuffer, buffer in the queue and no callbacks
- Problems with System.OutOfMemoryException At System.String.GetStringForStringBuilder in 32-Bit Managed Solutions
- Remove duplicates in a sorted array
- Remove duplicate elements in an array
- (pbc lua 加入)c++_lua_Python with/without extension性能测试 (10万次SerializeToString & ParseFromString)