剑指offer 05. 替换空格

剑指offer 05. 替换空格

请实现一个函数,把字符串 s 中的每个空格替换成"%20"。

示例 1:
输入:s = “We are happy.”
输出:“We%20are%20happy.”

限制:
0 <= s 的长度 <= 10000

Python代码实现

Python代码1:

class Solution:
    def replaceSpace(self, s: str) -> str:
        s = list(s) # 把字符串装换成字符串数组
        n = len(s)  # 求字符串数组的长度
        for i in range(n): # 循环遍历字符串数组
            if s[i] == ' ': # 如果遇到空字符,则替换
                s[i] = '%20'

        s = ''.join(s) # 使用了Python的join函数
        return s
# 测试用例
a = Solution() # 生成对象
print(a.replaceSpace('We are happy.')) #使用类方法

Python代码实现2:

class Solution:
    def replaceSpace(self, s: str) -> str:
    	# 直接使用Python的replace函数
        return s.replace(" ","%20")
# 测试用例
a = Solution() # 生成对象
print(a.replaceSpace('We are happy.')) #使用类方法

Java代码实现

Java代码实现1:

import java.lang.String;

public class LeetReplaceSpace {
	public String replaceSpace(String s) {
        int length = s.length();// 求字符串长度
        // 计算字符串中空字符的个数
        int count = 0;
        for(int i = 0;i<s.length();i++){
            if(s.charAt(i)==' '){
                count++;
            }
        }
        char[] array = new char[length + count * 2];// 创建新的字符串数组
        int size = 0; // size 表示替换字符字后的字符数组的长度,初始值为0
        for (int i = 0; i < length; i++) {
            char c = s.charAt(i);// 求下标为i的字符
            if (c == ' ') {
                array[size++] = '%';
                array[size++] = '2';
                array[size++] = '0';
            } else {
                array[size++] = c;
            }
        }
        String newStr = new String(array, 0, size);//生成0到size的新字符串
        return newStr;
    }
    public static void main(String[] args){
        LeetReplaceSpace str1 = new LeetReplaceSpace();
        String str2 = "this is a book.";
        System.out.println(str1.replaceSpace(str2));
    }

}

Java代码实现2:

import java.lang.String;
public class LeetReplaceSpace1 {
    public String replaceSpace(String s) {
        StringBuilder sb = new StringBuilder();
        for(int i = 0 ; i < s.length(); i++){
            char c = s.charAt(i);
            if(c == ' ') sb.append("%20");
            else sb.append(c);
        }
        return sb.toString();
    }
    public static void main(String[] args){
        LeetReplaceSpace1 str1 = new LeetReplaceSpace1();
        String str2 = "this is a book.";
        System.out.println(str1.replaceSpace(str2));
    }

}

Java代码实现3:

public class LeetReplaceSpace {
	public String replaceSpace(String s) {
        int count = 0;
        for(int i = 0;i<s.length();i++){
            if(s.charAt(i)==' '){
                count++;
            }
        }
        char[] a = new char[s.length()+count*2];
        int m = a.length-1;
        int n = s.length()-1;
        while(m>-1&&n>-1){
            if(s.charAt(n)==' '){
                a[m] = '0';
                a[m-1] = '2';
                a[m-2] = '%';
                m-=3;
                n--;
                continue;
            }else{
            a[m] = s.charAt(n);}
            m--;
            n--;
        }
        return String.valueOf(a);
    }
    public static void main(String[] args){
        LeetReplaceSpace str1 = new LeetReplaceSpace();
        String str2 = "this is a book.";
        System.out.println(str1.replaceSpace(str2));
    }

}

你可能感兴趣的:(数据结构与算法,字符串,python,java,leetcode,算法)