[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 ...
随机推荐
- Docker之数据卷Volume(七)
一.简介 Docker数据卷(volume)机制.volume是存在于一个或多个容器中的特定文件或文件夹,这个目录以独立于联合文件系统的形式在宿主机中存在,并为数据的共享与持久化提供便利. 1)v ...
- 剑指offer 2.字符串 替换空格
题目描述 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. public class Re ...
- Android的发展历史
Android一词最早出现于法国作家利尔亚当(Auguste Villiers de l’Isle-Adam)在1886年发表的科幻小说<未来夏娃>(L’ève future)中.他将外表 ...
- C#编程经验-enum and struct
enum,store fixed values,use array replace,not use this data-structurestruct,store several variables, ...
- python内置函数,匿名函数
一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...
- WM消息对应的Message消息中的Lparam和WParam
具体的消息表示: 1. WM_PAINT消息,LOWORD(lParam)是客户区的宽,HIWORD(lParam)是客户区的高 2. 滚动条WM_VSCROLL或WM_HSCROLL消息,LOWOR ...
- 搭建基于MySQL的读写分离工具Amoeba
搭建基于MySQL的读写分离工具Amoeba: Amoeba工具是实现MySQL数据库读写分离的一个工具,前提是基于MySQL主从复制来实现的: 实验环境(虚拟机): 主机 角色 10.10.10.2 ...
- MySQL学习笔记Windows篇<一> Welcome to MySQL
MySQL安装完毕后没有图形化操作界面,图形化管理界面需要另行安装,个人比较喜欢Navicat,界面更像SQLserver: 此篇学习笔记所有操作均使用命令行中完成: 1.开启/停止服务 使用MySQ ...
- 05python下
循环loop 有限循环 ,次数限制 无限循环=死循环 continue 结束本次循环,继续下一次循环 break 跳出整个当前的循环 forwhilebreak continue 数据类型 整数 字符 ...
- linux 查看任务运行时间
ps -eo pid,tty,user,comm,lstart,etime | grep pid 15590 ? meicai java Wed Sep 26 20:04:31 2018 35-15: ...