经典算法面试题目-设计算法移除字符串中重复的字符(1.3)

题目

Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional variables are fine. An extra copy of the array is not.

FOLLOW UP

Write the test cases for this method.

设计算法并写出代码移除字符串中重复的字符,不能使用额外的缓存空间。注意: 可以使用额外的一个或两个变量,但不允许额外再开一个数组拷贝。

进一步地,

为你的程序写测试用例。

解答

这道题目其实是要你就地(in place)将字符串中重复字符移除。你可以向面试官问清楚, 不能使用额外的一份数组拷贝是指根本就不允许开一个数组,还是说可以开一个固定大小, 与问题规模(即字符串长度)无关的数组。

如果根本就不允许你再开一个数组,只能用额外的一到两个变量。那么,你可以依次访问 这个数组的每个元素,每访问一个,就将该元素到字符串结尾的元素中相同的元素去掉( 比如置为’\0′).时间复杂度为O(n2 ),代码如下:

void removeDuplicate(char s[])
{
    int len = strlen(s);
    if(len < 2) return;//只有一个字符,肯定没有重复的
    int p = 0;//相当于一个游标
    //从0开始找不重复的字符
    for(int i=0; i < len; ++i)
    {
        if(s[i] != '\0')
        {
            s[p++] = s[i];
            for(int j=i+1; j < len; ++j)
                if(s[j]==s[i])
                    s[j] = '\0';
        }
    }
    s[p] = '\0';
}

如果可以开一个固定大小,与问题规模(即字符串长度)无关的数组,那么可以用一个数组来 表征每个字符的出现(假设是ASCII字符,则数组大小为256),这样的话只需要遍历一遍字符 串即可,时间复杂度O(n)。代码如下:

void removeDuplicate(char s[])
{
    int len = strlen(s);
    if(len < 2) return;
    bool c[256];//这个用true来标识这个字符出现过了。相比上个节省了时间。
    memset(c, 0, sizeof(c));
    int p = 0;
    for(int i=0; i < len; ++i)
    {
        if(!c[s[i]])
        {
            s[p++] = s[i];
            c[s[i]] = true;
        }
    }
    s[p] = '\0';
}

如果字符集更小一些,比如只是a-z,即字符串里只包含小写字母,那么使用一个int变量中 的每一位来表征每个字符的出现,一样可以在O(n)的时间里移除重复字符,而且还不需要额 外开一个数组。代码如下:

void removeDuplicate(char s[])
{
    int len = strlen(s);
    if(len < 2) return;
    int check = 0, p = 0;
    for(int i=0; i < len; ++i)
    {
        //int变量本身在内存中占4字节也就是32位!!
        int v = (int)(s[i]-'a');
        //如果没有出现重复的字母(字母种数小于32种),就不会出现(check & (1 << v)==1的情况!
        if((check & (1 << v))==0)
        {
            s[p++] = s[i];
            check |= (1 << v);
        }
    }
    s[p] = '\0';
}

测试用例:

不包含重复字符的字符串,比如:abcd
字符串全是重复字符,比如:aaaa
空字符串
重复字符连续出现,比如:aaabbb
重复字符不连续出现,比如:abababa
完整代码如下:

#include <iostream>
#include <cstring>
using namespace std;

string removeDuplicate1(string s)
{
    int check = 0;
    int len = s.length();
    if(len < 2) return s;
    string str = "";
    for(int i=0; i<len; ++i)
    {
        int v = (int)(s[i]-'a');
        if((check & (1<<v)) == 0)
        {
            str += s[i];
            check |= (1<<v);
        }
    }
    return str;
}

string removeDuplicate2(string s)
{
    int len = s.length();
    if(len < 2) return s;
    string str = "";
    for(int i=0; i<len; ++i)
    {
        if(s[i] != '\0')
        {
            str += s[i];
            for(int j=i+1; j<len; ++j)
                if(s[j]==s[i])
                    s[j] = '\0';
        }
    }
    return str;
}

void removeDuplicate3(char s[])
{
    int len = strlen(s);
    if(len < 2) return;
    int p = 0;
    for(int i=0; i<len; ++i)
    {
        if(s[i] != '\0')
        {
            s[p++] = s[i];
            for(int j=i+1; j<len; ++j)
                if(s[j]==s[i])
                    s[j] = '\0';
        }
    }
    s[p] = '\0';
}

void removeDuplicate4(char s[])
{
    int len = strlen(s);
    if(len < 2) return;
    bool c[256];
    memset(c, 0, sizeof(c));
    int p = 0;
    for(int i=0; i<len; ++i)
    {
        if(!c[s[i]])
        {
            s[p++] = s[i];
            c[s[i]] = true;
        }
    }
    s[p] = '\0';
}

