题目:给你一个字符串 s 和一个 长度相同 的整数数组 indices 。请你重新排列字符串 s ,其中第 i 个字符需要移动到 indices[i] 指示的位置。返回重新排列后的字符串。
示例 :
输入:s = "codeleet", indices = [4,5,6,7,0,2,1,3]
输出:"leetcode"
解释:如图所示,"codeleet" 重新排列后变为 "leetcode" 。
1.原创
class Solution {
public:
string restoreString(string s, vector<int>& indices) {
int n = s.size();
char temp[n+1];
string res;
for (int i=0;i<n;++i){
temp[indices[i]] = s[i];
}
temp[n] =' ';
for (int i=0;i<strlen(temp);++i){ //strlen是查找字符串中的“ ”,即结束符
res += temp[i];
}
return res;
}
};
2.题解
class Solution {
public:
string restoreString(string s, vector<int>& indices) {
int length = s.length();
string result(length, 0);
for(int i = 0; i < length; i++) {
result[indices[i]] = s[i];
}
return result;
}
};
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/shuffle-string/solution/zhong-xin-pai-lie-zi-fu-chuan-by-leetcode-solution/