[Offer] algorithm to prove safety problem 05. replace the space (C ++)

Please implement a function, replace each space in the string s to "20%."

[ Example 1 ]

Input: s = "We are happy. "
Output: "We% 20are% 20happy. "

[ Limit ]

Length 0 <= s of <= 10000

 

[Problem-solving ideas]

class Solution {
public:
  string replaceSpace(string s) {
    string s2;
      for(auto c:s){
      if(s[i]!=' ')
        s2 += s[i];
      else
        s2 += "%20";
    }
    return s2;
  }
};

[ Note]

1. C ++ the single and double quotes problem.

It is a single quote character, double-quoted string is
a single character in quotation marks actually represent an integer.
String enclosed in double quotes, it is representative of a pointer to an array of unknown starting character. The array is double-quote character between the characters and an additional binary zero '\ 0' initialization.

the difference between "a" and 'a', the former is a string that is a character.
In fact "a" is "a \ 0", the '\ 0' end. The 'a' this alone represents a character.
The string can be "abcde" a combination of such represent multiple characters, but 'abcde' this is wrong! ! !

In summary, usually a single character string is combined with the characters, the plurality of characters ~

2. About for (auto c: s) the wording.

C11 special format standard can be performed in a for loop, except that the reference type may be changed to the original value

-------------------------------------------------- ------------
Auto: used to declare an automatic variable. It is a storage type identifier, that the variable (auto) having a local scope, range block variable declaration (e.g., for the loop body variable declaration) is auto default storage type. In fact, most ordinary declarative statement variables are auto variables, they do not need to explicitly specify the auto keyword, the default is the auto. auto variables are out of scope become automatically released, the memory overflow (except class contains a pointer) does not occur. The advantage of using auto variables is not necessary to consider whether to go variable is released, relatively safe.
new: new keyword is used in the application heap memory address, he produced variable does not automatically release, unless delete manually release, or end the program when released by the operating system, the advantages of using new memory usage is more flexible, theory can apply for any size memory block when (the actual operating system dependent), but it is prone to problems, accidentally forgetting to release the object, the object is especially frequent in the function call created forgetting to release, it will produce memory overflow , leading to serious program error, system crash. new are generally used in a class definition, the combination can delete class contains new objects out also comes with variable function, thus inherited the advantages of the two approaches.
-------------------------------------------------- ------------
in addition, the latest C ++ standard auto update function keys in addition to the original meaning, but also adds a high-level language similar to other type do not use auto to deduce properties Instead of a variable type, the premise is initialized explicitly initialized variable type, you can use the auto keyword such as int i = 10; auto a = i; // this is a type int in using some of this template class, for reduce verbose code is also useful
-------------------------------------------------- -------------
another digression: auto corresponding type instead of using the new out of the variables, nor static variable
static variable is the end of the program when it releases the object, but it does not require manual freed.
When static if declared in a function, every time it entered this function, or the use of variable is first declared, and also the value of the last used saved.
If you use static variables and class structure, which structure or all object class definition, we will share a unique static variables.
When auto variable that is released at the end of the function, and this function is called again, and redefines a new variable

 

Guess you like

Origin www.cnblogs.com/ziziQ/p/12509732.html