返回介绍

Space Replacement

发布于 2025-02-22 13:01:22 字数 2163 浏览 0 评论 0 收藏 0

Source

Write a method to replace all spaces in a string with %20. 
The string is given in a characters array, you can assume it has enough space 
for replacement and you are given the true length of the string.

Example
Given "Mr John Smith", length = 13.

The string after replacement should be "Mr%20John%20Smith".

Note
If you are using Java or Python,please use characters array instead of string.

Challenge
Do it in-place.

题解

根据题意,给定的输入数组长度足够长,将空格替换为 %20 后也不会溢出。通常的思维为从前向后遍历,遇到空格即将 %20 插入到新数组中,这种方法在生成新数组时很直观,但要求原地替换时就不方便了,这时可联想到插入排序的做法——从后往前遍历,空格处标记下就好了。由于不知道新数组的长度,故首先需要遍历一次原数组,字符串类题中常用方法。

需要注意的是这个题并未说明多个空格如何处理,如果多个连续空格也当做一个空格时稍有不同。

C++

int replaceBlank(char string[], int length) {
  int n = 0;
  for (int i=0; i<length; i++)
    if (string[i] == ' ') n++;

  int new_len = length + n*2;
  for (int i=length-1; i>=0; i--) {
    if (string[i] != ' ') {
      string[--new_len] = string[i];
    } else {
      string[--new_len] = '0';
      string[--new_len] = '2';
      string[--new_len] = '%';
    }
  }
  return length + n*2;
}

Java

public class Solution {
  /**
   * @param string: An array of Char
   * @param length: The true length of the string
   * @return: The true length of new string
   */
  public int replaceBlank(char[] string, int length) {
    if (string == null) return 0;

    int space = 0;
    for (char c : string) {
      if (c == ' ') space++;
    }

    int r = length + 2 * space - 1;
    for (int i = length - 1; i >= 0; i--) {
      if (string[i] != ' ') {
        string[r] = string[i];
        r--;
      } else {
        string[r--] = '0';
        string[r--] = '2';
        string[r--] = '%';
      }
    }

    return length + 2 * space;
  }
}

源码分析

先遍历一遍求得空格数,得到『新数组』的实际长度,从后往前遍历。

复杂度分析

遍历两次原数组,时间复杂度近似为 O(n)O(n)O(n), 使用了 r 作为标记,空间复杂度 O(1)O(1)O(1).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文