Leetcode - Permutations I,II
Leetcode - 046 Permutations
全排列问题是回溯的典型例题:
1.可行解的组成形式是给定数组中的所有数的组合,故而大小上可以作为可行解判定条件
2.每次需要在剩下可被选中的集合中选择一个,创建mask数组
class Solution {
public:void dfs(vector<vector<int>> &vct, vector<int> &cur, vector<int>& nums,vector<int> & used){if (cur.size() == nums.size()){vct.push_back(cur);return;}for (int i = 0; i < nums.size(); ++i){if (used[i] == 0){cur.push_back(nums[i]);used[i] = 1;dfs(vct, cur, nums, used);used[i] = 0;cur.pop_back();}}}vector<vector<int>> permute(vector<int>& nums) {vector<vector<int>> vct;int n = nums.size();if (n <= 0)return vct;vector<int> cur;vector<int> used(n, 0);dfs(vct, cur, nums, used);return vct;}
};
Leetcode - 047. Permutations II
diff : 需要考虑val1 = val2 的情况,需要sort将相同元素聚类,然后可以参考前文 Leetcode - 040. Combination Sum II 去重的方法
class Solution {
public:void dfs(vector<vector<int>> &vct, vector<int> &cur, vector<int>& nums, vector<int> & used){if (cur.size() == nums.size()){vct.push_back(cur);return;}for (int i = 0; i < nums.size(); ++i){if (used[i] == 0){int pre_index = i - 1;bool repeated = false;while (pre_index >= 0 && nums[pre_index] == nums[i]){if (used[pre_index] == 0){repeated = true;break;}--pre_index;}if (repeated)continue;cur.push_back(nums[i]);used[i] = 1;dfs(vct, cur, nums, used);used[i] = 0;cur.pop_back();}}}vector<vector<int>> permuteUnique(vector<int>& nums) {vector<vector<int>> vct;vector<int> cur;int n = nums.size();if (n <= 0)return vct;vector<int> used(n, 0);dfs(vct, cur, nums, used);return vct;}
};
Leetcode - Permutations I,II相关推荐
- Leetcode - Subsets I,II
Leetcode - 078. Subsets 这道题重定义了什么叫可行解: 一般而言,可行解需要满足强约束性条件集,而本题的可行解就是单一弱约束性条件(distinct integers,只需要当前 ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode Permutations II(有重复元素的全排列)
问题:给出一个有重复元素的数组,要求输出全排列 思路:先排序,然后使用next_permutation 具体代码参考: https://github.com/wuli2496/OJ/tree/mast ...
- LeetCode Permutations(不重复数的所有排列)
问题:给出一个没有重复的数组,要求输出所有的排列 思路:先排序,然后使用STL中的next_permution 具体参考: https://github.com/wuli2496/OJ/tree/ma ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- 【DFS + 记忆化递归】LeetCode 140. Word Break II
LeetCode 140. Word Break II Solution1:我的答案 纯DFS,在第31个case时超时,还是记录一下.. class Solution { // DFS public ...
- 【重点!DP】LeetCode 639. Decode Ways II
LeetCode 639. Decode Ways II 参考网址:https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-639-d ...
- 【DFS + Backtracking】LeetCode 212. Word Search II
LeetCode 212. Word Search II Solution1:我的答案 暴力搜索..基于第79题的答案,真的是非常之慢啊!!! 快的方法均是基于字典树的方法,真是复杂.. class ...
- 【回文串5 重点+动态规划】LeetCode 132. Palindrome Partitioning II
LeetCode 132. Palindrome Partitioning II Solution1:我的答案1 直接模仿131那道题的DFS解法,找其中size最小的.果不其然,因为超时只能部分AC ...
最新文章
- loadrunner-4-3报告摘要
- “中科院博士后当辅警”,网友却吵翻了:家里有矿?
- 类型“unknown”上不存在属性“foreach”_JavaScript红宝书第四版精简解析系列--映射Map数据类型...
- Java Web整合开发(13) -- XML
- C++学习系列笔记(八)
- linux的挂载的问题,重启后就挂载就没有了
- 如何通过Facebook幻灯片广告让销售量疯涨
- 微服务的灾难:折磨人的环境!
- java作用域对象笔记_Java学习笔记(七)——对象
- DV、OV、EV :SSL证书那么多,我该选择哪一种?
- (二)NIST CSF-框架基础
- word图片另存为变黑色_Word的图片另存为图片格式的方法
- Astronauts UVA - 1391(2-sat)
- 您的Android版本不兼容,android – “你的设备与此版本不兼容”
- Find a Mother Vertex in a Graph
- The ninth day_打飞机项目
- Halcon读图出错怎么办?ReadImage常见错误与处理方法(C++)
- Python获取2021 获取全部基金信息 天天基金网 东方财富 基金排行
- 汇总!单片机(MCU)最强科普
- ecshop模板smarty foreach详解
热门文章
- 公务员计算机基础知识笔记,公务员计算机基础知识【精选】.doc
- mysql md 123456_MySQL修炼之路四
- LeetCode 769. Max Chunks To Make Sorted
- 蓝桥杯 ALGO-118 算法训练 连续正整数的和
- wifi 中间人攻击_揭秘3·15晚会“Wi-Fi中间人攻击”的操作原理
- angularjs过滤器(number)
- spring boot 处理自定义注解
- TechNet 晒文 - Windows 7 系列汇总
- kettle工具的设计原则
- RabbitMQ集群和失败处理