【待】1.4 Write a method to replace all spaces in a string with'%20'.

Write a method to replace all spaces in a string with'%20'. You may assume that the string has sufficient space at the end of the string to hold the dditional
characters, and that you are given the "true" length of the string. (Note: if implementing in Java, please use a character array so that you can perform this operation in place.)

EXAMPLE
Input: "Mr John Smith"
Output: "Mr%20Dohn%20Smith"

【初步思路】:

新建一个字符数组,大小为原字符串的三倍, 然后遍历原字符串,将相应元素置入数组(如果是空格则用%20代替)

暂时没想到 in-place 的方法。

【时间复杂度】:O(n)

【空间复杂度】:O(n)

public char[] solu(char[] s) {
        char[] result = new char[s.length * 3];
        for (int i = 0, j = 0; i < s.length; i++) {
            int ascii = s[i];
            if (ascii == 32 ) {
                result[j++] = '%';
                result[j++] = '2';
                result[j++] = '0';
            } else {
                result[j] = s[i];
                j++;
            }
        }
        return result;
    }

 

【优化】:in-place 方法。

先遍历一遍数组(原数组真实长度设为 len), 求得空格个数 count。则修改后数组长度应为 len+2*count。然后采用两个指针,从右至左遍历数组,不是空格则拷贝置新位置,是空格则修改为 "%20".

【时间复杂度】:O(n)

【空间复杂度】:O(1)

public class Solution {
    
    public static int solu(char[] s, int len) {
        int count = 0;
        for (int i = 0; i < len; i++) {
            if (s[i] == ' ') {
                count++;
                System.out.println(count);
            }
        }
        int oldPointer = len - 1;
        int newPointer = len + 2 * count - 1;
        while(oldPointer != newPointer) {
            if (s[oldPointer] == ' ') {
                s[newPointer--] = '0';
                s[newPointer--] = '2';
                s[newPointer--] = '%';
                oldPointer--;
            } else {
                s[newPointer--] = s[oldPointer--];
            }
        }
        return count;  // useful for test; 
    }
    
}

附上 test code

import static org.junit.Assert.*;

import org.junit.Test;

public class SolutionTest {

    @Test
    public void testCount() {
        char[] s1 = helper("");
        char[] s2 = helper("ab");
        char[] s3 = helper(" ");
        char[] s4 = helper(" a");
        assertTrue(Solution.solu(s1, 0) == 0);
        assertTrue(Solution.solu(s2, 2) == 0);
        assertTrue(Solution.solu(s3, 1) == 1);
        assertTrue(Solution.solu(s4, 2) == 1);
        char[] s_1 = {};
        char[] s_2 = {'a', 'b'};
        char[] s_3 = {'%', '2', '0'};
        char[] s_4 = {'%', '2', '0', 'a'};
        assertTrue(equal(s1, s_1));
        assertTrue(equal(s2, s_2));
        assertTrue(equal(s3, s_3));
        assertTrue(equal(s4, s_4));
    }
    
    public static boolean equal(char[] s1, char[] s2) {
        for (int i = 0; i < s2.length; i++) {
            if (s1[i] != s2[i]) {
                return false;
            }
        }
        return true;
    }
    
    public static char[] helper(String s) {
        char[] c = new char[100];
        for (int i = 0; i < s.length(); i++) {
            c[i] = s.charAt(i);
        }
        return c;
    }
    
}

 

 

 

2015-09-16

转载于:https://www.cnblogs.com/whuyt/p/4814524.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值