【CODE】字符串——牛客·剑指offer

替换空格

请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

class Solution {
public:
	void replaceSpace(char *str,int length) {
        int spacenum=0,oldlength=0,newlength=0;
        if(str==NULL || length<0) return ;
        while(str[oldlength]!='\0'){
            if(str[oldlength]==' ') spacenum++;
            oldlength++;
        }
        newlength=oldlength+spacenum*2;
        if(newlength>length) return ;
        int poldlength=oldlength,pnewlength=newlength;
        while(poldlength>=0 && pnewlength>poldlength){
            if(str[poldlength]!=' '){
                str[pnewlength]=str[poldlength];
                poldlength--;pnewlength--;
            }else{
                str[pnewlength--]='0';
                str[pnewlength--]='2';
                str[pnewlength--]='%';
                poldlength--;
            }
        }
        return ;
    }
};

正则表达式匹配

请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配

  • 两个字符串都为空:true
  • str不为空,pattern为空:false
  • pattern下一个字符不为'*':当前字符匹配成功(两个字符相同,或者pattern为'.'且str不为'\0'),则匹配下一个;当前字符匹配不成功,false。
  • pattern下一个字符为'*':当'*'匹配0个字符时,str当前字符不变,pattern字符向后移两位;当'*'匹配1个或多个字符时,str当前字符向下移一位,pattern不变。
     
class Solution {
public:
    bool match(char* str, char* pattern){
        if(*str=='\0' && *pattern=='\0') return true;
        if(*str!='\0' && *pattern=='\0') return false;
        if(*(pattern+1)!='*'){
            if(*pattern==*str || *pattern=='.' && *str!='\0') return match(str+1,pattern+1);
            else return false;
        }else{
            if(*pattern==*str || *str!='\0' && *pattern=='.')
                return match(str,pattern+2)||match(str+1,pattern);
            else return match(str,pattern+2);
        }
    }
};

表示数值的字符串

请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。

class Solution {
public:
    bool isNumeric(char* string){
        if(string==NULL) return false;
        if(*string=='+' || *string=='-') string++;
        if(*string=='\0') return false;
        int dot=0,num=0,nume=0;
        while(*string!='\0'){
            if(*string>='0'&&*string<='9'){
                string++;num++;
            }else if(*string=='.'){
                if(dot>0 || nume>0) return false;
                string++;dot++;
            }else if(*string=='e' || *string=='E'){
                if(num==0 || nume>0) return false;
                string++;nume++;
                if(*string=='+' || *string=='-') string++;
                if(*string=='\0') return false;
            }else return false;
        }
        return true;
    }

};

字符流中第一个不重复的字符

请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。

输出描述: 如果当前字符流没有存在出现一次的字符,返回#字符。

class Solution
{
public:
  //Insert one char from stringstream
    void Insert(char ch){
        ++hashArray[ch-'\0'];
        if(hashArray[ch-'\0']==1) data.push_back(ch);
    }
  //return the first appearence once char in current stringstream
    char FirstAppearingOnce(){
        while(!data.empty() && hashArray[data.front()]>=2) data.pop_front();
        if(data.empty()) return '#';
        return data.front();
    }
    private:
    unsigned char hashArray[128];
    deque<char> data;
};
发布了133 篇原创文章 · 获赞 35 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/Li_Jiaqian/article/details/104211291