zoukankan      html  css  js  c++  java
  • 【待】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

  • 相关阅读:
    Centos7 搭建FTP服务
    Mitmproxy 安装
    NET Reflector 8 使用
    web sevice 生成代理类及使用
    WCF 动态生成 不用增加引用两种方式
    oracle 表空间、用户名 相关语句
    恢复24小时之内删除的表及表数据
    pl/sql插入报错
    jquer ajax
    pdf增加水印
  • 原文地址:https://www.cnblogs.com/whuyt/p/4814524.html
Copyright © 2011-2022 走看看