题解 | #单词替换#

单词替换

https://www.nowcoder.com/practice/5b58a04679d5419caf62c2b238e5c9c7

#include <iostream>
#include<bits/stdc++.h>
#include <sstream>
using namespace std;

int main() {
    vector<string > st ;
    string tmp ;  
    while(getline(cin , tmp))
    {
        istringstream is(tmp) ;
        while(!is.eof())
        {
            string t; 
            is>>t ; 
            st.push_back(t) ; 
        }
        string willbereplace ; 
        getline(cin , willbereplace) ;
        string toreplace ;  
        getline(cin , toreplace) ; 
        for(int i = 0 ; i < st.size() ; ++ i)
        {
            if(st[i] == willbereplace)
            {
                st[i] = toreplace ; 
            }
        }
        for(int i = 0 ; i < st.size() ; ++ i)
        {
            cout<<st[i]<<" "; 
        }
        
    }
    // st.push_
}
// 64 位输出请用 printf("%lld")

使用了额外的空间。所以空间效率不高

全部评论

相关推荐

12-13 14:51
已编辑
井冈山大学 算法工程师
龙虾x:算法比你强的没有你美,比你美的…..算了已经没有比你美的了
工作两年想退休了
点赞 评论 收藏
分享
牛至超人:把哈工大,再加大加粗,看见闪闪发光的哈工大字样,面试官直接流口水
投递字节跳动等公司6个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务