Skip to content

Commit e4a41e8

Browse files
authored
Update README.md
递归交换str[begin]和str[i]时,判断是否在此i前已经交换过重复的字符str[i]。
1 parent 85c132c commit e4a41e8

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

028-字符串的排列/README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,20 +200,26 @@ public:
200200
i++)
201201
{
202202
//debug <<str[i] <<str[begin] <<endl;
203-
if(i == begin || str[i] != str[begin])
203+
if(!HasDuplicate(str, begin, i))
204204
{
205-
206205
swap(str[i], str[begin]);
207-
206+
debug <<"swap " <<str[i] <<"(" <<i <<")" <<" and " <<str[begin] <<"(" <<begin <<")" <<endl;
208207
PermutationRecursion(str, begin + 1);
209-
208+
//copy(str.begin( ), str.degin( ) + i, ostream_iterator<char>(cout," "));
210209
swap(str[i], str[begin]);
211-
212210
}
213211
}
214212
}
215213
}
216-
214+
215+
private:
216+
//find duplicate of str[i] in str[k,i)
217+
bool HasDuplicate(string& str, int k, int i) const {
218+
for (int p = k; p < i; p++)
219+
if (str[p] == str[i]) return true;
220+
221+
return false;
222+
}
217223
};
218224
219225
int __tmain( )

0 commit comments

Comments
 (0)