注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
Java版的OK, C++版的为啥总是过不了{2, 2, 3, 3, 3}这个case, 返回是{}。代码应该没错,leetcode custom testcase结果是正确的。
lintcode答案Java:http://www.jiuzhang.com/solutions/permutations-ii/
C++版代码:- class Solution {
- public:
- /**
- * @param nums: A list of integers.
- * @return: A list of unique permutations.
- */
- vector<vector<int> > permuteUnique(vector<int> &nums) {
- // write your code here
- vector<vector<int> > results;
- vector<int> permutation;
- int n = nums.size();
- if (n == 0)
- return results;
- vector<bool> visited(0);
-
- sort(nums.begin(), nums.end());
-
- helper(nums, n, visited, permutation, results);
-
- return results;
- }
-
- void helper(vector<int> &nums, int n, vector<bool> &visited, vector<int> &permutation,
- vector<vector<int> > &results) {
-
- if (permutation.size() == n) {
- results.push_back(permutation);
- return;
- }
-
- for (int i = 0; i < n; i++) {
- if (visited[i] == 1 || i != 0 && nums[i] == nums[i -1] && visited[i - 1]) {
- continue;
- }
- visited[i] = 1;
- permutation.push_back(nums[i]);
- helper(nums, n, visited, permutation, results);
- permutation.pop_back();
- visited[i] = 0;
- }
- }
- };
复制代码 |