剑指 Offer 05. 替换空格
难度简单 76
请实现一个函数,把字符串 s
中的每个空格替换成”%20”。
示例 1:
输入:s = “We are happy.”
输出:“We%20are%20happy.”
class Solution {
public String replaceSpace(String s) {
int length = s.length();
char[] array = new char[length * 3];//最大3倍
int size = 0;
for (int i = 0; i < length; i++) {
char c = s.charAt(i);
if (c == ' ') {
array[size++] = '%';
array[size++] = '2';
array[size++] = '0';
} else {
array[size++] = c;
}
}
String newStr = new String(array, 0, size);
return newStr;
}
// 作者:LeetCode-Solution
// 链接:https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/solution/mian-shi-ti-05-ti-huan-kong-ge-by-leetcode-solutio/
}