输入:s = "We are happy."
输出:"We%20are%20happy."
class Solution {
public String replaceSpace(String s) {
char[] nums = new char[s.length() * 3];
int index = 0;
for (int i=0;i<s.length();i++) {
if(s.charAt(i) != ' ') {
nums[index++] = s.charAt(i);
} else {
nums[index++] = '%';
nums[index++] = '2';
nums[index++] = '0';
}
}
String res = new String(nums, 0, index);
return res;
}
}
class Solution {
fun replaceSpace(s: String): String {
val nums = CharArray(s.length * 3)
var index = 0
for (i in 0..s.length-1) {
if (s[i] != ' ') {
nums[index++] = s[i]
} else {
nums[index++] = '%'
nums[index++] = '2'
nums[index++] = '0'
}
}
return String(nums, 0, index)
}
}