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

  • 相关阅读:
    Codeforces Global Round 2
    BZOJ4762 最小集合(动态规划+容斥原理)
    BZOJ4621 Tc605(动态规划)
    Luogu5289 十二省联考2019皮配(动态规划)
    Luogu5290 十二省联考2019春节十二响(贪心+启发式合并)
    Luogu5283 十二省联考2019异或粽子(trie/可持久化trie+堆)
    Luogu5284 十二省联考2019字符串问题(后缀数组+拓扑排序+线段树/主席树/KDTree)
    【转】Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask
    android Handler更新UI
    Android 四大组件之Activity生命周期
  • 原文地址:https://www.cnblogs.com/whuyt/p/4814524.html
Copyright © 2011-2022 走看看