当前位置: 首页 > news >正文

permutation II (boss出来了)

题目链接:https://leetcode.com/submissions/detail/55876321/

自己的做法,30个测试用例通过了29例,终究还是有一个系列类型的是无法通过的,因为自己妄想在permutation的代码上,通过排序来进行。然而,每一次同第一个元素交换位置之后,进入了递归,此时数组nums并不是有序的!!!

来看看自己的这段代码,谨记教训,然后还是去看看大神们的思路吧!

class Solution {
public:
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        if(nums.size()==0)
            return res;
        sort(nums.begin(),nums.end());//事实
        int len=nums.size();
        vector<int> temp;
        helper(nums,0,0,len,temp);
        return res;
    }
private:
    void helper(vector<int>& nums,int pos,int count,int len,vector<int>& temp);
private:
    vector<vector<int>> res;
};

void Solution::helper(vector<int>& nums,int pos,int count,int len,vector<int>& temp){
    if(count==len){
        res.push_back(temp);
        return;
    }
    for(int i=pos;i<len;i++){
        if(pos!=i && nums[pos]==nums[i])//后面元素与自己相等时,忽略此次递归
            continue;
        if(i>pos&&nums[i]==nums[i-1])//当后面有连续相同的元素存在时,只做第一次,后面的相等元素忽略
            continue;
        if(pos!=i)
        swap(nums[pos],nums[i]);
        temp.push_back(nums[pos]);//这儿可别写成了nums[i],害自己调试半天
        helper(nums,pos+1,++count,len,temp);
        temp.pop_back();
        count--;
        swap(nums[pos],nums[i]);
    }
}
View Code

http://www.cnblogs.com/TenosDoIt/p/3662644.html

参考了大神的博客,再修改自己的代码:

 1 class Solution {
 2 public:
 3     vector<vector<int>> permuteUnique(vector<int>& nums) {
 4         if(nums.size()==0)
 5             return res;
 6 //        sort(nums.begin(),nums.end());//事实
 7         int len=nums.size();
 8         vector<int> temp;
 9         helper(nums,0,0,len,temp);
10         return res;
11     }
12 private:
13     void helper(vector<int>& nums,int pos,int count,int len,vector<int>& temp);
14     bool find(vector<int>&nums,int begin,int end,int target);
15 private:
16     vector<vector<int>> res;
17 };
18 
19 void Solution::helper(vector<int>& nums,int pos,int count,int len,vector<int>& temp){
20     //在上一算法的基础上,当我们枚举第i个位置的元素时,若要把后面第j个元素和i交换,则先要保证[i…j-1]范围内没有和位置j相同的元素。有以下两种做法(1)可以每次在需要交换时进行顺序查找;(2)用哈希表来查重。具体见下面的代码。
21     if(count==len){
22         res.push_back(temp);
23         return;
24     }
25     for(int i=pos;i<len;i++){
26 //        if(pos!=i && nums[pos]==nums[i])//后面元素与自己相等时,忽略此次递归
27 //            continue;
28 //        if(i>pos&&nums[i]==nums[i-1])//当后面有连续相同的元素存在时,只做第一次,后面的相等元素忽略
29 //            continue;
30 //        if(pos!=i)
31         if(i>pos&&find(nums,pos,i-1,nums[i]))//第一次时竟然写成了i,于是乎每一次都会执行continue!!!!
32             continue;
33         swap(nums[pos],nums[i]);
34         temp.push_back(nums[pos]);//这儿可别写成了nums[i],害自己调试半天
35         helper(nums,pos+1,++count,len,temp);
36         temp.pop_back();
37         count--;
38         swap(nums[pos],nums[i]);
39     }
40 }
41 bool Solution::find(vector<int>&nums,int begin,int end,int target){
42     for(;begin<=end;begin++){
43         if(nums[begin]==target)
44             return true;
45     }
46     return false;
47 }

 

转载于:https://www.cnblogs.com/chess/p/5261120.html

相关文章:

  • Nginx在Window下的使用笔记
  • linux编程之GDB调试
  • 数组、指针
  • 架构师速成8.2-架构师要懂产品
  • Javascript设计模式学习之Observer(观察者)模式
  • python用sybase自带的sybpydb模块访问数据库
  • 三种对象传参和ModelDriven的原理
  • netty demo
  • [Angularjs]asp.net mvc+angularjs+web api单页应用
  • 解决tomcat6部署spring4+mybatisJSP页面产生的500错误,控制台报java.lang.NullPointerException的问题...
  • SQL Server中查看哪些游标未释放
  • 【抄】更改eclipse配置
  • 胜利大逃亡(续)
  • 理解JavaScript中的回调函数
  • hdu 5640 King's Cake(模拟)
  • angular2开源库收集
  • - C#编程大幅提高OUTLOOK的邮件搜索能力!
  • C学习-枚举(九)
  • Eureka 2.0 开源流产,真的对你影响很大吗?
  • iOS帅气加载动画、通知视图、红包助手、引导页、导航栏、朋友圈、小游戏等效果源码...
  • Less 日常用法
  • MobX
  • Python进阶细节
  • Python中eval与exec的使用及区别
  • SpringCloud集成分布式事务LCN (一)
  • Vue组件定义
  • Web Storage相关
  • 得到一个数组中任意X个元素的所有组合 即C(n,m)
  • 力扣(LeetCode)357
  • 聊聊spring cloud的LoadBalancerAutoConfiguration
  • 如何优雅的使用vue+Dcloud(Hbuild)开发混合app
  • 三栏布局总结
  • 设计模式(12)迭代器模式(讲解+应用)
  • 如何在 Intellij IDEA 更高效地将应用部署到容器服务 Kubernetes ...
  • 如何正确理解,内页权重高于首页?
  • ​html.parser --- 简单的 HTML 和 XHTML 解析器​
  • ​比特币大跌的 2 个原因
  • ​第20课 在Android Native开发中加入新的C++类
  • $分析了六十多年间100万字的政府工作报告,我看到了这样的变迁
  • (C#)一个最简单的链表类
  • (done) ROC曲线 和 AUC值 分别是什么?
  • (PyTorch)TCN和RNN/LSTM/GRU结合实现时间序列预测
  • (SpringBoot)第七章:SpringBoot日志文件
  • (翻译)Entity Framework技巧系列之七 - Tip 26 – 28
  • (五)关系数据库标准语言SQL
  • (原创)boost.property_tree解析xml的帮助类以及中文解析问题的解决
  • (转)EXC_BREAKPOINT僵尸错误
  • .NET I/O 学习笔记:对文件和目录进行解压缩操作
  • .net mvc部分视图
  • .Net Remoting(分离服务程序实现) - Part.3
  • .NET 线程 Thread 进程 Process、线程池 pool、Invoke、begininvoke、异步回调
  • .NET/C# 使用反射调用含 ref 或 out 参数的方法
  • .Net调用Java编写的WebServices返回值为Null的解决方法(SoapUI工具测试有返回值)
  • .NET中的Event与Delegates,从Publisher到Subscriber的衔接!
  • .Net组件程序设计之线程、并发管理(一)