• LintCode 3208:String splitting字符串处理入门题


    3208 · String splitting
    C++
    Easy

    This topic is a pre-release topic. If you encounter any problems, please contact us via “Problem Correction”, and we will upgrade your account to VIP as a thank you.
    Description
    This question requires you to complete the function splitString in the file Solution.cpp.

    This function takes two arguments.

    A string s as the original string to be split.
    A non-empty string t is used as a splitting basis.
    All the split substrings in order are formed into a vector and returned using the return statement.

    The evaluation machine runs main.cpp to call the splitString function in Soluction.cpp by importing a custom function library, and parses the contents of the returned string vector.

    There is no need to use the cin statement or the cout statement for this problem.
    The input data is stored in the arguments of the function, and the output data is returned using the return statement.
    Example
    Input Sample 1:

    asdf,Hello,ok
    ,
    Output sample 1:

    [“asdf”, “Hello”, “ok”]
    All commas , of the original string are removed and the string is separated from the comma position.

    Input Sample 2:

    ,
    Output sample 2:

    []
    The original string is empty, so an empty vector container is returned.

    解法1:

    /**
     * @param s: A string
     * @param t: A string
     * @return: A vector of sub strings
     */
    vector<string> splitString(string &s, string &t) {
        vector<string> res;
        string s2(s);
        while (s2.size() > 0) {
            int pos = s2.find(t);
            if (pos == -1) {
                res.push_back(s2);
                break;
            }
            res.push_back(s2.substr(0, pos));
            s2 = s2.substr(pos + 1);
        }
        return res;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    解法2:

    /**
     * @param s: A string
     * @param t: A string
     * @return: A vector of sub strings
     */
    vector<string> splitString(string &s, string &t) {
        vector<string> res;
        int start = 0, pos = 0;
        pos = s.find(t);
        while (pos != -1) {
            res.push_back(s.substr(start, pos - start));
            start = pos + 1;
            pos = s.find(t, start);
            if (pos == -1) res.push_back(s.substr(start));  
        }
        //if (pos == -1) res.push_back(s.substr(pos + 1));
        return res;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    phpcms_v9模板制作及二次开发常用代码
    HTML5期末考核大作业,网站——旅游景点。 学生旅行 游玩 主题住宿网页
    搭建个人博客,Docsify+Github webhook+JGit解决方案
    mxnet gluon GRU 文档
    在windows10环境下tensorboardX的安装及使用
    上周热点回顾(12.11-12.17)
    Serverless 产品手册
    Python学习03、空语句,顺序,条件,循环语句、缩进和代码块、模块
    JS快速入门
    10.java项目-尚医通(10)
  • 原文地址:https://blog.csdn.net/roufoo/article/details/127645332