成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LintCode] Space Replacement

13651657101 / 2424人閱讀

摘要:,這里要注意循環(huán)的是前個元素,不是當前元素在循環(huán)條件里前移,新位置的指針在語句前移序數(shù)從后向前沒有空格也要把當前位元素移到新的位置

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;
        //這里要注意:循環(huán)的是前l(fā)ength-1個元素,不是len-1
        for (int i = length - 1; i >= 0; i--) {
            //當前元素i--在for循環(huán)條件里前移,新位置的指針(len - j++)在if-else語句前移
            if (str[i] == " ") {
                //序數(shù)從后向前 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;
    }
}

文章版權歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.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

    摘要:構(gòu)造數(shù)組,是的,是的,是將位的轉(zhuǎn)換成位的需要的步數(shù)。初始化和為到它們各自的距離,然后兩次循環(huán)和即可。 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]

    摘要:從末位向前遍歷,假設循環(huán)開始全是倒序排列,如當?shù)谝淮纬霈F(xiàn)正序的時候,如的和此時從數(shù)列末尾向前循環(huán)到,找到第一個比大的交換這兩個數(shù),變成倒置第位到末位的數(shù)為正序排列這里的是完全倒置的排列,如,即上面循環(huán)的情況完全沒有出現(xiàn), 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

    摘要:最長連續(xù)遞增遞減子序列,設置正向計數(shù)器,后一位增加則計數(shù)器加,否則置。反向計數(shù)器亦然。每一次比較后將較大值存入。 Problem 最長連續(xù)遞增/遞減子序列 Give an integer array,find the longest increasing continuous subsequence in this array.An increasing continuous subs...

    wwq0327 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<