Next Permutation leetcode java
题目:
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
题解:
本文讲解转自Code Ganker稍稍修改“http://blog.csdn.net/linhuanmars/article/details/20434115”
“这道题是给定一个数组和一个排列,求下一个排列。算法上其实没有什么特别的地方,主要的问题是经常不是一见到这个题就能马上理清思路。下面我们用一个例子来说明,比如排列是(2,3,6,5,4,1),求下一个排列的基本步骤是这样:
1) 先从后往前找到第一个不是依次增长的数,记录下位置p。比如例子中的3,对应的位置是1;
2) 接下来分两种情况:
(1) 如果上面的数字都是依次增长的,那么说明这是最后一个排列,下一个就是第一个,其实把所有数字反转过来即可(比如(6,5,4,3,2,1)下一个是(1,2,3,4,5,6));
(2) 否则,如果p存在,从p开始往后找,找找找,找到第一个比他小的数,然后两个调换位置,比如例子中的4。调换位置后得到(2,4,6,5,3,1)。最后把p之后的所有数字倒序,比如例子中得到(2,4,1,3,5,6),
这个即是要求的下一个排列。
i--;
j++;
j--;
swap(num,i,j);
}
reverse(num, i+1,num.length-1);
}
num[i] = num[j];
num[j] = tmp;
}
swap(num, i++, j--);
}
Next Permutation leetcode java的更多相关文章
- Next Permutation - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Next Permutation - LeetCode 注意点 如果是字典序最大的串则要返回字典序最小的串 解法 解法一:参见:http://www.cn ...
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- LeetCode算法题-Letter Case Permutation(Java实现)
这是悦乐书的第315次更新,第336篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第184题(顺位题号是784).给定一个字符串S,将每个字母单独转换为小写或大写以创建另 ...
- Permutation Sequence leetcode java
题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- Regular Expression Matching leetcode java
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- Sqrt(int x) leetcode java
Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735 题目: Implement int sqrt(int x). Co ...
- 60. Permutation Sequence (JAVA)
The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and labeling all of the ...
- ZigZag Conversion leetcode java
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
随机推荐
- php面向对象编程self和static的区别
在php的面向对象编程中,总会遇到 class test{ public static function test(){ self::func(); static::func(); } public ...
- php中按指定标识及长度替换字符的方法代码
/** * 按指定标识及长度替换字符 * @param $str * @param int $start 开始的位数 * @param int $end 后面保留的位数 * @param string ...
- python 全栈开发,Day20(object类,继承与派生,super方法,钻石继承)
先来讲一个例子 老师有生日,怎么组合呢? class Birthday: # 生日 def __init__(self,year,month,day): self.year = year self.m ...
- this和super不能同时出现在构造方法中
package com.bjpowernode.t02inheritance.c09; /* * 使用super调用父类的构造方法 */public class TestSuper02 { publi ...
- 去除HTML5 SUMMARY 标签前的三角形
在CSS添加如下代码(Chrome): details summary::-webkit-details-marker { display:none; }
- SqlServer索引碎片
1.产生碎片的操作 通过sys.dm_index_physical_stats来查看,索引上的页不在具有连续性时就会产生碎片,碎片是索引上页拆分的物理结果. (1).插入操作: INSERT操作在聚集 ...
- python使用ssdb的队列,用于替换canal+rabbitmq
# pip install -i https://mirrors.aliyun.com/pypi/simple/ pyssdb import pyssdb c = pyssdb.Client('172 ...
- Java Timer, TimerTask, Timer.Schedule
schedule的意思(时间表.进度表) timer.schedule(new TimerTask(){ void run()},0, 60*60*1000);timer.schedule(new M ...
- poj 1789 每个字符串不同的字母数代表两个结点间的权值 (MST)
题目大意是就是给出n个长度为7的字符串,每个字符串代表一个车,定义车的距离是两个字符串间不同字母的个数,题目要求的数不同的车的距离的最小值,即所求的就是最小生成树 Sample Input 4aaaa ...
- Struts2的动态Action和全局跳转视图以及配置各项默认值
1:Struts2的默认访问后缀是.action(特别需要注意的是改了配置文件web.xml或者struts.xml需要重启服务器) 2:Struts2中常用的常量介绍:<!-- 一:全局配置 ...