本文主要是介绍面试题 01.03. String to URL LCCI,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Problem
Write a method to replace all spaces in a string with ‘%20’. You may assume that the string has sufficient space at the end to hold the additional characters,and that you are given the “true” length of the string. (Note: If implementing in Java,please use a character array so that you can perform this operation in place.)
Example1
Input: "Mr John Smith ", 13
Output: “Mr%20John%20Smith”
Example2
Input: " ", 5
Output: “%20%20%20%20%20”
Solution
class Solution {
public:string replaceSpaces(string S, int length) {string ret = "";if(length == 0)return ret;for(int i = 0;i<length;++i){if(S[i] == ' '){ret += "%20";}else{ret += S[i];}}return ret;}
};
这篇关于面试题 01.03. String to URL LCCI的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!