【字符串与数组】

Q: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

题目:设计一个算法,在不使用额外存储空间的情况下,去掉字符串中重复的字符。(注:允许使用一个或者两个附件变量,但不能傻不拉几的把所有的字符复制一遍)

解答:


方法一:暴力解法,时间复杂度为O(n3)

从头至尾遍历字符串,对于每个字符,判断其后面的每个字符是否与其相同,如果相同,则将相同字符到字符串结尾的所有字符前移一位(相当于删除了该相同的字符)。

void remove_duplicate(char* str){ if(str==NULL) return; int len=strlen(str); if(len<2) return; int i,j,k; for(i=0;i<len;++i) for(j=i+1;j<len;++j) if(str[i]==str[j]){ for(k=j+1;k<len;++k){ str[k-1]=str[k]; len--; } } str[len-1]='\0'; }

方法二:优雅的解法,时间复杂度为O(n2)。

遍历字符串,遇到相同的字符,就将其置为’\0’。注意这里index变量的使用。

void remove_duplicate(char* str){ if(str==NULL) return; int len=strlen(str); if(len<2) return; int i,j; int index=0; for(i=0;i<len;++i){ if(str[i]!='\0'){ str[index++]=str[i]; for(j=i+1;j<len;++j){ if(str[j]==str[i]) str[j]='\0'; } } } str[index]='\0'; }

方法三:文艺一点的解法,时间复杂度为O(n)。

题目要求在不使用额外存储空间操作,我开辟一个常数大小的数组不为过吧?假设字符串都为ascii码不为过吧?如果可以开辟常数大小的空间,可以假设字符串都为ascii码字符,那么文艺范的思维是这样的:开辟一个256大小的数组,遇到一个字符,根据该字符的ascii码值,将数组对应的位置为1。所以,每次我们判断一个字符是不是重复出现的字符,只需要先看看数组中那个位置上是不是已经是1了,如果已经是1,那它之前出现过。

void remove_duplicate(char* str){ if(str==NULL) return; int len=strlen(str); if(len<2) return; char flags[256]; memset(flags,0,sizeof(flags)); int i,j; int index=1; flags[str[0]]=1; for(i=1;i<len;++i){ if(flags[str[i]]!=1){ str[index++]=str[i]; flags[str[i]]=1; } } str[index]='\0'; }

测试用例:

1.不含重复字符的字符串,如abcdefg

2.全是重复字符的字符串,如aaaaaa

3.NULL字符串

4.空字符串

5.含有连续重复字符的字符串,如aaaabbbb

6.含有非连续重复字符的字符串,如ababab


作者:Viidiot  微信公众号:linux-code

[google面试CTCI]1-3.字符串去重的更多相关文章

  1. [google面试CTCI] 1-5.替换字符串中特定字符

    [字符串与数组] Q:Write a method to replace all spaces in a string with ‘%20’ 题目:写一个算法将一个字符串中的空格替换成%20 解答: ...

  2. [google面试CTCI] 1-8.判断子字符串

    [字符串与数组] Q:Assume you have a method isSubstring which checks if one word is a substring of another G ...

  3. [google面试CTCI] 1-4.判断两个字符串是否由相同字符组成

    [字符串与数组] Q:Write a method to decide if two strings are anagrams or not 题目:写一个算法来判断两个字符串是否为换位字符串.(换位字 ...

  4. [google面试CTCI] 1-7.将矩阵中特定行、列置0

    [字符串与数组] Q:Write an algorithm such that if an element in an MxN matrix is 0, its entire row and colu ...

  5. [google面试CTCI] 1-6.图像旋转问题

    [字符串与数组] Q:Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, wr ...

  6. [google面试CTCI] 2-1.移除链表中重复元素

    [链表] Q:Write code to remove duplicates from an unsorted linked list      FOLLOW UP      How would yo ...

  7. [google面试CTCI] 2-2 找出链表的倒数第n个节点元素

    [链表] Q:Implement an algorithm to find the nth to last element of a singly  linked list . 题目:找出链表的倒数第 ...

  8. [google面试CTCI] 2-3 只给定链表中间节点指针,如何删除中间节点?

    [链表] Q:Implement an algorithm to delete a node in the middle of a single linked list, given only acc ...

  9. [google面试CTCI] 2-0.链表的创建

    创建链表.往链表中插入数据.删除数据等操作,以单链表为例. 1.使用C语言创建一个链表: typedef struct nd{ int data; struct nd* next; } node; / ...

随机推荐

  1. [Linux]使用Clang实现代码静态分析

    1.按下开关Clang sudo apt-get install Clang 2.编写测试程序  memleak.c #include<stdio.h> #include<stdli ...

  2. jQuery - 基于serializeArray的serializeObject

    将表单序列化成JSON对象,注意不管是自实现的serializeObject()还是原生的serializeArray(),所要序列化的控件都必须要有name,而不是id jQuery.prototy ...

  3. Matlab图像处理系列4———傅立叶变换和反变换的图像

    注意:这一系列实验的图像处理程序,使用Matlab实现最重要的图像处理算法 1.Fourier兑换 (1)频域增强 除了在空间域内能够加工处理图像以外,我们还能够将图像变换到其它空间后进行处理.这些方 ...

  4. crawler_java应用集锦9:httpclient4.2.2的几个常用方法,登录之后访问页面问题,下载文件_设置代理

    在工作中要用到android,然后进行网络请求的时候,打算使用httpClient. 总结一下httpClient的一些基本使用. 版本是4.2.2. 使用这个版本的过程中,百度很多,结果都是出现的o ...

  5. measureChildren作品

    无论是在改写View依然是ViewGroup什么时候.特别ViewGrop什么时候,通常是不可避免的重写onMeasure方法,我们一定会调用setMeasuredDimension()将測量好的宽高 ...

  6. Matlab基于学习------------------函数微分学

    <span style="font-size:18px;">% 函数微分学 % 函数微分学难比功能区,中的积分函数的性质整体叙述性说明.在某些时候差描述叙事的斜率功能 ...

  7. 用fildder 查看loveuv 刷流量时通信的数据

    loveuv是一个用来刷网页流量的站点,以下介绍怎么查看它刷流量时数据的传输 首先是注冊页面,邀请码UBMNEY 注冊账号登陆后,在账户资料页面http://www.loveuv.com/user/m ...

  8. ListView排序并隔色显示

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...

  9. Linq的理论知识

    概述 前面的博客中写到过关于Linq的一些知识,可是,没有具体的说Linq,本篇博客将会说一下Linq. 什么是Linq Linq是一个概念,它实现了数据查询使用同一方式,即,它使我们程序猿通过使用它 ...

  10. Linux server关闭自己主动

    公司linux server发生错误.mysql server没有理由关闭,我找不到理由.Version: '5.6.13-enterprise-commercial-advanced' socket ...