void removeDuplicate5(char s[])
{
    int len = strlen(s);
    if(len < 2) return;
    int check = 0, p = 0;
    for(int i=0; i<len; ++i)
    {
        int v = (int)(s[i]-'a');
        if((check & (1<<v))==0)
        {
            s[p++] = s[i];
            check |= (1<<v);
        }
    }
    s[p] = '\0';
}
int main()
{
    string s1 = "abcde";
    string s2 = "aaabbb";
    string s3 = "";
    string s4 = "abababc";
    string s5 = "ccccc";
    cout<<removeDuplicate1(s1)<<" "<<removeDuplicate2(s1)<<endl;
    cout<<removeDuplicate1(s2)<<" "<<removeDuplicate2(s2)<<endl;
    cout<<removeDuplicate1(s3)<<" "<<removeDuplicate2(s3)<<endl;
    cout<<removeDuplicate1(s4)<<" "<<removeDuplicate2(s4)<<endl;
    cout<<removeDuplicate1(s5)<<" "<<removeDuplicate2(s5)<<endl;
    char ss1[] = "abcde";
    char ss2[] = "aaabbb";
    char ss3[] = "";
    char ss4[] = "abababc";
    char ss5[] = "ccccc";
    removeDuplicate5(ss1);
    removeDuplicate5(ss2);
    removeDuplicate5(ss3);
    removeDuplicate5(ss4);
    removeDuplicate5(ss5);
    cout<<ss1<<" "<<ss2<<" "<<ss3<<" "<<ss4<<" "<<ss5<<endl;
    return 0;
}

测试结果如下:

时间: 2024-08-17 13:05:11

经典算法面试题目-设计算法移除字符串中重复的字符(1.3)的相关文章

经典算法面试题目-判断两个字符串是否是变位词(1.4)

题目 Write a method to decide if two strings are anagrams or not. 写一个函数判断两个字符串是否是变位词. 解答 变位词(anagrams)指的是组成两个单词的字符相同,但位置不同的单词. 比如说, abbcd和abcdb就是一对变位词. 也就是说,2个字符串,不管排列顺序如何,只要全部的单个字符能对应上,就是一对变位词! 该题目有两种做法: 时间复杂度为O(nlogn)的解法 由于组成变位词的字符是一模一样的,所以按照字典序排序后,两

C# 移除数组中重复数据

#region 移除数组中重复数据 /// <summary> /// 移除数组中重复数据 /// </summary> /// <param name="array">需要除重的数组</param> /// <returns>不重复数组</returns> public static string[] DelRepeatData(string[] array) { return array.GroupBy(p =

javascript数组删除 排序 移除数组中重复

删除数组中数据 array.prototype.del = function(n) { if (n<0) return this; return this.slice(0,n).concat(this.slice(n+1,this.length)); } // 数组洗牌 array.prototype.random = function() { var nr=[], me=this, t; while(me.length>0) { nr[nr.length] = me[t = math.flo

经典算法面试题目-判断一个字符串中的字符是否唯一(1.1)

题目: Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures? 实现一个算法来判断一个字符串中的字符是否唯一(即没有重复).不能使用额外的数据结构. (即只使用基本的数据结构) 解答: 首先,你可以问面试官,构成字符串的字符集有多大?是ASCII字符,还是只是26个字母? 还是有更大的字符集,对于不同

C字符串中删除指定字符几种算法

题如下图所示   题目意思很明显了,我们的思路其实也挺简单的,换句话说,删掉一个然后重构数组,补上那个空,一个个字符推进一格就行了嘛,不用想得太复杂(简单的来说就是偷懒).  代码如下 复制代码 #include<stdio.h> #include<string.h> void delchar(char s[], char c); int main(void) {  char c;  char s[80];  printf("Input a string: ")

经典算法面试题目-置矩阵行列元素为0(1.7)

题目 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0. 写一个函数处理一个MxN的矩阵,如果矩阵中某个元素为0,那么把它所在的行和列都置为0. 解答 简单题.遍历一次矩阵,当遇到元素等于0时,记录下这个元素对应的行和列. 可以开一个行数组row和列数组col,当元素a[i][j]等于0时, 就把row[i]和col[j]置为1. 第二次遍

经典算法面试题目-矩阵旋转90度(1.6)

题目 Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place? 一张图像表示成NxN的矩阵,图像中每个像素是4个字节,写一个函数把图像旋转90度. 你能原地进行操作吗?(即不开辟额外的存储空间) 解答 我们假设要将图像逆时针旋转90

经典算法面试题目-判断s2是否是s1的旋转字符串(1.8)

题目 Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring ( i.e., "waterbottle" is a rotation of &qu

经典算法面试题目-翻转一个C风格的字符串(1.2)

题目: Write code to reverse a C-Style String. (C-String means that "abcd" is represented as five characters, including the null character.) 写代码翻转一个C风格的字符串.(C风格的意思是"abcd"需要用5个字符来表示,包含末尾的 结束字符) 解答: 这道题如果就是要考察你有没有注意到C风格字符串最后的那个结束符,那我觉得还是像书