理论

C++ 中的next_permutation 一般作为正序全排列的使用规则,其实这个就是正序字典排序的实现。

比如我们要对 列表 [1,2,3]  做full permutation 一般使用递归实现 如下,

 static boolean generateP(int index, int high, int[] list) {

     if (index == high) {

         System.arraycopy(P,,list,,list.length);

         return true;

     }else {

         for (int x = index; x <high; x++) {

             swap(list,list[x],list[index]);

             generateP(index+,high,list);

             swap(list,list[x],list[index]);

         }

     }

     return false;

 }

下面对字典排序规则说一下

(1)从后往前遍历,找到第一个逆序,比如1,2,4,3 的2,记录位置pos与值value

(2) 如果遍历完也没有找到这个元素说明已经是排序的最后一个了那么从头到尾做reverse 使其他为升序 如4 3 2 1 变为 1->2->3->4;

(3)如果步骤一找到了这个数,那么从后面往前面找到第一大于它的数,并且交换(很多人说从步骤一发现的数开始往后面找是不对的)

步骤3交换后从步骤一发现的的位置后面开始进行头尾的逆序操作。

拆分出来需要三个方法 reverse ,swap,permutation,

板子

 static void next_permutation(int[] nums) {

     int value = , pos = ;

     int i = , temp = ;

     for (i = nums.length - ; i > ; i--) {

         if (nums[i] > nums[i - ]) {//记录非逆序的第一个数

             value = nums[i - ];

             pos = i - ;

             break;

         }

     }

     if (i == ) {//未发现数那么直接进行逆置

         for (i = ; i < nums.length / ; i++) {

             temp = nums[i];

             nums[i] = nums[nums.length - i - ];

             nums[nums.length - i - ] = temp;

         }

         return;

     }

     for (int j = nums.length - ; j > pos; j--) {

         if (value < nums[j]) {//从后往前面找到第一大于非逆序数

             temp = value;

             nums[pos] = nums[j];

             nums[j] = temp;

             break;

         }

     }

     for (i = pos + ; i < pos +  + (nums.length -  - pos) / ; i++) {

         temp = nums[i];//从非逆序数开始进行倒置

         nums[i] = nums[nums.length -  - i + pos + ];

         nums[nums.length -  - i + pos + ] = temp;

     }

 }

题目例子:

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be in-place, do not allocate extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.

1,2,3 → 1,3,2

3,2,1 → 1,2,3

1,1,5 → 1,5,1

字典排序permutation的更多相关文章

  1. C# 字典排序Array.Sort

    Array.Sort可以实现便捷的字典排序,但如果完全相信他,那么就容易产生些异常!太顺利了,往往是前面有坑等你. 比如:微信接口,好多地方需要签名认证,签名的时候需要用的字典排序,如果只用Array ...

  2. python 字典排序 关于sort()、reversed()、sorted()

    一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...

  3. <转>python字典排序 关于sort()、reversed()、sorted()

    一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...

  4. java的字典排序

    按照教程上的代码还是报错 应该是字典排序的问题,不能是Arrays.sort()

  5. 排序 permutation

    习题2-6 排序 permutation 用1,2,3……9组成3个三位数abc,def和ghi,每个数字恰好使用一次,要求abc:def:ghi=1:2:3.按照“abc def ghi”的格式输出 ...

  6. php strcmp()字典排序

    字典排序(lexicographical order)是一种对于随机变量形成序列的排序方法.其方法是,按照字母顺序,或者数字小大顺序,由小到大的形成序列. 比如,字典中a-z,是依次递增的,a,b,c ...

  7. 深入Python(1): 字典排序 关于sort()、reversed()、sorted()

    http://www.cnblogs.com/BeginMan/p/3193081.html 一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠 ...

  8. 签名:实现参数字典排序,然后拼接为url参数形式

    在很多地方请求参数需要做处理例如: 步骤 1.参数字典排序. 2.拼接字符. /// <summary> /// 生成签名 /// </summary> /// <par ...

  9. python中字典排序,列表中的字典排序

    python中字典排序,列表中的字典排序 一.使用python模块:operator import operator #首先要导入模块operator x = {1:2, 3:4, 4:3, 2:1, ...

随机推荐

  1. mysql七:视图、触发器、事务、存储过程、函数

    阅读目录 一 视图 二 触发器 三 事务 四 存储过程 五 函数 六 流程控制 一 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名 ...

  2. malloc和new的区别是什么?

    http://zhidao.baidu.com/link?url=iUDUZeJtj1o12PvUETLlJgvAMqzky5HxGCJRGnULpsO8HdWAdjKkQqGCJ9-o-aTu8NP ...

  3. [原创]Java集成PageOffice在线打开编辑word文件 - Spring Boot

    开发环境:JDK1.8.Eclipse.Sping Boot + Thymeleaf框架. 一. 构建Sping Boot + Thymeleaf框架的项目(不再详述): 1. 新建一个maven p ...

  4. python-多线程3-生产者消费者

    '''生产者和消费者''' ''' 用python写一个多线程的生产者和消费者 生产者x x>0,有东西,print(不生产) x=0,没东西,print(生产) for循环 消费者x x=0, ...

  5. SpringBoot_Exception_01_No plugin found for prefix 'spring-boto' in the current project

    一.异常现象 spingbott项目在eclipse中执行maven命令:spring-boot:run, 出现异常: No plugin found for prefix 'spring-boto' ...

  6. 改变Ecplise项目窗口字体样式

    Eclipse\plugins\org.eclipse.ui.themes_1.1.1.v20151026-1355\css e4-dark_win.css CTabFolder Tree, CTab ...

  7. 01PS基础

    通道:记录颜色信息 alpha通道:主要用来记录选取 画笔颜色模式:会保留纹理,不要直接在原图上画,可以新建一个层,然后变成颜色模式 调整色阶(ctrl + l)的三种方式:1.输入:修改前  > ...

  8. select查询语句执行顺序

    查询中用到的关键词主要包含六个,并且他们的顺序依次为select--from--where--group by--having--order by其中select和from是必须的,其他关键词是可选的 ...

  9. CF 438 E & bzoj 3625 小朋友和二叉树 —— 多项式开方

    题目:http://codeforces.com/contest/438/problem/E https://www.lydsy.com/JudgeOnline/problem.php?id=3625 ...

  10. C#的Unit Test如何根据exception来判断函数是否执行正确

    添加ExpectedException属性, 然后指定异常类型, catch后决定Assert.IsTrue The following class contains the method to te ...