T3- replace spaces

Title Description

Please implement a function, a string to replace each space to "20%." For example, when the string is We Are Happy. After the string is replaced after We% 20Are% 20Happy.

Time limit: C / C ++ 1 second, 2 seconds languages other
space restrictions: C / C ++ 32M, 64M languages other
heat index: 1309646

analysis of idea

I encountered a similar topic at the beginning of learning c programming, at that time, which did not consider the extra space overhead, direct idea is:

  1. Open up a new array of strings, then the original string array to traverse from start to finish;
  2. If a space character is not copied to the new string, if the space is not copied and inserted in the new string 20%;
  3. Cycle, until the original string been traversed;

Above ideas, not specifically described herein. We should explore a more efficient algorithm for string replacement.

With "20%" to replace the spaces in the string, character has changed, the length becomes. If you do not open up a new string is simply involves two key points: mobile and replaced. Further, to ensure no mistakes string replacement,Operation must move before the replacement operation

At first, I thought about the problem, I would think: front to back traversal strings, encountered a space, put the space character moved back behind, then replaced in the blanks. Although this can solve the problem, but each time it encounters a space had to move the string so that the operation becomes cumbersome, affecting efficiency.
So, I think,Whether the one-time all shift operation is completed in place? You know, where each of the replacement rules are fixed, the resulting string is yes, then the answer to this question is yes of course.

Problem-solving ideas

First of all, before and after the replacement were analyzed:
Here Insert Picture Description

Step 1: Determine the shift operation, the number of bits of each character movement.

  • Every Replace: "20%" -> "" (a three character substitution), an increase of two string units; in other words, a space, an increase of two string units;
  • To determine the final length of the string, the string will need to record the total number of spaces;
  • To determine the number of bits per character to move backwards, you need to record the number of spaces in front of each character;

The second step: to solve two key questions above

  • "The total number of spaces in the string record": traverse the entire string, with variable count ++ way to record the number of spaces;
  • "The record number of spaces before each character": Since the total number of spaces in the string and determined, the last character of the movement of digits after the space should be 2 x count, the space between the penultimate and last space moving the character digit is 2 x (count-1) ... and so on, until the count = 0;
  • Based on the above rules, it is determined during our Alternatively, traversal of the string should be back to front;
  • String move is completed, replace the vacated space is then a simple replacement operation can be performed;

See detailed process of the algorithm code portion;

Code

class Solution {
public:
    void replaceSpace(char *str,int length) {
        int count=0;
        //首先,从前往后遍历字符串,用count记录字符串中的空格数
        for(int i=0;i<length;i++){
            if(str[i]==' ')
                count++;
        }	
        //从后往前反向遍历
        //移位每个非空格字符,给空格的替换腾出空间,移动的位数为2*count
        for(int i=length-1;i>=0;i--){	
            if(str[i]!=' '){
                str[i+2*count]=str[i];
            }	
            else{	
            	//遇到空格直接开始替换,注意这里逻辑顺序,参考前文图示,弄清为什么要先count--
                count--;
                str[i+2*count]='%';
                str[i+2*count+1]='2';
                str[i+2*count+2]='0';
            }
        }
    }
};
Published 24 original articles · won praise 0 · Views 2054

Guess you like

Origin blog.csdn.net/weixin_44849403/article/details/104062802