Day1
面试01.03
//C 代码 本机编译过,但力扣编译不过
char* replaceSpaces(char* S, int length){
if(length <= 0 || length > 500000) {
return "";
}
char* tmp = (char*)malloc(sizeof(char)*length+1);
char* value = (char*)malloc(sizeof(char));
memset(tmp,'\0',sizeof(char)*length+1);
for(int i = 0; i < length&&*(S+i)!='\0'; i++){
if(S[i] == ' ') {
strcat(tmp,"%20");
}else{
value[0] = *(S+i);
strcat(tmp,value);
}
}
return tmp;
}
//C++
class Solution {
public:
string replaceSpaces(string S, int length)
{
string tmp = "";
if (length <= 0 || length > 500000) {
return tmp;
}
for (int i = 0; i < length; ++i) {
if (S[i] == ' ') {
tmp += "%20";
} else {
tmp += S[i];
}
}
return tmp;
}
};
1528重排字符串
//C++
class Solution {
public:
string restoreString(string s, vector<int>& indices) {
if(sizeof(s) == 0 || indices.size() == 0)
return NULL;
string dest = s;
for(int i = 0; i < indices.size(); ++i)
{
dest[indices[i]] = s[i];
}
return dest;
}
};