【leetcode】Next Permutation(middle)
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
思路:
首先,从后向前找到第一对递增的的数 如 1 4 2 5 7 6 3 中的 5 7, 这表示,7 6 3 是一个连续递减的序列,即这三个数字可以组成的最大的数。 把这三个数翻转,就是 3 6 7即这三个数字可以组成的最小的数字。把5与这三个数字中比它大的最小的数字与它交换变成 1 4 2 6 7 5 3,即下一个较大的数字。注意,像2 3 1 3 3 这样后面比交换点数大一点的数有相同的时候,后面翻转后,交换后面的那个。
class Solution {
public:
void nextPermutation(vector<int> &num)
{
if (num.empty()) return; // in reverse order, find the first number which is in increasing trend (we call it violated number here)
int i;
for (i = num.size()-; i >= ; --i)
{
if (num[i] < num[i+]) break;
} // reverse all the numbers after violated number
reverse(begin(num)+i+, end(num));
// if violated number not found, because we have reversed the whole array, then we are done!
if (i == -) return;
// else binary search find the first number larger than the violated number
auto itr = upper_bound(begin(num)+i+, end(num), num[i]);
// swap them, done!
swap(num[i], *itr);
}
};
我自己写得:思路是先交换,再翻转。比上面的繁琐一点。
void nextPermutation(vector<int> &num) {
if(num.empty()) return;
bool b = false;
int chg1 = , chg2 = ;
int post = num.size() - ;
for(int i = num.size() - ; i >= ; --i)
{
if(num[post] > num[i])
{
b = true;
chg1 = i;
chg2 = post;
break;
}
post = i;
} if(!b)
{
reverse(num.begin(), num.end());
return;
} for(int i = chg1 + ; i < num.size(); i++)
{
if(num[i] > num[chg1] && num[i] <= num[chg2])
chg2 = i;
}
swap(num[chg1], num[chg2]); int l1 = chg1 + ;
int l2 = num.size() - ;
while(l1 < l2)
{
swap(num[l1++], num[l2--]);
}
return;
}
【leetcode】Next Permutation(middle)的更多相关文章
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Reorder List (middle)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Rotate List(middle)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【leetcode】Partition List(middle)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 【leetcode】Spiral Matrix(middle)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode】Rotate Image(middle)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【leetcode】Reverse Bits(middle)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 【leetcode】Surrounded Regions(middle)☆
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
随机推荐
- 让我们一起学Node.js-文章列表
新浪的博客最近不给力,只好在博客园落个窝.至此之后,技术随笔会在博客园以及新浪的博客上同时更新,如果新浪给力的话~~~ 如果你想看先前新浪博客上分享的技术,请点击此处 忘尘子新浪博客! 我是拜读了朴灵 ...
- iOS 8 牛刀小试
iOS 8 牛刀小试 1.UIWindow的bounds发生变化(Window本身发生了旋转) iOS 7之前Window的bounds不会随着方向而变化,但是到了iOS 8以后,随着设备方向的旋转, ...
- VS2013 Community配置OpenCV3.0.0
配置环境:32位win7系统+VS2013 Community版本 1.首先从OpenCV官网上下载最新版本的OpenCV for Windows. 2.直接双击打开下载得到的opencv-3.0.0 ...
- MetadataType来帮助entity framework自动生成的代码进行标注
真的是,用的时候就四处google,还是记在这里容易找 [MetadataType(typeof(Person.Metadata))] public partial class Person { pr ...
- iTool拷贝app到电脑上
iTool拷贝app到电脑上 方法一. iTool找到你的app, 归档在桌面, 桌面就生成了ipa, 其实ipa是一个压缩包, 使用解压软件解压之后 生成Payload文件夹, 点开就可以看到Clo ...
- MySQL 同主机不同数据库之间的复制
MySQL同主机不同数据库的复制命令:注意运行在Terminal中,不运行在MySQL命令行中. mysqldump Portal_DEV -u root -ppassword1 --add-drop ...
- 160809212田京诚C语言程序设计实验2 选择结构程序设计_进阶
实验2-6 猜数字游戏 实验要求: 编写一个C程序实现一个[1-100]以内的猜数字游戏. (1) 系统随机产生一个[1-100]之间的随机数. (2) 输入任意一个数字.数字 ...
- 跟着百度学PHP[4]-OOP面对对象编程-3-实例化一个对象
当定义好类后,我们使用new关键字来实例化一个对象! 格式: $object = new 类名; <?php class Person{ private $name; "; priva ...
- Hashtable和HashMap类的区别
Hashtable和HashMap类有三个重要的不同之处.第一个不同主要是历史原因.Hashtable是基于陈旧的Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现. ...
- hdu1054 树形dp&&二分图
B - Strategic Game Time Limit:10000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...