题解 | #没有重复项数字的全排列#

没有重复项数字的全排列

https://www.nowcoder.com/practice/4bcf3081067a4d028f95acee3ddcd2b1

#include <vector>
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param num int整型vector 
     * @return int整型vector<vector<>>
     */
    vector<vector<int> > permute(vector<int>& num) {
        // write code here
        // 用hash判断是否重复
        // 用递归完成向后判断
        
        vector<vector<int>> resList;
        if(num.size()==1){
            cout<<"只是一个值"<<endl;
            resList.push_back(num);
            return resList;
        }
        vector<int> tmpList;
        for (int v1 : num) {
                    cout << v1 << ' ';
            }
        cout<<"____"<<endl;
        for(int i = 0; i<num.size(); i++){
            tmpList = num;
            tmpList.erase(tmpList.begin()+i);
            
            
            vector<vector<int>> tmpRes = permute(tmpList);
            
            for(int j = 0; j < tmpRes.size(); j++){
                vector<int> tmpValue = tmpRes[j];
                cout<<tmpValue.size()<<endl;
                cout<<"开始插入"<<num[i]<<endl;
                tmpValue.insert(tmpValue.begin(), num[i]);
                
                resList.push_back(tmpValue);
                cout<<"resList的长度"<<resList.size()<<endl;
            }
        }
        return resList;
    }
};

全部评论

相关推荐

拓竹投了十多天了&nbsp;一直没反应想起来还投了这家&nbsp;看了眼发现已经挂了&nbsp;苦笑(是我太不上心了)
机械打工仔:简历挂和你不太上心有啥关系,别啥都怪自己,自信点
投递拓竹科技等公司10个岗位
点赞 评论 收藏
分享
约会议室面试被+1发现了
积极的悲伤蛙愿off...:这怎么了,我都直接跟带教说投了哪些公司让他帮我看看简历,本来就是阶段性的实习生,别真把自己当正式工了
点赞 评论 收藏
分享
自学java狠狠赚一...:骗你点star的,港卵公司,记得把star收回去
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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