剑指OFFER----面试题38. 字符串的排列

链接:https://leetcode-cn.com/problems/zi-fu-chuan-de-pai-lie-lcof/

 

代码:

class Solution {
public:
    vector<string> permutation(string s) {
        vector<string> res;
        sort(s.begin(), s.end());
        dfs(s, 0, s.size(), res);
        return res;
    }

    void dfs(string s, int l, int r, vector<string>& res) {
        if (l == r) res.push_back(s);
        else {
            for (int i = l; i < r; ++i) {
                if (i > l && s[i] == s[l]) continue;
                swap(s[i], s[l]);
                dfs(s, l + 1, r, res);
            }
        }
    }
};

 

posted @ 2020-03-02 21:43  景云ⁿ  阅读(109)  评论(0编辑  收藏  举报