题解 | #替换空格#

替换空格

https://www.nowcoder.com/practice/0e26e5551f2b489b9f58bc83aa4b6c68

这一题一看到就想到了队列(结果顺序不变),先将字符串全部压入队列,再pop之前确任front是否为空格。

  • 时间复杂度:O(2n)
  • 空间复杂度:用了一个和字符串等长的队列,所以是O(n)
    string replaceSpace(string s) {
          // write code here
          queue  q;
          char i;
          string::iterator it;
          string result = "";
          if(s.empty()) return result;
          for(it = s.begin();it!=s.end();it++) {
              q.push(*it);
          }
          while(!q.empty()) {
              i = q.front();
              if(i == ' ') {
                  result += "%20";
              } else {
                  result += i;
              }
              q.pop();
          }
          return result;
      }
全部评论

相关推荐

码农顶针:估计让你免费辅导老板孩子的学习
点赞 评论 收藏
分享
09-12 18:28
门头沟学院 Java
网友描述的太精准了👍
迷茫的大四🐶:不管活脏还是累,钱到位就行,钱到位啥都不用抱怨
投递字节跳动等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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