[leetcode]31. Next Permutation下一个排列
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 and use only constant 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,23,2,1 → 1,2,31,1,5 → 1,5,1
思路:
给定一个排列,按照字典序把下一个排列找出来。
Solution1:







code:
/*
Time: O(n). We travese the given array
Space: O(1). We only used constant extra space.
*/ class Solution {
public void nextPermutation(int[] nums) {
// corner case
if (nums == null || nums.length == 0) return; int replace = nums.length - 2; //为何初始化为这个?因为下面要replace 和replace +1 比较
while (replace >= 0) {
if (nums[replace] < nums[replace + 1]) break;//从右往左扫
replace--;
}
if (replace < 0) {//说明数组总没有出现nums[replace]<nums[replace+1], 则数组为 654321 这种排序
Arrays.sort(nums); //返回题干说述的升序排列
return;
}
int lgrIdx = replace + 1;
//从nums[replace]往后,开始找剩下数字中,大于且最接近的nums[replace]的值
while (lgrIdx < nums.length && nums[lgrIdx] > nums[replace]) {
lgrIdx++;
}
int temp = nums[replace];
nums[replace] = nums[lgrIdx - 1];
nums[lgrIdx - 1] = temp;
Arrays.sort(nums, replace + 1, nums.length);
}
}
[leetcode]31. Next Permutation下一个排列的更多相关文章
- [LeetCode] 31. Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically ne ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【LeetCode每天一题】Next Permutation(下一个排列)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode(31): 下一个排列
Medium! 题目描述: (请仔细读题) 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列) ...
- Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 031 Next Permutation 下一个排列
实现获取下一个排列函数,这个算法需要将数字重新排列成字典序中数字更大的排列.如果不存在更大的排列,则重新将数字排列成最小的排列(即升序排列).修改必须是原地的,不开辟额外的内存空间.这是一些例子,输入 ...
- lintcode:next permutation下一个排列
题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] ...
- 31. Next Permutation (下一个全排列)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
随机推荐
- [随笔][Life][咖啡][咖啡分类]
转载自:https://www.chalook.net/doc/201607/4710.shtml
- laravel 使用 php artisan make:model到指定目录(controller同理)
在 \app\Models 目录下创建一个BusinessProduct模型文件 D:\htdocs\PHPTutorial\WWW\gf>php artisan make:model /Mod ...
- postgreSQL数据库limit分页、排序
postgreSQL数据库limit分页.排序 语法: select * from persons limit A offset B; 解释: A就是你需要多少行: B就是查询的起点位置. 示例 ...
- Python程序打包之PyInstaller
1.背景说明 [Python版本]Python 2.7.14 [系统平台]Windows 7 [优缺点描述]据说PyInstaller 比较慢,但是PyInstaller打包出来的exe简洁(就一个文 ...
- 黄聪:微信URL Scheme,URL唤起微信
微信URL Scheme 在外部浏览器中,可以通过<a href="weixin://">打开微信APP 也可以通过加一些参数,打开微信APP里的指定页面 <a ...
- Win7系统安装Centos7.0双系统(三)
4.6语言选择 4.7安装信息设置,除以下几项改动其他都可默认. 软件选择(默认最小):带GUI的服务器或GNOME桌面,可根据使用需要选择安装软件. 磁盘分区:Linux默认可分为3个分区,分别是b ...
- 学习 Hadoop3.0 一、Hadoop3.0的安装与配置
一.JDK1.8的安装 添加ppa sudo add-apt-repository ppa:webupd8team/java sudo apt-get update 安装Oracle-java-ins ...
- FileProvider相关 Failed to find configured root that contains
问题: 使用FileProvider构造SD卡中文件uri时异常 java.lang.IllegalArgumentException: Failed to find configured root ...
- 从OsChina Git下载项目到MyEclipse中
前提是,拥有权限下载 1.进入MyEclipse,点击File-->Import,选择Git,点击“Next”,如下图: , 2.选择“URI”,点击"Next" 3.输入项 ...
- Vue自学笔记--项目的创建
一.项目的创建 1.必须要安装nodejs 2.搭建vue的开发环境 ,安装vue的脚手架工具 官方命令行工具 npm install --global vue-cli / ...