剑指offer 35-第一次只出现一次的字符
2015-06-24 10:55
232 查看
1.
使用hash表,存储字符出现次数,然后扫描出现次数为1的字符
扩展
2.实现一函数,给定两个字符串str1和str2,将字符串str2中在str1出现的字符删除掉。例如,str1=“We are strudent”,str2=“eas”,最终返回“W r tudnt”。
做法:遍历str2并将其字符放入哈希数组中,再遍历str1,去掉哈希数组中存在的字符即可。与上题思路一致。时间复杂度为O(n+m),n和m分别为str1和str2的长度。空间为O(1)。
3.
实现一函数,将字符串中出现重复字符全部去掉。例如,str=“google”,那么返回为“gole”。
做法:类似,可用一个布尔数组去记录。第一次出现标志位true,当第二次访问该字符时,当前标志位为true时,删除当前字符。时间复杂度为O(n),n为str的长度。[b]空间为O(1)。[/b]
#include<iostream>
using namespace std;
void deleteDupChar(char *str)
{
if(str==NULL)
return ;
const int tableSize=256;
bool table[tableSize];
for(int i=0;i<tableSize;i++)
table[i]=false;
char *result = new char(strlen(str)+1);
char *p=result;
while(*str!='\0')
{
if(table[*str]==false)
{
*p++ = *str;
table[*str] = true;
}
str++;
}
*p='\0';
while(*result!='\0')
{cout<<*result;
result++;
}
}
int main()
{
char *str ="google";
deleteDupChar(str);
}4.变位词
#include<iostream>
using namespace std;
bool IsAnagramChar(char *firstStr,char *secondStr)
{
if(firstStr==NULL || secondStr==NULL)
return false;
const int tableSize=256;
int table[tableSize];
for(int i=0;i<tableSize;i++)
table[i]=0;
while(*firstStr!='\0')
table[*(firstStr++)]++;
while(*secondStr!='\0')
table[*(secondStr++)]--;
for(int i=0;i<tableSize;i++)
{
if(table[i]!=0)
return false;
}
return true;
}
int main()
{
char *firtsStr ="slient";
char *secondStr="listen";
cout<<IsAnagramChar(firtsStr,secondStr);
}
题目:在字符串中找出第一个只出现一次的字符。如输入“abaccdeff”,则输出‘b’。
使用hash表,存储字符出现次数,然后扫描出现次数为1的字符
#include<iostream> using namespace std; char FirstNotRepeatingChar(char *str) { if(str==NULL) return '\0'; const int tableSize=256; int table[tableSize]; for(int i=0;i<tableSize;i++) table[i]=0; char *p=str; while((*p)!='\0') table[*(p++)]++; p=str; while(*p!='\0') { if(table[*p]==1) return *p; p++; } return '\0'; } void Test(char* testName,char *str,char expected) { cout<<testName<<"\t"; if(expected == FirstNotRepeatingChar(str)) cout<<"Passed\n\n"; else cout<<"Failed\n\n"; } void Test1() { char *str = "abaccdeff"; Test("test1",str,'b'); } void Test2() { char *str = "a"; Test("test2",str,'a'); } void Test3() { char *str = "ababccddff"; Test("test3",str,'\0'); } void Test4() { char *str = NULL; Test("test4",str,'\0'); } int main() { Test1(); Test2(); Test3(); Test4(); }
扩展
2.实现一函数,给定两个字符串str1和str2,将字符串str2中在str1出现的字符删除掉。例如,str1=“We are strudent”,str2=“eas”,最终返回“W r tudnt”。
做法:遍历str2并将其字符放入哈希数组中,再遍历str1,去掉哈希数组中存在的字符即可。与上题思路一致。时间复杂度为O(n+m),n和m分别为str1和str2的长度。空间为O(1)。
#include<iostream> using namespace std; void deleteChar(char *str,char *deleted) { if(str==NULL) return ; const int tableSize=256; int table[tableSize]; for(int i=0;i<tableSize;i++) table[i]=0; while((*deleted)!='\0') table[*(deleted++)]=1; char *result = new char(strlen(str)+1); char *p=result; while(*str!='\0') { if(table[*str]!=1) { *p++ = *str; } str++; } *p='\0'; while(*result!='\0') {cout<<*result; result++; } } int main() { char *firstStr ="we are students."; char *secondStr = "aeiou"; deleteChar(firstStr,secondStr); }
3.
实现一函数,将字符串中出现重复字符全部去掉。例如,str=“google”,那么返回为“gole”。
做法:类似,可用一个布尔数组去记录。第一次出现标志位true,当第二次访问该字符时,当前标志位为true时,删除当前字符。时间复杂度为O(n),n为str的长度。[b]空间为O(1)。[/b]
#include<iostream>
using namespace std;
void deleteDupChar(char *str)
{
if(str==NULL)
return ;
const int tableSize=256;
bool table[tableSize];
for(int i=0;i<tableSize;i++)
table[i]=false;
char *result = new char(strlen(str)+1);
char *p=result;
while(*str!='\0')
{
if(table[*str]==false)
{
*p++ = *str;
table[*str] = true;
}
str++;
}
*p='\0';
while(*result!='\0')
{cout<<*result;
result++;
}
}
int main()
{
char *str ="google";
deleteDupChar(str);
}4.变位词
#include<iostream>
using namespace std;
bool IsAnagramChar(char *firstStr,char *secondStr)
{
if(firstStr==NULL || secondStr==NULL)
return false;
const int tableSize=256;
int table[tableSize];
for(int i=0;i<tableSize;i++)
table[i]=0;
while(*firstStr!='\0')
table[*(firstStr++)]++;
while(*secondStr!='\0')
table[*(secondStr++)]--;
for(int i=0;i<tableSize;i++)
{
if(table[i]!=0)
return false;
}
return true;
}
int main()
{
char *firtsStr ="slient";
char *secondStr="listen";
cout<<IsAnagramChar(firtsStr,secondStr);
}
相关文章推荐
- CSS3前端开发中需要用到的变换矩阵
- [Token] 从index.jsp中获取Token
- UFT与jQuery的交互DEMO
- 关于Action向页面传递数据,对传递属性JSON化的拦截。
- $.getJSON( )的使用方法简介
- SVG路径动画解密
- CSS3 box-shadow 属性
- ExtJs里表格自动显隐滚动条
- ExtJs里表格自动显隐滚动条
- 对JavaScript的全文搜索实现相关度评分的功能的方法
- 解决Maven报Plugin execution not covered by lifecycle configuration
- 浅析Java静态代码的分析工具Infer
- 剑指offer 34- 丑数
- css
- 深入理解javascript中的Function.prototye.bind
- jsp页面table列表
- jsp页面div列表
- JavaScript笔记
- 7 个基本的 JS 函数【译】
- JS之OOP