16.Permutations II-带重复元素的排列(中等题)

基于KiCad的STM32最小系统设计实战案例(含PCB布线) 手把手带你用KiCad完成STM32最小系统设计,涵盖原理图绘制、封装匹配、规则约束设置及关键信号PCB布线技巧;全程基于开源EDA工具KiCad,强调实操细节与常见坑点规避。 阅读详情

带重复元素的排列

  1. 题目

    给出一个具有重复数字的列表,找出列表所有不同的排列。

  2. 样例

    给出列表 [1,2,2],不同的排列有:
    这里写图片描述

  3. 挑战

    使用递归和非递归分别完成该题。

  4. 题解

1.递归解法
在无重复元素的全排列算法的基础上加入重复数字判断就可以了,即[start,j-1]之间是否有和nums[j]相同的数。

class Solution {
    /**
     * @param nums: A list of integers.
     * @return: A list of unique permutations.
     */
    private ArrayList<List<Integer>> list = new ArrayList<List<Integer>>();

    public List<List<Integer>> permuteUnique(int[] nums) {
        permutation(nums,0,nums.length);

        return list;
    }

    private void permutation(int[] nums, int start, int end)
    {
        if (start == end)
        {
            ArrayList<Integer> arrList = new ArrayList<Integer>();
            for (int i=0;i<end;i++)
            {
                arrList.add(nums[i]);
            }
            list.add(arrList);
        }
        else
        {
            for (int j=start;j<end;j++)
            {
                if (isValid(nums,start,j))
                {
                    swap(nums,j,start);
                    permutation(nums,start+1,end);
                    swap(nums,j,start);
                }
            }
        }
    }

    private void swap(int[] nums, int i, int j)
    {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

    private boolean isValid(int[] nums,int start,int index)
    {
        for (int i=start;i<index;i++)
        {
            if (nums[i] == nums[index])
            {
                return false;
            }
        }
        return true;
    }
}

2.字典法
字典法天然可以处理重复元素问题。

class Solution {
    /**
     * @param nums: A list of integers.
     * @return: A list of unique permutations.
     */
    public List<List<Integer>> permuteUnique(int[] nums) {
        ArrayList<List<Integer>> result = new ArrayList<List<Integer>>();
        if (nums.length == 0)
        {
            result.add(new ArrayList<Integer>());
            return result;
        }
        Arrays.sort(nums);
        addList(result,nums);
        while (nextPermutation(nums))
        {
            addList(result,nums);
        }

        return result;
    } 

    private void addList(ArrayList<List<Integer>> list, int[] nums)
    {
        ArrayList<Integer> arrList = new ArrayList<Integer>();
        for (int i=0;i<nums.length;i++)
        {
            arrList.add(nums[i]);
        }
        list.add(arrList);
    }

    private boolean nextPermutation(int[] arr)
    {
        int i=arr.length-2;
        boolean bFind = false;
        for(;i >= 0;i--)
        {
            if(arr[i] < arr[i + 1])
            {
                bFind = true;
                break;
            }
        }
        if (!bFind)
        {
            return false;
        }
        int j = i;
        for (int k=i+1;k<arr.length;k++)
        {
            if (arr[k] > arr[i])
            {
                j = k;
            }
        }
        swap(arr, i, j);
        reverse(arr, i+1, arr.length-1);

        return true;
    }

    private void reverse(int[] arr,int start, int end)
    {
        while (start < end)
        {
            swap(arr, start++, end--);
        }
    }

