程序员面试100题之36在字符串中删除特定的字符

来源:互联网 发布:uvwxyz.xyz index.php 编辑:程序博客网 时间:2024/04/27 15:42
// 程序员面试100题之36在字符串中删除特定的字符.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>using namespace std;/** like hash table, fasten the search speed*/int _tmain(int argc, _TCHAR* argv[]){char str1[]="they are students";//delete from str1 while the char exist in str2char str2[]="aeiou";int hashtable[26]={0};char c;for(int i2=0,c=str2[0];c!='\0';){hashtable[c-'a']=1;c=str2[++i2];}for (int i1=0;i1<26;i1++){cout<<hashtable[i1]<<" ";}char ch=str1[0];int i=0,j=0; for (i=0,j=0;str1[i]!='\0';)//j is the bond of the final string, {ch=str1[i];if ((ch>'z'||ch<'a')||(hashtable[ch-'a']==0))//this can be executed even ch is null, but ....{if(i!=j)str1[j++]=str1[i++];// when last letter because ch does not be changed so the for loop can be executed once more //it is necessary to copy in every step, otherwise it is difficult and give arise to many problemselse{i++;j++;}}elsei++;//while(hashtable[ch-'a']==1)// if while is here then the logic is messed up//{//ch=str1[++i];//}}//system("pause");str1[j]='\0';cout<<str1;cout<<endl;/**/system("pause");return 0;}

简单的一道题,做了2小时,太菜了。犯了简单的错误,其实也不简单,熟不熟试试就知道。随便问个问题,“你能投篮进球吗?”,你可以轻松地回答,“我能”。因为你可以投一百遍,一千遍,投一天,只要进一个求就算了。但是只让你投一次呢?在三分外呢?在真正的比赛中呢?在有防守的情况下呢?在关键时刻呢?在受干扰的时候呢?只剩一秒的时间呢?“你还能吗?”熟不熟现在就知道了。
原创粉丝点击