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

第一种方法用StringBuffer

package question5_replace_blank;

import question3_find_repeat_number.solution1;

/**
 * @Classname Solution1
 * @Description 遍历字符创使用StringBuffer
 * @Date 2020/3/7 13:27
 * @Created by mmz
 */
public class Solution1 {

    public static String replaceBlank(String str){
        //创建一个StringBuffer
        if(str == null || str.length() < 0){
            return null;
        }
        StringBuffer stringBuffer = new StringBuffer();
        //循环遍历添加
        for(int i = 0 ; i <str.length();++i){
            if(str.charAt(i) == ' '){
                stringBuffer.append("%20");
            }else{
                stringBuffer.append(str.charAt(i));
            }
        }
        return stringBuffer.toString();
    }

    public static void main(String args[]) {
        /**
         * 测试用例
         */
        String str1 = "We are happy";
        String str2 = " Wearehappy.";
        String str3 = "Wearehappy. ";
        String str4 = "We   are   happy  .";
        String str5 = "Wearehappy.";
        String str6 = "    ";
        String str7 = " ";
        String str8 = null;
        String str9 = "";
        String string = replaceBlank(str9);
        System.out.println(string);
    }

}

第二种方法
用到的就是剑指offer上面的双指针方法

package question5_replace_blank;

/**
 * @Classname Solution2
 * @Description TODO
 * @Date 2020/3/7 13:45
 * @Created by mmz
 */
public class Solution2 {
    public static String findBlank(char[] chars){
        int count = 0;
        for(int i = 0 ;i<chars.length ; ++i){
            if(chars[i] == ' '){
                ++count;
            }
        }
        int newlength = chars.length + count*2;
        int i = chars.length-1;
        int j = newlength -1;
        char[] c = new char[newlength];
        while(i>=0 && j>=i){
            if(chars[i] == ' '){
                c[j--] = '0';
                c[j--] = '2';
                c[j--] = '%';
            }else{
                c[j--] = chars[i];
            }
            i--;
        }
        return c.toString();
    }

    public static void main(String args[]) {
        /**
         * 测试用例
         */
        String str1 = "w e";
        String str2 = " Wearehappy.";
        String str3 = "Wearehappy. ";
        String str4 = "We   are   happy  .";
        String str5 = "Wearehappy.";
        String str6 = "    ";
        String str7 = " ";
        String str8 = null;
        String str9 = "";
        String string = findBlank(str1.toCharArray());
        System.out.println(string);
    }
}

问题来了,java中数组不能改变大小,那就相当于新的数组一定开辟了空间。
用到的函数
把一个字符串变成字符串数组String.toCharArray()


二刷
直接做出来了。

package question5_replace_blank;

/**
 * @Classname Solution3
 * @Description TODO
 * @Date 2020/3/31 14:50
 * @Created by mmz
 */
public class Solution3 {
    static String repalceBlank(String string){
        char[] chars = string.toCharArray();
        int count = 0;
        for(int i = 0;i<chars.length;++i){
            if(chars[i] == ' '){
                count++;
            }
        }
        int newlength = count*2+chars.length;
        char charnew[] = new char[newlength];
        int j = charnew.length-1;
        for(int i =chars.length-1;i>=0;--i){
            if(chars[i] != ' '){
                charnew[j--] = chars[i];
            }else{
                charnew[j--] ='0';
                charnew[j--] = '2';
                charnew[j--] = '%';
            }
        }
        return new String(charnew);
    }

    public static void main(String[] args) {
        String string = repalceBlank("We are happy.");
        System.out.println(string);
    }
}

记住,如果有char的数组,直接可以用构造函数转字符串。


package question5_替换空格;

/**
 * @Classname Main
 * @Description TODO
 * @Date 2020/4/11 15:06
 * @Created by mmz
 */
public class Main {
    public static String Core(String str){
        str = str.replace(" ","%20");
        System.out.println(str);
        return str;
    }

    public static void main(String[] args) {
        Core("We are happy");
    }
}

string里面直接有replace方法,可以直接转换。



三刷 20200718 ```java package question5_替换空格;

/**

  • @Classname Mmz

  • @Description TODO

  • @Date 2020/7/18 14:18

  • @Created by mmz
    /
    public class Mmz {
    public static void Core(String string){
    char[] chars = string.toCharArray();
    int count = 0 ;
    for(char x:chars){
    if(x == ’ '){
    count++;
    }
    }
    char[] newchar = new char[chars.length+count
    2];
    int j = 0 ;
    for(int i = 0;i<chars.length;++i){
    if(chars[i] == ’ ‘){
    newchar[j++] =’%’;
    newchar[j++] =‘2’;
    newchar[j++] =‘0’;
    }else{
    newchar[j++]=chars[i];
    }
    }
    String strings = new String(newchar);
    System.out.println(strings);

    }
    public static void main(String[] args) {
    Core("We are happy ");
    }
    }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值