leetcode permutation/combination

Next Permutation

将整个排列看成是一个数,按数大小排列,求下一个排列

// ①从右到左找到第一个非递增的位置 pivot
// ②从右到左找到第一个大于 *pivot 的位置 change 
// ③交换*pivot与*change
// ④将pivot右边的元素全部逆置
// @algorithm http://?sherlei.blogspot.com/2012/12/leetcode-next-permutation.html 
// @author soulmachine http://github.com/soulmachine/leetcode

void nextPermutation(vector<int> A) {
    return next_permutation(A.begin(), A.end());
}

template<typename Itr>
bool next_permutation(Itr first, Itr last) {
    
    const auto rfirst = reverse_iterator<Itr>(last);
    const auto rlast = reverse_iterator<Itr>(first);
    
    auto pivot = next(rfirst);
    
    while (pivot != rlast && *pivot >= *prev(pivot)) {
        pivot = next(pivot);
    }
    
    if (pivot == rlast) {
        reverse(rfirst, rlast);
        return false;
    }
    
    auto change = find_if(rfirst, pivot, bind1st(less<int>, *pivot));
    
    swap(*pivot, *change);
    
    reverse(rfirst, pivot);
    
    return true;
}

Permutations/ Permutations II

// 求全排列
// @author soulmachine http://github.com/soulmachine/leetcode

vector<vector<> > permute(vector<int> &A) {
    vector<vector<int> > v;
    sort(A.begin(), A.end());
    
    do {
        v.push_back(A);
    }while (next_permutation(A.begin(), A.end()));
    
    return v;
}

Combinations

// 求全组合Cnk,深搜, O(n!)
// @author soulmachine http://github.com/soulmachine/leetcode

vector<vector<int> > combine(int n, int k) {
    vector<vector<int> > v;
    vector<int> t;
    dfs(n, k, 1, 0, t, v);
    return v;
}

void dfs(int n, int k, int start, int curr, vector<int> &t, vector<vector<int> > &v) {
    
    if (curr == k)
        v.push_back(t);
        
    for (int i = start; i <= n; ++i) {       // 确定一个找下一个
        t.push_back(t);
        dfs(n, k, i + 1, curr + 1, t, v);    // 深度
        t.pop_back();                        // 返回该层
    }
}
时间: 2024-12-29 07:32:05

leetcode permutation/combination的相关文章

Leetcode - Letter Combination Of A Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

Java for LeetCode 216 Combination Sum III

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1

LeetCode: Permutation Sequcence 题解

The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3): "123" "132" "213" "231" "312" "3

Leetcode permutation 系列

关于permutation的讲解,请参见http://blog.csdn.net/xuqingict/article/details/24840183 下列题目的讲解均是基于上面的文章: 题1: Next Permutation Total Accepted: 8066 Total Submissions: 32493My Submissions Implement next permutation, which rearranges numbers into the lexicographic

[LeetCode: 题解] Combination Sum

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) will

[LeetCode] 039. Combination Sum (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 039. Combination Sum (Medium) 链接: 题目:https://leetcode.com/problems/combination-sum/ 代码(github):https://github.com/illuz/leetcode 题意: 给出一些正整数集合,以及一个目标数,从集合中选择一

[leetcode] 040. Combination Sum II (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 040. Combination Sum II (Medium) 链接: 题目:https://leetcode.com/problems/combination-sum-ii/ 代码(github):https://github.com/illuz/leetcode 题意: 跟 039 一样(给出一些正整数集合,

LeetCode: Permutation Sequence [059]

[题目] The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3): "123" "132" "213" "231" "312" &

[leetcode]Permutation Sequence @ Python

原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3): "123" "132&