【题目】
Given a collection of numbers, return all possible permutations.
For example,
[1,2,3]
have the following permutations:
[1,2,3]
,[1,3,2]
,[2,1,3]
,[2,3,1]
,[3,1,2]
,
and[3,2,1]
.
【分析】
无
【代码】
/*********************************
* 日期:2015-01-16
* 作者:SJF0115
* 题目: 46.Permutations
* 网址:https://oj.leetcode.com/problems/permutations/
* 结果:AC
* 来源:LeetCode
* 博客:
**********************************/
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int> > result;
if(num.empty()){
return result;
}//if
vector<int> visited;
// 递归
DFS(num,visited,result);
return result;
}
private:
void DFS(vector<int> &num,vector<int> &visited,vector<vector<int> > &result){
// 形成一个全排列
if(num.size() == visited.size()){
result.push_back(visited);
return;
}//if
vector<int>::iterator isVisited;
for(int i = 0;i < num.size();i++){
// 判断num[i]是否已经访问过
isVisited = find(visited.begin(),visited.end(),num[i]);
// 如果没有访问过
if(isVisited == visited.end()){
visited.push_back(num[i]);
DFS(num,visited,result);
visited.pop_back();
}//if
}//for
}
};
int main(){
Solution solution;
vector<int> num;
num.push_back(1);
num.push_back(2);
num.push_back(3);
num.push_back(4);
// 重新排列
vector<vector<int> > permutes = solution.permute(num);
// 输出
for(int i = 0;i < permutes.size();i++){
cout<<"[";
for(int j = 0;j < permutes[i].size();j++){
cout<<permutes[i][j];
}//for
cout<<"]"<<endl;
}
return 0;
}
【分析二】
直接调用STL中的next_permutation函数,如果在面试中面试官肯定会让你自己实现next_permutation函数
【代码二】
/*********************************
* 日期:2015-01-17
* 作者:SJF0115
* 题目: 46.Permutations
* 网址:https://oj.leetcode.com/problems/permutations/
* 结果:AC
* 来源:LeetCode
* 时间复杂度:O(n!)
* 空间复杂度:O(1)
* 博客:
**********************************/
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int> > result;
// 排序
sort(num.begin(),num.end());
// 直接调用STL中的next_permutation
do{
result.push_back(num);
}while(next_permutation(num.begin(),num.end()));
return result;
}
};
int main(){
Solution solution;
vector<int> num;
num.push_back(1);
num.push_back(2);
num.push_back(3);
num.push_back(4);
// 重新排列
vector<vector<int> > permutes = solution.permute(num);
// 输出
for(int i = 0;i < permutes.size();i++){
cout<<"[";
for(int j = 0;j < permutes[i].size();j++){
cout<<permutes[i][j];
}//for
cout<<"]"<<endl;
}
return 0;
}
【分析三】
自己实现next_permutation函数
【代码三】
class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int> > result;
// 排序
sort(num.begin(),num.end());
vector<int> tmp = num;
// 全排列的下一排列
result.push_back(num);
nextPermutation(num);
while(num!= tmp){
result.push_back(num);
nextPermutation(num);
}//while
return result;
}
private:
void nextPermutation(vector<int> &num) {
int count = num.size();
if(count == 0 || count == 1){
return;
}//if
// From right to left,find first digit which violate the increase
// call it partitionNumber
int index;
for(index = count-2;index >= 0;index--){
if(num[index+1] > num[index]){
break;
}//if
}//for
int tmp;
// -1 indicate largest
if(index != -1){
// From right to left,find first digit which large than partitionNumber
// call it changeNumber
for(int i = count-1;i > index;i--){
if(num[index] < num[i]){
// swap the partition index and changeNumber
tmp = num[index];
num[index] = num[i];
num[i] = tmp;
break;
}//if
}//for
}//if
// reverse all the digit on the right of partition index
for(int i = index+1,j = count - 1;i < j;i++,j--){
tmp = num[i];
num[i] = num[j];
num[j] = tmp;
}//for
}
};
分享到:
相关推荐
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] 二.解题思路 主要是有两种...
题目来源:https://leetcode-cn.com/problems/permutations/ 题目 给定一个 没有重复 数字的序列,返回其所有可能的全排列。 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,...
permutations2.py - 返回所有可能的唯一排列 3sum_2.py - 查找数组中所有唯一的三元组,其总和为零。 3sum.py - 查找数组中所有唯一的三元组,其总和为零。 first_last_pos_array.py - 找到给定目标值的开始和结束...
Source file for LeetCode Permutations Problem
leetcode怎么销号 LeetCode-Solutions :green_heart:My own LeetCode solutions No. Problem LeetCode 力扣 Python Go Solution Difficulty Tag 0017 Letter Combinations of a Phone Number Medium 回溯、暴力 0034...
permutations.cpp - kth_missing_number.cpp - 3sum2.cpp - 3sum.cpp - BST_from_preorder_traversal.cpp - 2sum.cpp - remove_adjacent_duplicates.cpp - 子集.cpp - 子集_2.cpp - Remove_Nth_Node_From_End_of_...
是时候回到Leetcode了2021-03-23 一些常用的内置函数: bin() :信息在collections.Counter :信息在itertools.permutations() :信息在记住,因为它在处理问题时确实可以提供帮助2021-03-24 开始处理HackerRank...
leetcode怎么计算空间复杂度是指 LeetCode-Solution my first solution of LeetCode 2015-5-7 Problem 95,98(80 already!) 我经常在递归的结束地方忘记return!!! 题型一:经典暴力递归:(里面涉及到重复不重复的...
leetcode 2 LeetCode - 30 Days 前言 相信所有写程式的人在面试前,总是在揣测在白板题会被问到什么问题,而我们最常听到的准备方式就是“刷”。上有数百个可能是面试官问你的题目,我把它...Permutations 目录 ref:
Permutations (交换 取子集两种方式) Trie树 11 中序遍历 无堆栈 (前序 后序) 12 map边删除 边输出 不太建议这么用。。。 以及其他基本用法 iterate 红黑树 13.set 16.unordered_map 与 map 17.最大m字段和 (动态...
permutations import random import itertools import collections from fractions import Fraction from collections import Counter import operator import re from functools import reduce from collections ...
permutations (normal and better), sum_3.py, unique_paths.py + unique_paths2.py, nQueens.py 很少包含外部库,所以下面的就可以了。 编译任何 C 程序: gcc -o X Xc 执行 python 脚本: chmod 755 X.py --> ....
2. combinations获取所有的组合情况,permutations可以获取所有的排列情况 3. 限制时间范围 4. 转化为字符型
leetcode题库 Little Algorithm 从 2020 年初开始,我在公众号《面向大象编程》上发表面试算法、LeetCode 题解相关文章,至今收获不少好评。此仓库是公众号内容的补充,包括公众号文章涉及到的题目的参考代码,以及 ...
leetcode Java 246 題目及解答 (英文) Contents 1 Rotate Array in Java 15 2 Reverse Words in a String II 19 3 Evaluate Reverse Polish Notation 21 4 Isomorphic Strings 25 5 Word Ladder 27 6 Word Ladder ...
All_Unique_Permutations Best_Time_To_Buy_And_Sell_Stocks_1 Best_Time_To_Buy_And_Sell_Stocks_2 Best_Time_To_Buy_And_Sell_Stocks_3 bst_Iterator 糖果 Character_Precedence Clone_Graph 组合_总和 Counting_...
AlgoHub囊括了 POJ, ZOJ, leetcode, HackerRank 等网站的经典题目(一些质量不高的题目则忽略),且 AlgoHub有非常简单的加题系统,用户不需要写一行代码即可自己添加题目,所以AlgoHub的题库还在飞速增长中。...