Given a list of integers, which denote a permutation.

Find the previous permutation in ascending order.

Note
The list may contains duplicate integers. Example
For [1,3,2,3], the previous permutation is [1,2,3,3] For [1,2,3,4], the previous permutation is [4,3,2,1]

Next Permutation很像,只不过条件改成

for (int i=nums.lenth-2; i>=0; i--)

  if (nums[i] > nums[i+1]) break;

for (int j=i; j<num.length-1; j++)

  if (nums[j+1]>=nums[i]) break;

 public class Solution {
/**
* @param nums: A list of integers
* @return: A list of integers that's previous permuation
*/
public ArrayList<Integer> previousPermuation(ArrayList<Integer> nums) {
// write your code
if (nums==null || nums.size()==0) return nums;
int i = nums.size()-2;
for (; i>=0; i--) {
if (nums.get(i) > nums.get(i+1)) break;
}
if (i >= 0) {
int j=i;
for (; j<=nums.size()-2; j++) {
if (nums.get(j+1) >= nums.get(i)) break;
}
int temp = nums.get(j);
nums.set(j, nums.get(i));
nums.set(i, temp);
}
reverse(nums, i+1);
return nums;
} public void reverse(ArrayList<Integer> nums, int k) {
int l = k, r = nums.size()-1;
while (l < r) {
int temp = nums.get(l);
nums.set(l, nums.get(r));
nums.set(r, temp);
l++;
r--;
}
}
}

Lintcode: Previous Permuation的更多相关文章

  1. LintCode "Previous Permutation"

    A reverse version of the Dictionary algorithm :) If you AC-ed "Next Permutation II", copy ...

  2. lintcode:previous permutation上一个排列

    题目 上一个排列 给定一个整数数组来表示排列,找出其上一个排列. 样例 给出排列[1,3,2,3],其上一个排列是[1,2,3,3] 给出排列[1,2,3,4],其上一个排列是[4,3,2,1] 注意 ...

  3. Next Permutation & Previous Permutation

    Next Permutation Given a list of integers, which denote a permutation. Find the next permutation in ...

  4. lintcode-51-上一个排列

    51-上一个排列 给定一个整数数组来表示排列,找出其上一个排列. 注意事项 排列中可能包含重复的整数 样例 给出排列[1,3,2,3],其上一个排列是[1,2,3,3] 给出排列[1,2,3,4],其 ...

  5. [LintCode] Permuation Index

    Given a permutation which contains no repeated number, find its index in all the permutations of the ...

  6. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  7. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  8. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  9. SVN:Previous operation has not finished; run 'cleanup' if it was interrupted

    异常处理汇总-开发工具  http://www.cnblogs.com/dunitian/p/4522988.html cleanup failed to process the following ...

随机推荐

  1. 判断i在字符串中出现的次数(2016.1.12P141-1)

    // 方法一,利用substring截取获得出现的次数 String number = "iminigrikejijavabi"; String a = number; int c ...

  2. MvcPager分页控件的使用

    1.引入MvcPager.dll(MvcPager分页控件:http://www.webdiyer.com/mvcpager/) 2.后台C# Controller: //Ddemo使用Webdiye ...

  3. git-svn

    sudo apt-get install git-svn svn作为一个优秀源码版本的管理工具,可以适合绝大多数项目.但是因为它的采用中心化管理,不可避免的存在本地代码的备份和版本管理问题.也就是说对 ...

  4. 通过全局设置过滤器,就能让所有窗口都可移动,而不是都要继承指定QDialog

    #include "appinit.h" #include <QMouseEvent> #include <QApplication> #include & ...

  5. 流媒体学习四------- ortp队列的实现

    在ortp中实现了一个通用的队列,每一个队列包括三个实体,分别是队列.消息块和数据块,这三个实体分别对应queue_t.msgb和datab结构体. queue_t的定义如下所示: typedef s ...

  6. android通过pc脚本执行sqlite3脚本

    最近在调研市面上的一些android db框架,需要经常重复的输入一堆比如 adb shell cd /data/data/com.example.testandroiddb/databases sq ...

  7. 文件操作_菜单<代码>

    info文件中的内容为: { "河北省": {"石家庄": {"无极县":"", "高邑县":&qu ...

  8. 图解SSL/TLS协议

    本周,CloudFlare宣布,开始提供Keyless服务,即你把网站放到它们的CDN上,不用提供自己的私钥,也能使用SSL加密链接. 我看了CloudFlare的说明(这里和这里),突然意识到这是绝 ...

  9. .Net 文件名补位

    文件以name-1.pdf.name-2.pdf.name-3.pdf......name-80.pdf命名,传到数据库中排序混乱:1之后为10,2之后是20,所以要把文件名中的数字补位变成相同位数, ...

  10. C# serialport

    最近使用C#写串口的程序,实现串口助手的功能,参考文档记录于此. 参考文档 http://blog.csdn.net/geekwangminli/article/details/7851673 htt ...