my_next_permutation in c++
2008-08-22 08:34
253 查看
bool my_next_permutation(int *pData, int nSize)
{
int *pLast = pData + nSize - 1;
int *pCurr = pLast;
for(;;)
{
if (pCurr == pData)
return false;
int *pPrev = pCurr-1;
if (*pPrev < *pCurr)
{
int *pTemp = pLast;
for(;;)
{
if (pTemp == pData)
return false;
if (*pTemp > *pPrev)
break;
pTemp--;
}
std::swap(*pPrev, *pTemp);
std::reverse(pCurr, pLast+1);
return true;
}
pCurr--;
}
}
TEST:
do
{
copy(a, a + size, ostream_iterator<int>(cout, ","));
cout << endl;
} while(my_next_permutation(a, size));
{
int *pLast = pData + nSize - 1;
int *pCurr = pLast;
for(;;)
{
if (pCurr == pData)
return false;
int *pPrev = pCurr-1;
if (*pPrev < *pCurr)
{
int *pTemp = pLast;
for(;;)
{
if (pTemp == pData)
return false;
if (*pTemp > *pPrev)
break;
pTemp--;
}
std::swap(*pPrev, *pTemp);
std::reverse(pCurr, pLast+1);
return true;
}
pCurr--;
}
}
TEST:
do
{
copy(a, a + size, ostream_iterator<int>(cout, ","));
cout << endl;
} while(my_next_permutation(a, size));
相关文章推荐
- 【C++】【LeetCode】31. Next Permutation && 46. Permutations
- [leetcode-117]Populating Next Right Pointers in Each Node II(c++)
- c++中的 next_permutation的实现
- C++STL的next_permutation
- C++ STL之next_permutation小证明与Java实现
- [C++]LeetCode: 124 Populating Next Right Pointers in Each Node II(链接二叉树)
- 全排列的生成使用C++的库函数:next_permutation
- C++ next_permutation 源码分析
- C++ 求排列函数 next_permutation, prev_permutation
- my_combination in c++
- C++ STL algorithm库中 next_permutation()用法 注意事项
- C++实现全排列(调用next_permutation)
- Visual C++ Standard Library:next_permutation
- C++-7.2.4-下一个排列(next_permutation)
- Leetcode_populating-next-right-pointers-in-each-node(updated c++ and python version)
- C++之next_permutation
- 关于对自己C++的next_permutation的一点提醒
- C++STL的next_permutation
- C++ 全排列函数 std::next_permutation与std::prev_permutation
- [C++] next_permutation的函数 与 copy() 函数的用法