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

  • 相关阅读:
    JavaScript系列:《JavaScript高级程序设计》,chapter2, 在html中使用JavaScript
    Java系列:JVM指令详解(下)(zz)
    Java系列:JVM指令详解(上)(zz)
    Java系列:关于Java中的桥接方法
    REST: C#调用REST API (zz)
    Activiti系列:为什么Activiti 5.18 的REST的api总是返回404错误
    timeSeries db之:使用Metrics监控应用程序的性能 (zz)
    Java系列:国际化(zz)
    通过数据库方式访问excel 2007及其以后(xlsx)文件的连接字符串
    java系列:《java核心技术 卷1》学习笔记,chapter 11 调试技巧
  • 原文地址:https://www.cnblogs.com/whuyt/p/4814524.html
Copyright © 2011-2022 走看看