leetcode 46-Permutations and 47-Permutations II
Permutations
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].
Permutations II
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].
全排列问题
第二个题目基于第一题。第二题给的数组中还有可能相等的元素。
请參照 全 排 列
题一代码
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>>v;
v.clear();
vector<int>a;
next_c(v,nums.size(),a,nums,0);
return v;
}
void next_c(vector<vector<int>>&v,int n,vector<int>& a,vector<int>& b,int cur) /// b[]中的数能够同样
{
if(cur==n){
v.push_back(a);
}
else{
for(int i=0;i<n;i++)
if(!i||b[i]!=b[i-1])///
{
int c1=0,c2=0;
for(int j=0;j<cur;j++) if(a[j]==b[i]) c1++;
for(int j=0;j<n;j++) if(b[j]==b[i]) c2++;
if(c1<c2){
a.push_back(b[i]);// 不能用a[cur]=b[i];
next_c(v,n,a,b,cur+1);
a.pop_back();
}
}
}
}
};
题二代码。当然能够过题一。
class Solution {
public:
vector<vector<int>>v;
vector<vector<int>> permuteUnique(vector<int>& nums) {
v.clear();
vector<int>a;a.clear();
sort(nums.begin(),nums.end());
next_c(nums.size(),a,nums,0);
return v;
}
void next_c(int n,vector<int>& a,vector<int>& b,int cur) /// b[]中的数能够同样
{
if(cur==n){
v.push_back(a);
}
else{
for(int i=0;i<n;i++)
if(!i||b[i]!=b[i-1])///
{
int c1=0,c2=0;
for(int j=0;j<cur;j++) if(a[j]==b[i]) c1++;
for(int j=0;j<n;j++) if(b[j]==b[i]) c2++;
if(c1<c2){
a.push_back(b[i]); //a[cur]=b[i];
next_c(n,a,b,cur+1);
a.pop_back();
}
}
}
}
};
leetcode 46-Permutations and 47-Permutations II的更多相关文章
- leetcode 46. 全排列 及 47. 全排列 II
46. 全排列 问题描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3 ...
- leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列
字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- Leetcode之回溯法专题-47. 全排列 II(Permutations II)
Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- [LeetCode] 47. Permutations II 全排列 II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Backtracking Template for (Subsets, Permutations, and Combination Sum)
根据issac3 用Java总结了backtracking template, 我用他的方法改成了Python. 以下为template. def backtrack(ans, temp, nums, ...
- [LeetCode] 47. 全排列 II
题目链接 : https://leetcode-cn.com/problems/permutations-ii/ 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [ ...
- 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 ...
随机推荐
- 练习题 - js函数
代码贴出来 1 function Cat() { 2 getColor = function(){ console.log(1);} 3 return this; 4 } 5 Cat.getColor ...
- oracle中用rownum分页并排序的查询SQL语句
oracle的sql语句中没有limit,limit是mysql中特有的,在oracle中可用rownum来表示,用于查询结果中的前N行数据. 如要查询emp表中的前5行数据,可用如下语句: sele ...
- Redis Cluster管理
author:JevonWei 版权声明:原创作品 Redis 3开始支持了Cluster模式,增强了Redis的水平扩展能力,Redis Cluster的节点分片通过hash slot实现,每个节点 ...
- 修改Linux内核参数 减少TIME-WAIT
编辑/etc/sysctl.conf文件 增加 net.ipv4.tcp_syncookies = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_tw_recycle ...
- Linux PC开发环境搭建建议
搭建Linux PC开发环境 很早之前整理的在Linux(ubuntu)系统下搭建 PC开发环境的工具的推荐和简单说明,尽管现在有些已经不再使用,但还是要备份一下,作为以后的参考: package: ...
- iOS - 倒计时封装
+(NSString *)countdownStartTime:(NSString *)startTime{ NSString *TIME = [startTime substringToIndex: ...
- 80人环游世界(bzoj 2055)
Description 想必大家都看过成龙大哥的<80天环游世界>,里面的紧张刺激的打斗场面一定给你留下了深刻的印象.现在就有这么 一个80人的团伙,也想来一次环游世界. ...
- poj 3250 Bad Hair Day
Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21084 Accepted: 7202 Des ...
- css垂直居中 转
原文发布时间为:2009-07-26 -- 来源于本人的百度文章 [由搬家工具导入] CSS 垂直居中2009-07-24 09:09 前看到很多人一直都问这个问题,不过当时我没当一回事,因为在 CS ...
- poj 2528 Mayor's posters 线段树 || 并查集 离线处理
题目链接 题意 用不同颜色的线段覆盖数轴,问最终数轴上有多少种颜色? 注:只有最上面的线段能够被看到:即,如果有一条线段被其他的线段给完全覆盖住,则这个颜色是看不到的. 法一:线段树 按题意按顺序模拟 ...