理论

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

最新文章

  1. udacity google deep learning 学习笔记
  2. C程序员学bash shell容易掉坑的注意点(未完待续)
  3. 【BZOJ】3434: [Wc2014]时空穿梭
  4. python 多线程就这么简单(续)
  5. IOS开发之—— 客服QQ(调用qq网页聊天),客服热线(拨打电话)
  6. cf.295.B Two Buttons (bfs)
  7. 窗口截图(可指定HWND窗口句柄)(三篇文章)
  8. 基于注解的SpringMVC整合JPA
  9. OC中-方法到底是如何使用的?
  10. 一张图告诉你如何优化web 性能
  11. nginx使用小记
  12. NUnit详细使用方法
  13. IO.Path路径
  14. WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
  15. poj 2253 floyd最短路
  16. OpenStack-Ocata版+CentOS7.6 云平台环境搭建 — 5.在控制节点上部署计算服务Nova
  17. centos 6&amp;7 升级openssh
  18. 2018牛客网暑期ACM多校训练营(第三场) H - Shuffle Cards - [splay伸展树][区间移动][区间反转]
  19. virtualbox+vagrant学习-3-Vagrant Share-1-简介
  20. Android HTTP通讯

热门文章

  1. Contiki clock模块
  2. Cocos2d-x中定时器的使用
  3. chrome浏览器常用快捷键
  4. ES搜索排序,文档相关度评分介绍——TF-IDF—term frequency, inverse document frequency, and field-length norm—are calculated and stored at index time.
  5. Java 并发 —— Java 标准库对并发的支持及 java.util.concurrent 包
  6. AtCoder Beginner Contest 102
  7. PowerDesigner根据SQL文件生成PDM数据字典
  8. 「CF779B」「LOJ#10201.」「一本通 6.2 练习 4」Sherlock and His Girlfriend(埃氏筛
  9. Oracle中的float类型字段
  10. AR/VR-VR-Info-Micron-Insight:一镜观一屋:VR 将建筑设计变为现实