Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 25.md #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions algorithm/For-offer/25.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

这其实是很典型的递归思路。

考虑字符串中含有重复字符的情况,第一个字符和后面所要交换的字符重复时,无需交换。

## 三、解题代码

```java
Expand Down Expand Up @@ -40,16 +42,21 @@ public class Test {
if (chars.length - 1 == begin) {
System.out.print(new String(chars) + " ");
} else {
char tmp;
           // 首位不变时,进行排列
           permutation(chars, begin + 1);
           char tmp;
// 对当前还未处理的字符串进行处理,每个字符都可以作为当前处理位置的元素
for (int i = begin; i < chars.length; i++) {
// 下面是交换元素的位置
tmp = chars[begin];
chars[begin] = chars[i];
chars[i] = tmp;

// 处理下一个位置
permutation(chars, begin + 1);
           for (int i = begin + 1; i < chars.length; i++) {
            if(chars[begin] != chars[i]){
// 下面是交换元素的位置
tmp = chars[begin];
chars[begin] = chars[i];
chars[i] = tmp;

// 处理下一个位置
permutation(chars, begin + 1);
}

}
}
}
Expand Down