博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
100-63
阅读量:6933 次
发布时间:2019-06-27

本文共 1609 字,大约阅读时间需要 5 分钟。

hot3.png

63.在字符串中删除特定的字符(字符串)。

题目:输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。

例如,输入”They are students.”和”aeiou”,

则删除之后的第一个字符串变成”Thy r stdnts.”。

分析:这是一道微软面试题。在微软的常见面试题中,与字符串相关的题目占了很大的一部分,

因为写程序操作字符串能很好的反映我们的编程基本功。

思路:

老题了。首先要考虑的是怎么快速的删除节点。我觉的只要是考虑到要快速插入删除的,就用链表。然后就是要考虑怎么快速的判断在第二个字符串中是否存在当前的字符。这点如果用扫描的的话,整个的时间复杂度就是O(mn)。那么我们就想到可以使用hash表的方式,就可以在O(1)的时间复杂度中判断是否存在。整个的时间复杂度为O(m+n)

贴上代码,代码不是我自己写的,是在网上搜集的:

///// Delete all characters in pStrDelete from pStrSource///void DeleteChars(char* pStrSource, const char* pStrDelete){      if(NULL == pStrSource || NULL == pStrDelete)            return;       // Initialize an array, the index in this array is ASCII value.      // All entries in the array, whose index is ASCII value of a      // character in the pStrDelete, will be set as 1.      // Otherwise, they will be set as 0.      const unsigned int nTableSize = 256;      int hashTable[nTableSize];      memset(hashTable, 0, sizeof(hashTable));       const char* pTemp = pStrDelete;      while ('\0' != *pTemp)      {            hashTable[*pTemp] = 1;            ++ pTemp;      }       char* pSlow = pStrSource;      char* pFast = pStrSource;      while ('\0' != *pFast)      {            // if the character is in pStrDelete, move both pStart and            // pEnd forward, and copy pEnd to pStart.            // Otherwise, move only pEnd forward, and the character            // pointed by pEnd is deleted            if(1 != hashTable[*pFast])            {                  *pSlow = *pFast;                  ++ pSlow;            }             ++pFast;      }       *pSlow = '\0';}

转载于:https://my.oschina.net/dapengking/blog/95771

你可能感兴趣的文章
架构周报:微信后台系统的演进之路
查看>>
Oracle宣布提供新的Java支持价格体系
查看>>
phpstrom配置svn/git提交
查看>>
关于Redux的一些总结(一):Action & 中间件 & 异步
查看>>
专访1药网技术副总裁黄哲铿:揭秘技术跨界管理之道
查看>>
Markdown通用的常用语法说明
查看>>
gulp关于scss的基础配置
查看>>
PHP:echo、print、print_r() 和 var_dump()
查看>>
Gerrit代码Review入门实战
查看>>
Swift中一个类中的枚举(enum)类型的数据该如何实现序列化(NSCoder)
查看>>
WebSocket 原理
查看>>
按端口终止进程
查看>>
Permutations I & II leetcode
查看>>
[LeetCode/LintCode] Factorial Trailing Zeros
查看>>
iOS病毒XcodeGhost批量检测工具,开源Github(检测ipa文件)
查看>>
npm 加入 TC39 委员会,参与定制 JavaScript 标准
查看>>
centos7.2安装mysql
查看>>
关于 Python
查看>>
AVFoundation学习Demo--拍摄视频
查看>>
阿里云账号注册流程方法(图文教程)
查看>>