字典排序permutation
理论
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的更多相关文章
- C# 字典排序Array.Sort
Array.Sort可以实现便捷的字典排序,但如果完全相信他,那么就容易产生些异常!太顺利了,往往是前面有坑等你. 比如:微信接口,好多地方需要签名认证,签名的时候需要用的字典排序,如果只用Array ...
- python 字典排序 关于sort()、reversed()、sorted()
一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...
- <转>python字典排序 关于sort()、reversed()、sorted()
一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...
- java的字典排序
按照教程上的代码还是报错 应该是字典排序的问题,不能是Arrays.sort()
- 排序 permutation
习题2-6 排序 permutation 用1,2,3……9组成3个三位数abc,def和ghi,每个数字恰好使用一次,要求abc:def:ghi=1:2:3.按照“abc def ghi”的格式输出 ...
- php strcmp()字典排序
字典排序(lexicographical order)是一种对于随机变量形成序列的排序方法.其方法是,按照字母顺序,或者数字小大顺序,由小到大的形成序列. 比如,字典中a-z,是依次递增的,a,b,c ...
- 深入Python(1): 字典排序 关于sort()、reversed()、sorted()
http://www.cnblogs.com/BeginMan/p/3193081.html 一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠 ...
- 签名:实现参数字典排序,然后拼接为url参数形式
在很多地方请求参数需要做处理例如: 步骤 1.参数字典排序. 2.拼接字符. /// <summary> /// 生成签名 /// </summary> /// <par ...
- python中字典排序,列表中的字典排序
python中字典排序,列表中的字典排序 一.使用python模块:operator import operator #首先要导入模块operator x = {1:2, 3:4, 4:3, 2:1, ...
随机推荐
- java 创建 HMAC 签名
ava 创建 HMAC 签名 psd素材 1. []ComputopTest.java package com.javaonly.hmac.test; import java.io.IOExcepti ...
- 机器学习:simple linear iterative clustering (SLIC) 算法
图像分割是图像处理,计算机视觉领域里非常基础,非常重要的一个应用.今天介绍一种高效的分割算法,即 simple linear iterative clustering (SLIC) 算法,顾名思义,这 ...
- OpenCV——Skewing
// define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...
- poj3017 Cut the Sequence[平衡树+单调队列优化]
这里已经讲得很清楚了. 本質上是決策點與區間最大值有一定關係,於是用单调队列来维护决策集合(而不是常规的),然后在决策集合中选取最小值. 然后觉得这题方法还是很重要的.没写平衡树,用优先队列(堆)来维 ...
- 南阳oj水题集合,语言的灵活运用
a+b 输入 输入两个数,a,b 输出 输出a+b的值 样例输入 2 3 样例输出 5 c/c++ #include<iostream> using namespace std; int ...
- SQL一次性插入大量数据【转载】
在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQL一系统性能问题.下面介绍SQL Server支持的两种批量 ...
- 错误:(26, 13) Failed to resolve: com.android.support:appcompat-v7:27.+
小编也是初学安卓,今天配置环境的时候遇到这个问题了,搞了半天终于找到了问题 在build.gradle中添加 allprojects { repositories { jcenter() maven ...
- PCL中有哪些可用的PointT类型(5)
博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=270 Narf36 - float x, y, z, roll, pitch ...
- usb资料2
ubuntu linux下如何在启动时就关闭usb接口? https://zhidao.baidu.com/question/548651197.html Linux USB 驱动开发(四)—— 热插 ...
- HTML页面弹出窗口调整代码总结
弹出跟你当前的窗口有没有菜单工具栏没有关系,你只要在页面中写一个脚本它就弹出了.比如<a href=# onclick="window.open('xxx.aspx','窗口名称',' ...