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 ...
随机推荐
- easyui 右键绑定事件
$(function(){ $('#hospitalTree').bind('contextmenu', function(e) { e.preventDefault(); ...
- hihoCoder #1758 加减
$\DeclareMathOperator{\lowbit}{lowbit}$ 题目大意 对于一个数 $x$,设它最低位的 1 是第 $i$ 位,则 $\lowbit(x)=2i$ . 例如 $\lo ...
- Date()和new Date()区别
当任意一个普通函数用于创建一类对象时,它就被称作构造函数,或构造器. new操作符来调用一个构造函数时,创建一个空对象obj, 将这个空对象的__proto__成员指向了构造函数对象的prototyp ...
- Nginx + tomcat 实现简单集群(基于反向代理方式)
一.安装 nginx 1. nginx依赖以下一些软件库,在安装之前请确保安装了这些软件库,它们包括:gcc,openssl,zlib,pcre(可通过rpm -q命令查询是否已安装) 可通过命令 r ...
- hdu 1423 最长上升递增子序列
#include <iostream> #include <cstdio> #include <cstring> using namespace std; ; in ...
- java修炼
java程序员修炼之道——大牛告诉我们应该好好学习与修炼以下知识与技能 ———————————————————— 一:Java语言学习(对线程(thread),串行化,反射,网络编程,JNI技术, ...
- SPOJ QTREE Query on a tree VI
You are given a tree (an acyclic undirected connected graph) with n nodes. The tree nodes are number ...
- 在android app中使用STL库(转)
1.在jni目录下新建Application.mk; 加入 APP_STL := stlport_static右边的值还可以换成下面几个: system - 使用默认最小的C++运行库,这样生成的应用 ...
- nodejs编写实例基础操作
学习视频地址 https://cnodejs.org/topic/5a72c66ace45d440451465c3 初始化项目 首先查看项目中是否有package.json 文件,如果有可执行np ...
- mysql 增加字段
alter table 表名 add 字段 varchar(500) comment '备注' default 0 after 字段;