[leetcode刷题系列]Permutations
来源:互联网 发布:软考程序员考试题目 编辑:程序博客网 时间:2024/05/17 21:45
很基本的题目把, 不过貌似限制了不许用stl现有的next_permutation, 反正用的时候显示编译错误了,不知道是不是我写错了。
于是就直接自己写了个next_permutation的实现。 由于最近一直有在看英文版的组合数学,而且最近恰巧也练习过如何按照字典序得出所有的序列。
所以这题写的比较顺利, 编译通过后就过了。
class Solution { bool next_permutation(int* begin, int* end){ if(begin + 1 >= end) return false; int* p = end - 1; while(p - 1 >= begin) if(*(p - 1) < *p) break; else -- p; if(p - 1 < begin){ reverse(begin, end); return false; } int * target = p - 1; p = end - 1; while(p > target) if(*p > *target) break; else -- p; swap(*target, *p); reverse(target + 1, end); return true; }public: vector<vector<int> > permute(vector<int> &num) { // Start typing your C/C++ solution below // DO NOT write int main() function int n = num.size(); int a[num.size()]; for(int i = 0; i < n; ++ i) a[i] = num[i]; sort(a, a + n); vector<vector<int> > ret; do{ vector<int> v(a, a + n); ret.push_back(v); }while(next_permutation(a, a + n)); return ret; }};
- [leetcode刷题系列]Permutations
- [leetcode刷题系列]Permutations II
- 【Leetcode长征系列】Permutations
- leetcode系列(55)Permutations, Permutations II
- leetcode 刷题之路 12 Permutations
- LeetCode刷题笔录Permutations II
- LeetCode刷题笔录Permutations II
- [leetCode刷题笔记]46. Permutations
- [leetCode刷题笔记]47. Permutations II
- LeetCode算法题之Permutations
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- leetcode 刷题之路 77 Permutations II
- LeetCode: Permutations
- LeetCode: Permutations
- [Leetcode] Permutations
- JavaSE笔记01:原生数据类型
- Firebug使用详解
- C语言基础知识
- 码字员的自我修养
- c++ builder for ubuntu
- [leetcode刷题系列]Permutations
- 我的心里只有你--单件模式
- J2SE 必知必会(十一)
- CSharp生成二维条码的步骤
- JavaSE笔记02:运算符
- [leetcode刷题系列]Permutations II
- 8,少年白_穷忙族
- UIBarButtonSystemItemAction的风格
- Vim常用命令