URL化

{URL化}https://leetcode-cn.com/problems/string-to-url-lcci/submissions/

  1. 我第一次做法

    public String replaceSpaces(String s, int length) {
            StringBuilder result = new StringBuilder();
            char[] charArr = s.toCharArray();
            for (int i = 0; i < length; i++) {
                if (charArr[i] == ' ') {
                    result.append("%20");
                } else {
                    result.append(charArr[i]);
                }
            }
            return result.toString();
        }
    

    Fail - 超时了

  2. 参考评论区做法

    发现自己没理解对,题目的本意是用字符数组来解答

     public String replaceSpaces1(String s, int length) {
            char[] chars = s.toCharArray();
            int i = length - 1;
            int j = chars.length - 1;
    
            while (i >= 0) {
                if (chars[i] == ' ') {
                    chars[j--] = '0';
                    chars[j--] = '2';
                    chars[j--] = '%';
                } else {
                    chars[j--] = chars[i];
                }
                --i;
            }
    
            return String.valueOf(chars, j + 1, chars.length - 1 - j);
        }
    
  3. 类似于解法2

    public String replaceSpace2(String s, int length) {
            char[] chars = s.toCharArray();
            char[] result = new char[s.length()];
            int newIndex = 0;
            for (int i = 0; i < length; i++) {
                if (chars[i] == ' ') {
                    result[newIndex++] = '%';
                    result[newIndex++] = '2';
                    result[newIndex++] = '0';
                }else {
                    result[newIndex++] = chars[i];
                }
            }
            return String.valueOf(result, 0, newIndex);
        }
    

    但是内存消耗比较多

  4. 优化2的内存使用

        /**
         * 减少了一次字符数组的copy
         */
     public String replaceSpace3(String s, int length) {
            char[] result = new char[s.length()];
            int newIndex = 0;
            for (int i = 0; i < length; i++) {
                if (s.charAt(i) == ' ') {
                    result[newIndex++] = '%';
                    result[newIndex++] = '2';
                    result[newIndex++] = '0';
                }else {
                    result[newIndex++] = s.charAt(i);
                }
            }
            return String.valueOf(result, 0, newIndex);
        }
    
  5. 在4的基础上,寻找优化空间

       /**
         * while 替换 for 发现内存使用少了点
         */
        public String replaceSpace4(String s, int length) {
            char[] result = new char[s.length()];
            int newIndex = 0;
            int i = 0;
            while (i < length){
                if (s.charAt(i) == ' ') {
                    result[newIndex++] = '%';
                    result[newIndex++] = '2';
                    result[newIndex++] = '0';
                }else {
                    result[newIndex++] = s.charAt(i);
                }
    
                ++i;
            }
            return String.valueOf(result, 0, newIndex);
        }
    

你可能感兴趣的:(URL化)