zoukankan      html  css  js  c++  java
  • 编程之法第一章习题——删除特定的字符

    题目


    给定一个原始字符串和模式字符串,要求在原始字符串中删除所有在模式字符串中出现过的字符,对应位置用空格占位。要求性能最优。例如:原始字符串为“They are students.”,模式字符串为“aeiou”,那么删除之后得到的字符串为“They r stdnts.”

    代码


    #pragma once
    #include<iostream>
    #include <map>
    using namespace std;
    class Solution
    {
    public:
        void deleteStr(string& rawStr, string& matchStr)
        {
            map<char, bool> matchTable;
            for (auto i:matchStr)
            {
                matchTable[i] = true;
            }
            int step = 0;
            for (auto iter = rawStr.begin(); iter != rawStr.end(); iter++)
            {
                *(iter - step) = *iter;
                if (matchTable[*iter])
                {
                    step++;
                }
    
            }
            rawStr.erase(rawStr.end() - step, rawStr.end());
        }
    };
    https://github.com/li-zheng-hao
  • 相关阅读:
    Tree Grafting
    敌兵布阵
    畅通工程(并查集)
    The Suspects(并查集)
    Ubiquitous Religions(friends变形)
    Friends(采用树结构的非线性表编程)
    小球下落(二叉树)
    铁轨
    卡片游戏
    征服C指针
  • 原文地址:https://www.cnblogs.com/lizhenghao126/p/11053678.html
Copyright © 2011-2022 走看看