Skip to content

Latest commit

 

History

History
147 lines (106 loc) · 5.31 KB

02.字符串替换空格.md

File metadata and controls

147 lines (106 loc) · 5.31 KB

目录介绍

  • 01.题目要求
  • 02.问题分析
  • 03.实例代码

好消息

  • 博客笔记大汇总【15年10月到至今】,包括Java基础及深入知识点,Android技术博客,Python学习笔记等等,还包括平时开发中遇到的bug汇总,当然也在工作之余收集了大量的面试题,长期更新维护并且修正,持续完善……开源的文件是markdown格式的!同时也开源了生活博客,从12年起,积累共计N篇[近100万字,陆续搬到网上],转载请注明出处,谢谢!所有博客陆续更新到GitHub上!
  • 链接地址:https://github.com/yangchong211/YCBlogs
  • 如果觉得好,可以star一下,谢谢!当然也欢迎提出建议,万事起于忽微,量变引起质变!

01.题目要求

  • 问题如下所示:
    • 请实现一个函数,把字符串中的每个空格替换成"%20"
  • 示例 :
    • 例如“We are happy.”,则输出“We%20are%20happy.”。

02.问题分析

  • 先判断字符串中空格的数量。根据数量判断该字符串有没有足够的空间替换成"%20"。
  • 如果有足够空间,计算出需要的空间。根据最终需要的总空间,维护一个指针在最后。从后到前,遇到非空的就把该值挪到指针指向的位置,然后指针向前一位,遇到“ ”,则指针前移,依次替换为“02%”。

03.实例代码

  • 第一种解决代码
    public static String replace(String str) {
        int length = str.length();
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < length; i++) {
            char b = str.charAt(i);
            if (String.valueOf(b).equals(" ")) {
                result.append("%20");
            } else {
                result.append(b);
            }
        }
        return result.toString();
    
    }
    
    public static String replace(StringBuffer str) {
        return str.toString().replaceAll("\\s", "%20");
    }
    
  • 第二种解决代码
    public class Test {  
        /** 
         * 请实现一个函数,把字符串中的每个空格替换成"%20",例如“We are happy.“,则输出”We%20are%20happy.“。 
         * 
         * @param string     要转换的字符数组 
         * @param usedLength 已经字符数组中已经使用的长度 
         * @return 转换后使用的字符长度,-1表示处理失败 
         */  
        public static int replaceBlank(char[] string, int usedLength) {  
            // 判断输入是否合法  
            if (string == null || string.length < usedLength) {  
                return -1;  
            }  
      
            // 统计字符数组中的空白字符数  
            int whiteCount = 0;  
            for (int i = 0; i < usedLength; i++) {  
                if (string[i] == ' ') {  
                    whiteCount++;  
                }  
            }  
      
            // 计算转换后的字符长度是多少  
            int targetLength = whiteCount * 2 + usedLength;  
            int tmp = targetLength; // 保存长度结果用于返回  
            if (targetLength > string.length) { // 如果转换后的长度大于数组的最大长度,直接返回失败  
                return -1;  
            }  
      
            // 如果没有空白字符就不用处理  
            if (whiteCount == 0) {  
                return usedLength;  
            }  
      
            usedLength--; // 从后向前,第一个开始处理的字符  
            targetLength--; // 处理后的字符放置的位置  
      
            // 字符中有空白字符,一直处理到所有的空白字符处理完  
            while (usedLength >= 0 && usedLength < targetLength) {  
                // 如是当前字符是空白字符,进行"%20"替换  
                if (string[usedLength] == ' ') {  
                    string[targetLength--] = '0';  
                    string[targetLength--] = '2';  
                    string[targetLength--] = '%';  
                } else { // 否则移动字符  
                    string[targetLength--] = string[usedLength];  
                }  
                usedLength--;  
            }  
      
            return tmp;  
        }  
    }  
    

其他内容

01.关于博客汇总链接

02.关于我的博客