• we are happy 把空格换成 %20 剑指offer P44


    public class StringReplace {
        public static void replaceSpace(String[] str, int length) {
            if(str == null || length <= 0) {
                return;
            }
            int originalLength = 0;
            int numberOfBlank = 0;
            int i = 0;
            while(str[i] != null) {
                originalLength ++;
                if(str[i] == " ") {
                    numberOfBlank ++;
                }
                i++;
            }
            //新的字符串长度
            int newLength = originalLength + 2 * numberOfBlank;
            if(newLength > length) {//不能超出字符数组的长度
                return;
            }
            int originalIndex = originalLength-1;
            int newIndex = newLength-1;
            while(originalIndex >=0 && originalIndex < newIndex) {
                if(str[originalIndex] == " ") {
                    str[newIndex--] = "0";
                    str[newIndex--] = "2";
                    str[newIndex--] = "%";
                    originalIndex --;
                }else {
                    str[newIndex--] = str[originalIndex--];
                }
                
            }
            
        }
        
        public static void main(String[] args) {
            String[] temp = {"we", " ","are", " ", "happy",null,null,null,null,null,null,null,null };
            replaceSpace(temp,13);
            for(int i=0; i< temp.length; i ++) {
                System.out.print(temp[i]);
            }
        }
    
    }
  • 相关阅读:
    [Postman]历史(8)
    [Postman]响应(7)
    [Postman]请求(6)
    [Postman]查找替换(5)
    ORA-02050故障诊断一例
    转 js实践篇:例外处理Try{}catch(e){}
    转 PHP
    HTML DOM getElementById() 方法
    地点选择
    9i 和 11 g 区别
  • 原文地址:https://www.cnblogs.com/leehfly/p/5228630.html
Copyright © 2020-2023  润新知