【字符串】面试题 01.03. URL化

题目:

 

 

解答:

 1 class Solution {
 2 public:
 3     string replaceSpaces(string S, int length) 
 4     {
 5         string ans;
 6         int i=0;
 7         for(auto c:S)
 8         {
 9             if(c==' ') 
10             {
11                 ans+="%20";
12             }
13             else 
14             {
15                 ans+=c;
16             }
17             i++;
18             if(i==length) 
19             {
20                 break;
21             }
22         }
23         return ans;
24 
25     }
26 };

 

posted @ 2020-05-04 09:44  梦醒潇湘  阅读(161)  评论(0)    收藏  举报