TreeviewCopyright © aleen42 all right reserved, powered by aleen42
05. 替换空格
https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/
Java
class Solution {
public String replaceSpace(String s) {
StringBuilder result = new StringBuilder();
for(Character c : s.toCharArray()) {
if(c == ' ') {
result.append("%20");
}else {
result.append(c);
}
}
return result.toString();
}
}
Python
'''
Author: Goog Tech
Date: 2020-08-04 16:02:32
LastEditTime: 2020-08-04 16:03:10
Description: https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/
FilePath: \leetcode-googtech\剑指 Offer\#05. 替换空格\Solution.py
'''
class Solution(object):
def replaceSpace(self, s):
"""
:type s: str
:rtype: str
"""
result = []
for c in s:
if c == ' ':
result.append("%20")
else:
result.append(c)
return ''.join(result)