【剑指offer】替换空格

替换空格

http://www.nowcoder.com/questionTerminal/4060ac7e3e404ad1a894ef3e17650423

  1. 直接调用函数和new StringBuffer然后append 简直没谁了...
public class Solution {
    public static String replaceSpace(StringBuffer str) {
        if (str == null) {
            return null;
        }

        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == ' ') {
                count++;
            }
        }

        int l = str.length() - 1;
        int r = l + count * 2;
        str.setLength(r + 1);

        while (l >= 0) {
            if (str.charAt(l) != ' ') {
                str.setCharAt(r, str.charAt(l));
                r--;
            } else {
                str.replace(r - 2, r + 1, "%20");
                r -= 3;
            }
            l--;
        }


        return str.toString();
    }
}
全部评论
牛客这题应该就给个字符数组
点赞 回复 分享
发布于 2019-09-23 23:54
这不一样调用了replace方法么,而且还循环了两次
点赞 回复 分享
发布于 2019-09-17 22:19

相关推荐

09-22 22:22
中山大学 Java
双尔:赌对了,不用经历秋招的炼狱真的太好了,羡慕了
点赞 评论 收藏
分享
评论
5
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务