剑指offer面试题5 字符串 替换空格

package com.cloud.algorithm.demo;

import org.junit.Test;

import java.util.Arrays;

public class Topic5 {

    /**
     * 字符串 替换空格
     * 请实现一个函数,把字符串中的每个空格替换成"%20",
     * 例如输入"we are happy",则输出"we%20are%20happy"
     */
    @Test
    public void topic5_1() {
        /**
         * 先统计有多少空格,总共需要多少长度,从字符串尾部依次复制进新的字符串中
         */
        String strOriginal = "we 12 3 are happy";
        //转成Stringbuffer 此处为在Stringbuffer里面移动,也可以新建一个Stringbuffer
        //此处用StringBuffer演示,String不能被改动
        StringBuffer str = new StringBuffer(strOriginal);
        String s = this.demo01(str);
        System.out.println("-----" + s);

    }

    /**
     * 有两个排序的数组A1和A2,内存在A1的末尾有足够的多余空间容纳A2,
     * 请实现一个函数,把A2中所有数字插入A1中,并且所有的数字都是排序的
     */
    @Test
    public void topic5_2() {
        int[] aa = new int[10];
        aa[0] = 2;
        aa[1] = 3;
        aa[2] = 6;
        aa[3] = 7;
        int[] bb = {4, 5, 7, 8, 13};

        int[] ints = this.demo02(aa, 4, bb, 5);
        System.out.println(Arrays.toString(ints));

    }

    private String demo01(StringBuffer str) {
        //计算原有字符串长度
        int originalLength = str.length();
        //计算空格的长度
        int blankNum = 0;
        for (int i = 0; i < originalLength; i++) {
            if (str.charAt(i) == ' ') {
                blankNum++;
            }
        }
        //替换之后的字符串长度
        int newLength = originalLength + (blankNum << 1);
        str.setLength(newLength);
        int newIdx = newLength - 1;
        int originalIdx = originalLength - 1;
        for (int i = originalIdx; i >= 0; i--) {
            if (str.charAt(i) != ' ') {
                char c = str.charAt(i);
                str.setCharAt(newIdx--, c);
            } else {
                str.setCharAt(newIdx--, '0');
                str.setCharAt(newIdx--, '2');
                str.setCharAt(newIdx--, '%');
            }
        }
        return str.toString();
    }

    /**
     * @param aa
     * @param len1 aa中测试数据长度
     * @param bb
     * @param len2 bb 数组长度
     * @return
     */
    private int[] demo02(int[] aa, int len1, int[] bb, int len2) {
        int aaLen = aa.length;
        int allLen = len1 + len2;
        int idx1 = len1 - 1;
        int idx2 = len2 - 1;
        int idxAll = allLen - 1;
        //保证aa的长度大于合并之后的长度
        if (aaLen >= allLen) {
            //比较aa(测试的前n个数)和bb最末尾的数字谁大,大的放在最后,长度减1,总放置的长度也减1
            while (idx1 >=0 && idx2 >= 0) {
                if (aa[idx1] >= bb[idx2]) {
                    aa[idxAll--] = aa[idx1--];
                } else {
                    aa[idxAll--] = bb[idx2--];
                }
            }
            //如果bb还有剩余,从后往前全部放进aa中,  如果aa还有剩余不需要处理
            while (idx2 >= 0) {
                aa[idxAll--] = bb[idx2--];
            }
        }
        return aa;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值