类似快慢指针找cycle O(n) time O(1) space
1 2 3 4 5 6 7 8 9 10 11 12
| class Solution { public: string restoreString(string s, vector<int>& indices) { for (int i = 0; i < indices.size(); ++i) { while (i != indices[i]) { swap(s[i], s[indices[i]]); swap(indices[i], indices[indices[i]]); } } return s; } };
|