c++字符串类题目

    xiaoxiao2022-07-06  188

    感觉如果每次都单篇发的话,会很臃余,所以就打算每次进行修改然后放在一篇文章里了

    1、给定字符串,去掉所有重复,如abcda返回abcd,或bcdeeeee,返回bcde

    代码求解(最优时间复杂度求解):

    #include <iostream> using namespace std; //删除字符串中所有重复的字符,例如google,删除重复字符后为gole char * deleteRepeatedChars(char *s) { if (s == NULL) return s; char *fast = s; char *slow = s; //char为一个字节,8位即能代表0-255 //使用哈希表思想,将字符的acsⅡ码作为下标,值为bool值(0或1)来代表是否重复了 bool arr[256] = { 0 }; while (*fast != '\0') { //当不重复的时候,记录下slow if (arr[*fast] == 0) { arr[*fast]=1; *slow = *fast; slow++; } fast++; } *slow = '\0';//关键一步,截止字符串s return s; } int main() { char aa[7] = { "sdsass" }; char *s = aa; cout<< deleteRepeatedChars(s)<<endl; }
    最新回复(0)