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 ...
随机推荐
- Arduino红外遥控系列教程2013——发射与接收
教程一:红外接收教程本教程共四步操作,将教大家如何收集电视红外遥控器的编码 视频链接:http://v.youku.com/v_show/id_XNTE2NjQ5NTcy.html 第一步:电路连接, ...
- VC++学习之一
对于编程语言,我一直认为它只是一种工具,就像锤子,斧头一样,每种语言都用自己比较适用的地方,用的时候拿来就可以了.这种思想让我对语言没有做过很仔细的学习,虽然频繁使用过C,C++,java,C#,De ...
- cf455A Boredom
A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- iscroll5 版本下的 上拉,下拉 加载数据
上拉时候只是加载第一页的内容,可根据实际情况修改其中的代码. <section id="downwraper" class="nodeBottom bot0 bgf ...
- #include <boost/array.hpp>
Boost的array,元素可以是std::string #include <iostream> #include <string> #include <boost/ar ...
- android 滚动条
ScrollView简单应用 activity中经常只是一个LinearLayout,但这样的话,如果activity内容超过一屏,无法滚动查看下面的内容. 这时只需在外面嵌套一个ScrollView ...
- 创建渐进式jpeg图片
<?php // Create an image instance $im = imagecreatefromjpeg('test.jpg'); // Enable interlancing ...
- 最新首发Eclipse+CDT+android-ndk写纯c++安卓应用(附openGL Es)
首先下载eclipse和cdt.我的版本依次是:Version: Indigo Service Release 2和Version: 1.0.0.201202111925,再下载windows的ndk ...
- properties文件value换行处理方式
书写方式如下,就可以允许key1的value值换行了,但是整个过程要注意不要在文件中出现任何的非英文非半角的字符 key1=Where did you take the picture?\ ...
- MySql命令行命令和SQL语句
一.常用mysql命令行命令 1.启动MYSQL服务 net start mysql 停止MYSQL服务 net stop mysql 2.netstat -na|findstr 3306 查看被监听 ...