字典排序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, ...
随机推荐
- 分享知识-快乐自己:Mybatis缓存机制
论缓存机制: 1):mybatis 提供了缓存机制减轻数据库压力,提高数据库性能. 2):mybatis 的缓存分为两级:一级缓存.二级缓存 3):一级缓存是SqlSession级别的缓存,缓存的数据 ...
- google IO大会
怎么参加一次 Google I/O?大概要多少预算? Google I/O(参加Goole I/O 是我的一个梦想,因为我是Google死忠,想亲自去Google总部看看,所以想知道这些) 费用构成: ...
- JavaMail API的应用
JavaMail API 是一个用于阅读.编写和发送电子消息的可选包(标准扩展),用来创建邮件用户代理(Mail User Agent,MUA)类型程序. JavaMail API 需要 JavaBe ...
- str_1.判断两个字符串每个字符出现的次数一样
1.两个字符串每个字符出现的次数一样 $str1 = "ab'c4*"; $str2 = "cb*'a4"; $ret = isBX($str1, $str2) ...
- Visual Studio 2012简体中文专业版密钥(激活码)
VS2012 正式版在Beta版的基础上进行了很多改进,尤其是加入了全新的用户界面. VS2012 的硬件需求与VS2010相同,不过由于 Visual Studio 2012 利用了新版 Windo ...
- [转载]Windows网络编程系列教程之四:Select模型
原文:http://www.51see.com/asp/bbs/public/bp_show.asp?t_id=200308131152297103 讲一下套接字模式和套接字I/O模型的区别.先说明一 ...
- Oracle表空间维护总结
1. 概念:表空间:最大的逻辑存储文件,与物理上的一个或多个数据文件对应,每个数据库至少拥有一个表空间,表空间的大小等于构成表空间的所有数据文件的大小总和,用于存储用户在数据库中存储的所有内容. 2. ...
- TCP/IP详解卷1 - wireshark抓包分析
TCP/IP详解卷1 - 系列文 TCP/IP详解卷1 - 思维导图(1) TCP/IP详解卷1 - wireshark抓包分析 引言 在初学TCP/IP协议时,会觉得协议是一种很抽象的东西,通过wi ...
- Python3解leetcode Maximum SubarrayClimbing Stairs
问题: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...
- Ubuntu 重启命令
重启命令: 1.直接reboot 2.shutdown -r now 这两种都是立刻重启 3.shutdown -r 大概30秒后会重启 关机命令:1.halt 立刻关机2.poweroff 立刻 ...