去重算法里面,将所有重复字符删除的算法有bug

jambla 字符串的高级处理 2017年07月30日提问

  • 0 回答
  • 364 浏览

输入AAABCDDDEEF输出不对

/**
 * 删除所有重复字符(已排序)
 * @param string
 */
void removeAllRepeat(char *string) {
    if (string == nullptr) {
        return;
    }
    int index = 0;
    int pos = 1;
    while (string[pos] != '') {
        if (string[pos] != string[index]) {
            int temp = string[pos];
            int middleStart = pos;
            int middleEnd = pos   1;
            while (string[middleEnd] != '') {
                if (string[middleStart] != string[middleEnd]) {
                    break;
                }
                middleEnd  ;
            }
            if (middleEnd - middleStart == 1) {
                string[index  ] = string[middleStart];
                pos = middleStart;
            } else {
                pos = middleEnd - 1;
            }
        }
        pos  ;
    }
    string[index] = '';
}