lintcode 684. 缺少的字符串 find

给出两个字符串,你需要找到缺少的字符串

样例
样例 1:

输入 : str1 = "This is an example", str2 = "is example"
输出 : ["This", "an"]
注意事项
输出时字符串顺序为输入字符串内的顺序

思路:把s1,s2每一个单词分别放进不同的vector中,在比较两个vector中的单词是否能相互找到,若不能,放进结果容器中。
注意:find函数若无法找到返回end()。

class Solution {
public:
    /**
     * @param str1: a given string
     * @param str2: another given string
     * @return: An array of missing string
     */
    vector<string> missingString(string &str1, string &str2) {
        // Write your code here
        int len1=str1.size();
        int len2=str2.size();
        vector<string>s1;
        vector<string>s2;
        vector<string>result;
        string temp="";
        for (int i = 0; i < len1; i++) {
            /* code */
            if(str1[i]==' ') {s1.push_back(temp);temp="";}
            else temp+=str1[i];
            if(i==len1-1) {s1.push_back(temp);temp="";}
        }
        for (int i = 0; i < len2; i++) {
            /* code */
            if(str2[i]==' ') {s2.push_back(temp);temp="";}
            else temp+=str2[i];
            if(i==len2-1) {s2.push_back(temp);temp="";}
        }
        for (auto judge : s1) {
            /* code */
            if(find(s2.begin(),s2.end(),judge)==s2.end()) result.push_back(judge);
        }
        return result;
    }
};```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值