Lintcode: Previous Permuation
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的更多相关文章
- LintCode "Previous Permutation"
A reverse version of the Dictionary algorithm :) If you AC-ed "Next Permutation II", copy ...
- lintcode:previous permutation上一个排列
题目 上一个排列 给定一个整数数组来表示排列,找出其上一个排列. 样例 给出排列[1,3,2,3],其上一个排列是[1,2,3,3] 给出排列[1,2,3,4],其上一个排列是[4,3,2,1] 注意 ...
- Next Permutation & Previous Permutation
Next Permutation Given a list of integers, which denote a permutation. Find the next permutation in ...
- lintcode-51-上一个排列
51-上一个排列 给定一个整数数组来表示排列,找出其上一个排列. 注意事项 排列中可能包含重复的整数 样例 给出排列[1,3,2,3],其上一个排列是[1,2,3,3] 给出排列[1,2,3,4],其 ...
- [LintCode] Permuation Index
Given a permutation which contains no repeated number, find its index in all the permutations of the ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- 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 ...
随机推荐
- MYSQL PASSWORD()
https://www.pythian.com/blog/hashing-algorithm-in-mysql-password-2/ SELECT PASSWORD ("this_is_a ...
- Oracle用户、权限、角色管理(转)
http://blog.csdn.net/junmail/article/details/4381287 Oracle 权限设置一.权限分类:系统权限:系统规定用户使用数据库的权限.(系统权限是对用户 ...
- Nginx 上的 php-fpm 资源侵占问题
测试人员向我们反映:在Facebook平台的游戏比其它平台的游戏明显更慢.我询问,是不是因为FQ网络原因.他们说:不是,其它游戏也比较流畅.使用httpwatch查看了http请求,发现api.php ...
- oracle communities
应该常来这看看 https://www.oracle.com/communities/index.html
- 关于cocoa框架,你所要知道的一切(苹果官方文档,cocoa框架核心竞争力,必须收藏!)
https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/Accessib ...
- android jdbc 远程数据库
http://blog.csdn.net/conowen/article/details/7435231/
- pushViewController addSubview presentModalViewController视图切换
1.pushViewController和popViewController来进行视图切换,首先要确保根视图是NavigationController,不然是不可以用的, pushViewContro ...
- asp.net字符串的数学表达式计算结果
using System; using System.Collections.Generic; using System.Web; using System.CodeDom.Compiler; usi ...
- raspberryPi 拍照
调用python的库,学习raspberryPi的摄像头操作方法. 参考链接: https://www.raspberrypi.org/learning/getting-started-with-pi ...
- mysql中varchar(50)最多能存多少个汉字
首先要确定mysql版本4.0版本以下,varchar(50),指的是50字节,如果存放UTF8汉字时,只能存16个(每个汉字3字节) 5.0版本以上,varchar(50),指的是50字符,无论存放 ...