题解 | #字符串的排列#

字符串的排列

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

#include <algorithm>
class Solution {
  public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param str string字符串
     * @return string字符串vector
     */
    void Permutation(vector<string> & res,string str_head, string str,vector<bool> vt_vis)
    {
        if (str_head.length() == str.length()) {
            res.push_back(str_head);
        }

        for (int i=0; i<vt_vis.size(); i++) {
            if (vt_vis[i]) {
                continue;
            }

		//多个重复的字符,因为第二个重复字符continue了
		//所以最后str_head的长度无法达标,从而淘汰了后面继续使用多个重复的结果
            if (i>0 && str[i] == str[i-1] && vt_vis[i-1]) {
                continue;
            }

            vt_vis[i] = true;
            string str_head_temp = str_head + str[i];
            Permutation(res,str_head_temp,str,vt_vis);

            vt_vis[i] = false;
        }

    }

    vector<string> Permutation(string str) {
        vector<string> vt_str;
        if (str.empty()) 
        {
            return vt_str;
        }

        sort(str.begin(), str.end());
        
        vector<bool> vt_vis(str.length(),false);
        Permutation(vt_str,"",str,vt_vis);
        return vt_str;
    }
};

全部评论

相关推荐

野猪不是猪🐗:😇:恭喜你以出色的表现成为xxx的一员 😨:您以进入本公司人才库 实际点开:您愿望单中的xxx正在特卖!
点赞 评论 收藏
分享
09-14 20:51
四川大学 Java
慢热的鲸鱼在学习:985加粗就行了,第二个项目来不及准备也没事,省的写了问你你还不会。你只需准备面试八股和项目场景,剩下的交给985。即使面不过也没事,面试经验是最重要的,你现在不缺时间
简历中的项目经历要怎么写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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