LeetCode 047 Permutations II
来源:互联网 发布:淘宝店铺的权重 编辑:程序博客网 时间:2024/04/30 14:26
题目要求和上一题类似,不过给出的整数可能有重复,输出不能含有相同的排列。
参考第31题,Next Permutation。需要注意的是把循环里的一些大于号改成大于等于,这样就不会产生重复的排列。
代码:
bool getNext(vector<int>& order) { int j, k; k = order.size() - 1; while(k > 0 && order[k - 1] >= order[k]) k--; //注意大于等于 if(k == 0) { j = 0; k = order.size() - 1; while(j < k) { swap(order[j], order[k]); j++; k--; } return true; } j = k - 1; k = order.size() - 1; while(j < k && order[j] >= order[k]) k--; //注意大于等于 swap(order[j], order[k]); j++; k = order.size() - 1; while(j < k) { swap(order[j], order[k]); j++; k--; } return true; } vector<vector<int>> permuteUnique(vector<int>& nums) { vector<vector<int>> result; vector<int> origin(nums); do{ result.push_back(nums); getNext(nums); }while(nums != origin); return result; }
0 0
- LeetCode 047 Permutations II
- LeetCode 047 Permutations II
- LeetCode 047 Permutations II
- leetcode 047 Permutations II
- [LeetCode]047-Permutations II
- LeetCode 047 Permutations II
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- Leetcode: Permutations and Permutations II
- leetcode-permutations and permutations II
- Leetcode:Permutations与Permutations II
- leetcode 047 —— Permutations II
- leetcode解题方案--047--Permutations II
- LeetCode: Permutations II
- LeetCode Permutations II
- LeetCode: Permutations II
- 进度条
- 设计模式 - 设计模式概述
- 使用freemarker生成xml模板
- 非农户口迁回原籍所需要的材料
- 通用型线程池框架 C++
- LeetCode 047 Permutations II
- React Native之底层源码分析篇
- 统计字符串中重复的字符个数并输出
- Amabri 2.1安装HDP2.3.2 之 一、HDP介绍
- JMX之MBean介绍
- iOS 数据库篇1—SQLite简单介绍
- 训练3 习题18
- 详解OpenGL的坐标系、投影和几何变换-矩阵压栈思想/矩阵列式存储
- 第一天:Java web 的第一个例子 HelloWeb