leetcode 46. 全排列 及 47. 全排列 II
46. 全排列
问题描述
给定一个没有重复数字的序列,返回其所有可能的全排列。
示例:
输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
问题分析
代码
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
int n = nums.size();
vector<vector<int>> ans;
vector<int> path;
vector<bool> flag(n,false);
backtrack(ans,path,nums,n,0,flag);
return ans;
}
void backtrack(vector<vector<int>> &ans,vector<int> &path,vector<int>& nums,int n,int fir,vector<bool> &flag)
{
if(fir == n)
{
ans.push_back(path);
return;
}
for(int i = 0; i < n; i++)
{
if(!flag[i])
{
path.push_back(nums[i]);
flag[i] = true;
backtrack(ans,path,nums,n,fir+1,flag);
path.pop_back();
flag[i] = false;
}
}
}
};
结果:
执行用时 :12 ms, 在所有 cpp 提交中击败了95.23%的用户
内存消耗 :9.3 MB, 在所有 cpp 提交中击败了63.04%的用户
47. 全排列 II
问题描述
给定一个可包含重复数字的序列,返回所有不重复的全排列。
示例:
输入: [1,1,2]
输出:
[
[1,1,2],
[1,2,1],
[2,1,1]
]
问题分析
代码
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
int n = nums.size();
vector<vector<int>> ans;
vector<int> path;
vector<bool> flag(n,0);
sort(nums.begin(),nums.end());
backtrack(nums,flag,n,0,ans,path);
return ans;
}
void backtrack(vector<int>& nums,vector<bool> &flag,int n,int fir,vector<vector<int>>&ans,vector<int> &path)
{
if(fir == n){
ans.push_back(path);
return;
}
for(int i = 0; i < n; i++)
{
if(flag[i] == 0){
if(i > 0 && nums[i] == nums[i-1] && flag[i-1] == 1)continue;
path.push_back(nums[i]);
flag[i] = 1;
backtrack(nums,flag,n,fir+1,ans,path);
path.pop_back();
flag[i] = 0;
}
}
}
};
结果
执行用时 :16 ms, 在所有 C++ 提交中击败了79.11%的用户
内存消耗 :10.1 MB, 在所有 C++ 提交中击败了62.00%的用户
leetcode 46. 全排列 及 47. 全排列 II的更多相关文章
- Leetcode之回溯法专题-47. 全排列 II(Permutations II)
Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...
- [LeetCode] 47. 全排列 II
题目链接 : https://leetcode-cn.com/problems/permutations-ii/ 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [ ...
- [LeetCode] 47. Permutations II 全排列 II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode 47——全排列 II
1. 题目 2. 解答 在 LeetCode 46--全排列 中我们已经知道,全排列其实就是先确定某一个位置的元素,然后余下就是一个子问题.在那个问题中,数据没有重复,所以数据中的任意元素都可以放在最 ...
- Java实现 LeetCode 47 全排列 II(二)
47. 全排列 II 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] class Solut ...
- 47. 全排列 II
47. 全排列 II 题意 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2]输出:[ [1,1,2], [1,2,1], [2,1,1]] 解题思路 去重的全排列 ...
- 每日一题-——LeetCode(46)全排列
题目描述: 给定一个没有重复数字的序列,返回其所有可能的全排列.输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ...
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
随机推荐
- LuoguP4759 [CERC2014]Sums 题解
Content 给定 \(t\) 组数据,每组数据给定一个数 \(n\),判断 \(n\) 是否能够分解成连续正整数和,能的话给出最小数最大的方案. 数据范围:\(1\leqslant n\leqsl ...
- Linux(centos)使用nc命令发送测试数据
安装 yum -y install nmap-ncat 简单使用 nc -lk 7777 # 开启一个本地7777的TCP协议端口,由客户端主动发起连接,一旦连接必须由服务端发起关闭 nc -vw 2 ...
- JAVA中数组(Array)、字符串(String)、集合(List、Set)相互转换
1.数组转List String[] arr = new String[]{"A", "B", "C"}; List list = Arra ...
- 查找MySql的配置文件my.cnf所在路径
Linux系统 linux 上可以使用 mysql --help|grep my.cnf 过滤查看 [root@localhost etc]# mysql --help|grep my.cnf ord ...
- mac osx 准备 nanogui 记录
!!版权声明:本文为博主原创文章,版权归原文作者和博客园共有,谢绝任何形式的 转载!! 作者:mohist mac osx : 10.14.6 Mojave. 之前没有配置openGL相关开发环境,自 ...
- 【LeetCode】98. Validate Binary Search Tree 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 BST的中序遍历是有序的 日期 题目地址:ht ...
- 【LeetCode】58. Length of Last Word 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 双指针 单指针 日期 题目地址:https: ...
- 【LeetCode】162. Find Peak Element 解题报告(Python)
[LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...
- Primitive Roots(poj1284)
Primitive Roots Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3928 Accepted: 2342 D ...
- SROP
先放个例题吧,原理后面有时间再更:BUUCTF ciscn_2019_s_3 保护只开了nx 1 signed __int64 vuln() 2 { 3 signed __int64 v0; // r ...