题解 | #字符串的排列#

字符串的排列

http://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7

class Solution {
public:

    void subPermutation(string &str,int pos,vector<string> &temp){
        int size=str.size();
        if(pos==size-1)
            temp.push_back(str);
        for(int i=pos;i<size;i++)
        {
            if(i==pos)
                subPermutation(str, pos+1,temp);
            if(str[i]!=str[pos])
            {
                swap(str[pos], str[i]);
                subPermutation(str, pos+1,temp);
                swap(str[pos], str[i]);
            }

        }
    }
    vector<string> Permutation(string str) {
        vector<string> dp;
        if(str.size()==0)
            return dp;
        subPermutation(str,0,dp);
        return dp;
    }
};

都说使用动态规划,但是我还是感觉称之为回溯比较合适,典型的模板题

全部评论

相关推荐

11-26 09:30
复旦大学 Java
点赞 评论 收藏
分享
11-04 10:30
已编辑
门头沟学院 研发工程师
开心小狗🐶:“直接说答案”
点赞 评论 收藏
分享
双尔:反手回一个很抱歉,经过慎重考虑,您与我的预期暂不匹配,感谢您的投递
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务