JZ5 替换空格
描述
请实现一个函数,将一个字符串s中的每个空格替换成“%20”。
例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
数据范围:
。保证字符串中的字符为大写英文字母、小写英文字母和空格中的一种。
进阶:时间复杂度
,空间复杂度 ![]()
struct Solution{
}
impl Solution {
fn new() -> Self {
Solution{}
}
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return string字符串
*/
pub fn replaceSpace(&self, s: String) -> String {
// write code here
let mut res:String = String::new();
for c in s.chars() {
match c {
' ' => res.push_str("%20"),
_ => res.push(c),
}
}
res
}
}

浙公网安备 33010602011771号