Skip to content

Commit 0df4b38

Browse files
authored
Update shuffle-string.cpp
1 parent 05bf4e3 commit 0df4b38

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

C++/shuffle-string.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,15 @@ class Solution {
66
public:
77
string restoreString(string s, vector<int>& indices) {
88
for (int i = 0; i < s.length(); ++i) {
9-
if (s[i] < 0) {
9+
if (indices[i] == i) {
1010
continue;
1111
}
12-
char move = -s[i];
13-
for (int j = indices[i]; j != i ; j = indices[j]) {
12+
auto move = s[i];
13+
for (int j = indices[i]; j != i; swap(indices[j], j)) {
1414
swap(s[j], move);
15-
move = -move;
1615
}
1716
s[i] = move;
1817
}
19-
for (auto& c : s) {
20-
c = -c;
21-
}
2218
return s;
2319
}
2420
};

0 commit comments

Comments
 (0)