LeetCode: Next Permutation & Permutations1,2
Title:
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,3,2中后面的4,3,2都是逆序,因此需要替换1,找到1后面的稍微比它大的数。大致思路就是这样。
class Solution {
public:
void nextPermutation(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int nSize = num.size();
if (nSize <= ) return; int idx = nSize - ;
// 查找第一个下降的元素
while(--idx >= && num[idx] >= num[idx+]);
if (idx >= )
{
int i = nSize - ;
// 查找第一个比idx所指元素大的元素
while(num[i] <= num[idx])
{
--i;
}
swap(num[i], num[idx]);
// 反转后面所有元素,让它从小到大sorted
reverse(num.begin()+idx+, num.end());
}
else
{
reverse(num.begin(), num.end());
}
}
};
查找可用二分查找,虽然最后的时间都差不多
class Solution {
public:
void swap(int &i, int& j){
i = i+j;
j=i-j;
i=i-j;
} int find(vector<int> &num, int startIndex, int v){
int l = startIndex;
int h = num.size()-;
while (l <= h){
int m = (l+h)/;
if (num[m] > v){
l = m+;
}else{
h = m-;
}
}
return h;
}
void nextPermutation(vector<int> &num) {
if (num.size() <= )
return ;
int index = num.size()-;
int min;
int l,r;
while ( index > && num[index-] >= num[index]){
index--;
}
//cout<<index<<endl;
if (index == ){
//到头
l = ;
r = num.size()-;
while (l < r){
swap(num[l],num[r]);
l++;
r--;
}
}else{
//从后面的序列中找到比当前的数刚好大得那个数,并交换,然后重新从小到大排序
int temp = find(num,index,num[index-]);
//cout<<"temp: "<<temp<<endl;
swap(num[index-],num[temp]);
//sort(num.begin()+index,num.end());
l = index;
r = num.size()-;
while (l < r){
swap(num[l],num[r]);
l++;
r--;
}
}
}
};
Title:
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,[1,1,2]
have the following unique permutations:[1,1,2]
, [1,2,1]
, and [2,1,1]
.
思路,DFS搜索
void internalpermuteUnique(vector<int> &num, int index, vector<int> &perm, vector<vector<int> > &result) {
int size = num.size(); if (size == index) {
result.push_back(perm);
}
else {
for (int i = index; i < size; ++i) {
if ((i > index) && (num[i] == num[index])) {
continue;
}
else {
swap(num[index], num[i]);
} perm.push_back(num[index]);
internalpermuteUnique(num, index + , perm, result);
perm.pop_back();
swap(num[index], num[i]);
}
//sort(num.begin() + index, num.end());
}
} vector<vector<int> > permuteUnique(vector<int> &num) {
vector<vector<int> > result;
vector<int> perm; sort(num.begin(), num.end());
internalpermuteUnique(num, , perm, result); return result;
} Title: Permutation Sequence
The set [1,2,3,…,n]
contains a total of n! unique permutations.
By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
思路:题目不难,通过演算就可以找到规律,不过要注意下标,是从0开始的。所以最开始要将k--
class Solution{
public:
string getPermutation(int n,int k){
vector<int> v;
string result;
if (k <= )
return result;
if ( n < || n > )
return result;
for (int i = ; i <= n; i++)
v.push_back(i);
k--;
while (n > ){
int c = getFactorial(--n);
int a = k / c;
int b = k % c;
result.push_back(v[a]+'');
v.erase(v.begin()+a);
k = b;
}
result.push_back(v[]+'');
return result;
}
int getFactorial(int k){
if (k == )
return ;
else
return k * getFactorial(k-);
}
};
LeetCode: Next Permutation & Permutations1,2的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode Palindrome Permutation II
原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...
- LeetCode Palindrome Permutation
原题链接在这里:https://leetcode.com/problems/palindrome-permutation/ 题目: Given a string, determine if a per ...
- Java for LeetCode 060 Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Find Permutation 找全排列
By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decre ...
- [leetcode]Next Permutation @ Python
原题地址:https://oj.leetcode.com/problems/next-permutation/ 题意: Implement next permutation, which rearra ...
随机推荐
- 【HDOJ】【2089】不要62
数位DP cxlove基础数位DP第一题 用容斥把所有的不吉利数字去掉就得到吉利数字的数量= =(满足区间减法) //HDOJ 2089 #include<cmath> #include& ...
- check environment var
田+R cmd set XXX check environment var
- Caffe训练好的网络对图像分类
对于训练好的Caffe 网络 输入:彩色or灰度图片 做minist 下手写识别分类,不能直接使用,需去除均值图像,同时将输入图像像素归一化到0-1直接即可. #include <caffe/c ...
- Javascript的动态运动(1)
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 分享一个安装PE到硬盘的软件
Ton8pe_v5.0下载地址:http://pan.baidu.com/share/link?shareid=424350&uk=4180312589 电脑是XP,有光驱,但是没win8.1 ...
- hadoop 2.2.0的datanode中存储block的多个文件夹的负载均衡问题
hadoop的分布式文件系统HDFS的存储方式是,将数据分成block,分布式存储在整个hadoop集群的datanode中,每个block默认的大小是64M,这些block文件的具体存储位置是在ha ...
- lintcode 中等题:N Queens II N皇后问题 II
题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...
- Hibernate逍遥游记-第12章 映射值类型集合-001映射set(<element>)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- PowerDesigner概念模型的Notation设置
原文:PowerDesigner概念模型的Notation设置 在进行数据库设计模型时,分为概念模型设计和物理模型设计两种,概念模型主要是反映真是 世界中的业务关系,也就是我们常用的实体关系图.物理模 ...
- MSSQL2005数据库自动备份问题(到同一个局域网上的另一台电脑上)
有A数据库服务器,B本机: 我现在想通过在B机器上通过代码调用SQL来执行A数据库的备份到B机器上 调用的SQL语句为:Backup Database MYDATABASE To Disk ...