leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法
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, 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
思路:此题是我眼下做过的leetCode中感觉最难的题,它的难在于算法非常难自己短时间实现,假设曾经没有做过这种题,差点儿非常难非常快的有思路解出来。
在參考网上资料之前,我也尝试了好几种解法。可是没有一种能达到效果。最后參考资料。才恍然:这尼玛也能够这样做,可是臣妾非常难想到啊!
题目的总体思路是。从后往前读。当后面的数比前面的数大时,在开一个循环,从后開始于当前数比較。当比当前数大时,交换。然后再从当前数的后一位開始,直到最后反序就可以。
详细代码和凝视例如以下:
public class Solution {
public void nextPermutation(int[] nums) {
if(nums.length <= 1){
return;
}
for(int i = nums.length - 1;i > 0; i--){
if(nums[i] > nums[i-1]){//假设nums[i] > nums[i-1]
//再从后往前推断有否数字比nums[i-1]大
int j = nums.length - 1;
for(; j >= i -1;j--){
if(nums[j] > nums[i-1]){
break;//如有,则结束循环。保留j的值
}
}
//将nums[j]和nums[i-1]交换
int temp = nums[j];
nums[j] = nums[i-1];
nums[i-1] = temp;
//从i開始反序(即交换位置的地方開始反序)
for(j = 0; j < (nums.length - (i))/2;j++){
int k = nums.length - 1 - j;
int m = nums[j+i];
nums[j+i] = nums[k];
nums[k] = m;
}
return;
}
}
//假设到最后没有交换的数据,则说明是降序排列,须要升序排列
for(int i = 0; i < nums.length/2;i++){
int j = nums.length - 1 - i;//双指针排序,交换首尾相应的两两数据就可以
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法的更多相关文章
- [LeetCode] 31. Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- [leetcode]31. Next Permutation下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 2.1.12 Next Permutation 下一个字典序数组
对当前排列从后向前扫描,找到一对为升序的相邻元素,记为i和j(i < j).如果不存在这样一对为升序的相邻元素,则所有排列均已找到,算法结束:否则,重新对当前排列从后向前扫描,找到第一个大于i的 ...
- leetCode 90.Subsets II(子集II) 解题思路和方法
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- 31. Next Permutation (下一个全排列)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- leetCode 75.Sort Colors (颜色排序) 解题思路和方法
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- leetCode 87.Scramble String (拼凑字符串) 解题思路和方法
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- leetCode 85.Maximal Rectangle (最大矩阵) 解题思路和方法
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
随机推荐
- Spring再学习
一.主要版本变更 框架最早发布于2004年,其后发布了几个重大的版本更新:在Spring 2.0中提供对XML命名空间和AspectJ的支持:Spring 2.5中新增了注解驱动的配置:在Spring ...
- IT技术 | 让程序员抓狂的排序算法教学视频
点击「箭头所指处」可快速关注传智特刊微信号:CZTEKAN 原文地址:http://mp.weixin.qq.com/s?__biz=MjM5OTM4NDMyMg==&mid=20056820 ...
- NoSql 精粹导读图
- Eclipse 安装mybatis的编辑插件
1.MyEditor安装的方式 Eclipse 安装mybatis的编辑插件有以下4种方式,您可以使用下列方法之一来安装MyBatis的编辑器: Eclipse 3.7的(市场客户机安装):此图像拖放 ...
- Knots(找规律)
Knots Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- check cable connection PXE-M0F: Exiting intel PXE ROM no bootable device-- insert boot disk and pre
今天修电脑遇到一个问题,新买的电脑的原装的是linux,然后我按常规方式进入PE后重装系统,然后开机一直显示下面的代码,进不去: check cable connection PXE-M0F: Exi ...
- 什么是 CSS 预处理器?
什么是 CSS 预处理器? 就CSS本身而言,对于大多数Web前端从业人员来说就不是问题.学过CSS的人都知道,它不是一种编程语言.你可以用它开发网页样式,但是没法用它编程.换句话说,CSS基本上是 ...
- TimePicker控件、帧动画、补间动画
1.TimePicker控件 最近感觉每个开发平台的控件基本都差不多,在Android中控件的事件和.net控件直接写事件有一定的区别,net事件可以直接界面进行事件的绑定哈.不过在Silverlig ...
- 08JS高级 ——“继承”
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- JS赋值传递的问题
根据值的类型是基本类型值还是复杂类型的值在传递时会有不同. JS函数的参数传递是按值传递,基本类型值传递的是副本,复杂类型值传递的是引用.从而会影响原来的值,不会改变原来的复制前的引用. functi ...