剑指offer——面试题4:替换空格

力扣,https://leetcode.cn/problems/ti-huan-kong-ge-lcof/description/

Python

class Solution:
    def pathEncryption(self, path: str) -> str:
        return path.replace('.', ' ')

Java

class Solution {
    public String pathEncryption(String path) {
        StringBuilder sBuilder = new StringBuilder();
        for (int i = 0; i < path.length(); ++i) {
            if (path.charAt(i) == '.') {
                sBuilder.append(' ');
            } else {
                sBuilder.append(path.charAt(i));
            }
        }

        return sBuilder.toString();
    }
}

此题的关键思路在于字符串的从后向前复制!!!

class Solution {
public:
	void replaceSpace(char *str,int length) {//length是数组的容量
        if(str == NULL || length<0) return;//排除异常情况
        int m=strlen(str),num_blank=0;
        for(int i=0;str[i]!='\0';i++)
            if(str[i]==' ')
                num_blank++;
        int n=m+2*num_blank;
        if(n+1>length) return;
        int i=m,j=n;//字符串的结束符'\0'也需要手动复制
        while(i>=0 && j>i){
            if(str[i]==' '){
                str[j--]='0';
                str[j--]='2';
                str[j--]='%';
            }
            else{
                str[j--]=str[i];
            }
            i--;
        }
	}
};

反思一下自己:每天就跟傻逼一样活着
20190901日重做
C版

class Solution {
public:
	void replaceSpace(char *str,int length) {
        if (str == NULL || length == 0) {
            return;
        }
        int strLen = strlen(str), blankCount = 0;
        for (int i = 0; i < strLen; i++) {
            if (str[i] == ' ') {
                blankCount++;
            }
        }
        int newStrLen = strLen + 2 * blankCount;
        if (newStrLen > length) {
            return;
        }
        int i = strLen, j = newStrLen;
        while (i >= 0 && j > i) {
            if (str[i] != ' ') {
                str[j--] = str[i--];
            } else {
                str[j--] = '0';
                str[j--] = '2';
                str[j--] = '%';
                i--;
            }
        }
        return;
	}
};

有两点需要注意:

  1. C风格的字符串是以’\0’结尾的,这个必须手动复制;
  2. 当后指针的位置追上前指针的位置时,证明原字符串中所有的空格已经被填满了,再往前就没有空格了,也就不必再逐个字符复制了。
    Java版答案1:
    利用自带的replace函数,确实省劲啊~~~
public class Solution {
    public String replaceSpace(StringBuffer str) {
        int index = -1;
        while ((index = str.indexOf(" ")) != -1) {
            str = str.replace(index, index + 1, "%20");
        }
        return str.toString();
    }
}

Java版答案2:

public class Solution {
    public String replaceSpace(StringBuffer str) {
        int spacenum = 0;//spacenum为计算空格数
        for(int i=0;i<str.length();i++){
            if(str.charAt(i)==' ')
                spacenum++;
        }
        int indexold = str.length()-1; //indexold为为替换前的str下标
        int newlength = str.length() + spacenum*2;//计算空格转换成%20之后的str长度
        int indexnew = newlength-1;//indexold为为把空格替换为%20后的str下标
        str.setLength(newlength);//使str的长度扩大到转换成%20之后的长度,防止下标越界
        for(;indexold>=0 && indexold<newlength;--indexold){ 
                if(str.charAt(indexold) == ' '){  //
                str.setCharAt(indexnew--, '0');
                str.setCharAt(indexnew--, '2');
                str.setCharAt(indexnew--, '%');
                }else{
                    str.setCharAt(indexnew--, str.charAt(indexold));
                }
        }
        return str.toString();
    }
}

重点要熟悉StringBuffer的一些函数使用方法!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值