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

下一个排列 #8

Closed
ningyuwhut opened this issue Oct 5, 2013 · 1 comment
Closed

下一个排列 #8

ningyuwhut opened this issue Oct 5, 2013 · 1 comment

Comments

@ningyuwhut
Copy link

int next_permutation(int num[], int len) {
int i = len - 2; // partition number's index 
while (i >= 0 && num[i] >= num[i + 1]) i--;
if(i == -1) return 0;
 if (i >= 0) {
int j = len - 1; // change number's index 
while (num[j] <= num[i]) --j;
swap(num, i, j); reverse(num, i + 1, len);
} else {
reverse(num, i + 1, len);
}
return 1;

这段代码中if(i == -1) return 0;应该去掉吧

如果为-1应该对整个数组逆序,而不是返回.否则最后面的else子句根本不会执行

求确认

@soulmachine
Copy link
Owner

是必须的,你看看最新的代码

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants