资讯专栏INFORMATION COLUMN

[LintCode] Space Replacement

13651657101 / 2414人阅读

摘要:,这里要注意循环的是前个元素,不是当前元素在循环条件里前移,新位置的指针在语句前移序数从后向前没有空格也要把当前位元素移到新的位置

Problem

Write a method to replace all spaces in a string with %20. The string is given in a characters array, you can assume it has enough space for replacement and you are given the true length of the string.

You code should also return the new length of the string after replacement.

Example

Given "Mr John Smith", length = 13.

The string after replacement should be "Mr%20John%20Smith".

Note

If you are using Java or Python,please use characters array instead of string.

Challenge

Do it in-place.

Solution

It"s a easy problem but you are required to do it in-place.
No StringBuilder.

public class Solution {
    public int replaceBlank(char[] str, int length) {
        if (length == 0) return 0;
        int len = length;
        //calculate the new length after all the replacements
        for (int i = 0; i < len; i++) {
            if (str[i] == " ") {
                len += 2;
            }
        }
        int j = 1;
        //这里要注意:循环的是前length-1个元素,不是len-1
        for (int i = length - 1; i >= 0; i--) {
            //当前元素i--在for循环条件里前移,新位置的指针(len - j++)在if-else语句前移
            if (str[i] == " ") {
                //序数从后向前 len-1 len-2 len-3
                str[len - j++] = "0";
                str[len - j++] = "2";
                str[len - j++] = "%";
            }
            //没有空格也要把当前位元素移到新的位置 
            else {
                str[len - j++] = str[i];
            }
        }
        return len;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65507.html

相关文章

  • [LintCode/LeetCode] Integer Replacement

    Problem Given a positive integer n and you can do operations as follow: 1.If n is even, replace n with n/2.2.If n is odd, you can replace n with either n + 1 or n - 1. What is the minimum number of re...

    fyber 评论0 收藏0
  • [LintCode/LeetCode] Edit Distance

    摘要:构造数组,是的,是的,是将位的转换成位的需要的步数。初始化和为到它们各自的距离,然后两次循环和即可。 Problem Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 s...

    snowell 评论0 收藏0
  • [LintCode] Next Permutation II [Next Permutation]

    摘要:从末位向前遍历,假设循环开始全是倒序排列,如当第一次出现正序的时候,如的和此时从数列末尾向前循环到,找到第一个比大的交换这两个数,变成倒置第位到末位的数为正序排列这里的是完全倒置的排列,如,即上面循环的情况完全没有出现, Problem Implement next permutation, which rearranges numbers into the lexicographic...

    mikasa 评论0 收藏0
  • [LintCode/LeetCode/CC] Set Matrix Zeroes

    摘要:把矩阵所有零点的行和列都置零,要求不要额外的空间。对于首行和首列的零点,进行额外的标记即可。这道题我自己做了四遍,下面几个问题需要格外注意标记首行和首列时,从到遍历时,若有零点,则首列标记为从到遍历,若有零点,则首行标记为。 Problem Given a m x n matrix, if an element is 0, set its entire row and column t...

    zhangwang 评论0 收藏0
  • [LintCode] Longest Increasing Continuous Subseque

    摘要:最长连续递增递减子序列,设置正向计数器,后一位增加则计数器加,否则置。反向计数器亦然。每一次比较后将较大值存入。 Problem 最长连续递增/递减子序列 Give an integer array,find the longest increasing continuous subsequence in this array.An increasing continuous subs...

    wwq0327 评论0 收藏0

发表评论

0条评论

13651657101

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<