    private void swap(int[] nums, int i, int j)
    {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}


Last Update 2016.9.28

RK3568平台(USB篇)USB HUB集线器 hub,集线器,连接在host与device之间的一种用于usb接口扩展的usb设备。可以将一个usb上行接口扩展为多个下行接口,使得一个host可以同时与多个device连接。一般来说,一块hub桥接芯片可扩展4个usb接口,而市面上的一拖七hub,其实使用了两块hub桥接芯片,其中一块hub桥接芯片的上行端口连接到另一块hub桥接芯片的下行端口。hub的上行端口面向host,下行端口面向device。在下行端口上,hub提供了设备接入检测和设备移除检测的能力,并给下行端口供电。 阅读详情

相关推荐

基于AMD AU15P FPGA的SLVS-EC桥PCIe设计方案分享

各位FPGAer周末愉快,今天熊猫君分享一个基于AMD AU15P FPGA的SLVS-EC桥PCIe设计方案。

haoxingheng的专栏 1445

【LeetCode】Permutation II(带重复元素的排列) - (LinkedIn) Medium

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1].挑战

It’s All Uphill From Here 556

【PADS-开发指南】

馒头破戒大师

sinat_23896491的博客 3213

LintCode 198: Permutation Index II (有重复元素的permutation index)

Permutation Index II Given a permutation which may contain repeated numbers, find its index in all the permutations of these numbers, which are ordered in lexicographical order. The index begins at 1...

roufoo的博客 648

用STM32定时器PWM高级功能驱动步进电机:F103C8T6+TB6600实战教程

本文详细介绍了如何利用STM32F103C8T6微控制器的定时器PWM高级功能驱动TB6600步进电机驱动器,实现高效、灵活的运动控制方案。通过硬件配置、PWM参数计算、运动控制算法及TB6600高级功能应用的全面解析,为工业自动化和精密控制领域提供了实用的技术参考。

game4的博客 185

Spring Authorization Server自定义密码模式:突破标准OAuth2限制,实现MFA与复杂业务集成

OAuth 2.0授权框架是构建现代应用安全体系的基石,其密码模式(Resource Owner Password Credentials Grant)提供了一种直接的资源所有者凭证交换令牌的机制。该模式的核心原理是客户端收集用户密码并直接向授权服务器请求访问令牌,适用于受信任的客户端场景。在技术价值上,它简化了某些传统应用的OAuth集成流程。然而,标准实现往往难以满足复杂的现实业务需求,例如需要集成多因素认证(MFA)、对接外部LDAP目录服务或执行自定义密码加密校验等场景。Spring Authori

weixin_34302561的博客 437

带重复元素permutation

带重复元素的排列 public List> permuteUnique(int[] nums) { ArrayList> results = new ArrayList>(); if (nums == null) { return results; } if(nums.lengt

suv1234的博客 517

Discrete Optimization (Coursera) 学习笔记 Week 1 introduction (开个帖子)

Week 1 Introduction 这位教授Pascal Van Hentenryck 真的算是优化领域神级别的人物了吧?截一张Google scholar的图大家感受一下… 居然在Coursera上看到了他开的课:Discrete Optimization。试听了几节后,发现讲得很有激情,而且深入浅出。遂决定花暑假跟完这一门课。 这门课每周花两三个小时介绍基础概念,然后得花十几个小时写作业(coding)… 一开始以为Coursera上的课的作业应该不会太难,然后第一次作业(knapsack)我就被

ZJ_11701的博客 656

递归实现全排列(含有重复元素)

using namespace std; bool judge(int begin, int end,int k) { for (int i = begin; i &lt;= end; i++) { if (a[i] == k) { return false; } } return true; } void permutation(int n) { if (n == ...

NoahCool 1004

EMC基础知识总结

电磁干扰(Electromagnetic Interference),简称EMI,有和两种。传导干扰主要是电子设备产生的干扰信号通过导电介质或公共电源线互相产生干扰;辐射干扰是指电子设备产生的干扰信号通过空间耦合把干扰信号传给另一个电网络或电子设备。

学海无涯的专栏 1363

CMake是什么?为什么学习CMake

CMake的主要作用和重要性,包括其在跨平台构建中的应用、如何支持多种编译器及操作系统,以及它在管理大型和复杂项目中的优势。

小鱼酱的博客 3866

排列序号

排列序号给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的编号。其中,编号从1开始。思路:这个问题是一个组合数学问题,如果知道 康托展开,那么好理解一点。但是绝大部分人应该都不知道康托展开。对于这道题,有高中数学基础也就够了,为了便于描述,这里给康托展开的描述,我来解释原因。 3 5 7 4 1 2 9 6 8 展开为 98884 X=2*8!+3*7!+4*6!+2*

NeverGiveUp 2132

基于环形槽谐振器与PIN二极管的频率可重构Vivaldi天线设计与CST仿真研究

摘要 本文设计了一种基于FR4基板的频率可重构Vivaldi天线,通过集成环形槽谐振器(CSRR)和PIN二极管开关实现超宽带(UWB,2-8 GHz)与窄带(2.4/3.5/5.2 GHz)模式的电控切换。天线采用指数渐变槽线结构,结合CSRR的滤波特性和PIN二极管的开关功能,利用CST仿真优化参数。结果表明,UWB模式下S11<-10 dB,窄带模式下目标频点谐振显著,其他频段抑制良好,验证了设计的可行性。该方案为多频段无线通信系统提供了紧凑、低成本的可重构天线解决方案。 关键词:频率可重构天线

huanghm88的专栏 217

【LeetCode】 47. Permutations II 全排列 II(Medium)(JAVA)

【LeetCode】 46. Permutations 全排列(Medium)(JAVA) 题目地址: https://leetcode.com/problems/permutations/ 题目描述: Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3]...

qq_16927853的博客 218

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

LintCode 16. Permutations II

Permutation的进阶题。 此题给的List里是有重复元素的,也就意味着用全排列的方式,可能会有重复的排列,那么怎么规避掉重复的排列呢,我们可以为重复元素标号,比如: [1, 2, 2, 3, 4, 4, 5] 标号后为: [1, 2(1), 2(2), 3, 4(1), 4(2), 4(3), 5] 我们在搜索的时候,强制要求,本来排序靠前的重复元素,在排列后,必须依旧靠前,如: [2(1...

Yee's Blog 255

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

40 全排列II(Permutations II)

文章目录1 题目2 解决方案2.1 思路2.2 图解2.3 时间复杂度2.4 空间复杂度3 源码4 另解4.1 交换法 1 题目 题目:全排列II(Permutations II) 描述:给出一个具有重复数字的列表,找出列表所有不同的排列。 lintcode题号——16,难度——medium 样例1: 输入:nums = [1,1] 输出: [ [1,1] ] 解释: [1,1]的不同排列只有[1,1]。 样例2: 输入: nums = [1,2,2] 输出: [ [1,2,2],

SeeDoubleU的专栏 429

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

[Lintcode]16. Permutations II/[Leetcode]47. Permutations II

16. Permutations II/47. Permutations II 本题难度: Medium Topic: Search & Recursion Description Given a list of numbers with duplicate number in it. Find all unique permutations. Example Example 1: I...

weixin_30411997的博客 144

LeetCode—permutations-ii(有重复数字的排列)—java

题目描述:Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2]have the following unique permutations:[1,1,2],[1,2,1], and[2,1,1].思路解析:这个题...

Lynn_Baby的博客 592

Lintcode_16 Permutations II

Given a list of numbers with duplicate number in it. Find all unique permutations. Example For numbers [1,2,2] the unique permutations are: [ [1,2,2], [2,1,2], [2,2,1] ] 思路是转化为字符串(中间加“

Bernini @ buffalo 266

leetcode Permutations II 无重全排列

作者:jostree转载请注明出处http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排列 题目要求对有重复的数组进行无重全排列。为了保证不重复,类似于全排列算法使用dfs,将第一个数字与后面第一次出现的数字交换即可。例如对于序列1,1,2,2 有如下过程: 1,1,2,2 -&...

weixin_34112208的博客 102

leetcode 46. Permutations and leetcode 47. Permutations II(python实现)

LeetCode 46. Permutations 全排列,树形状态回溯 ,python实现 LeetCode 47. Permutations II,python实现

rainMan的博客 786

leetcode -- Permutations II TODO

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2]have the following unique permutations:[1,1,2],[1,2,1], and[2,1,1]. ...

162

Permutations II

Permutations II 1 public class Solution { 2 public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) { 3 ...

91

LeetCode --- 47. Permutations II

题目链接:Permutations II Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [

makuiyu的专栏 842

Lintcode 15/16 Permutations I/ll

Permutations IGiven a collection of numbers, return all possible permutations.For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].交换法复杂度时间 O(...

Howie66的博客 262

【leetcode】【47】Permutations II

一、问题描述 Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1],

斗code、年华 573
上一篇: 15.Permutations-全排列(中等题)
下一篇: 31.Partition Array-数组划分(中等题)
普通网友
博客等级 码龄11年 12粉丝 261原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值