zoukankan      html  css  js  c++  java
  • leetcode Permutations II

    题目:和上一题一样,就是这时候给定的数字可能有重复。

    做法:只要将num排好序,然后判断如果当前的值等于前一个的话,那么就跳过,就不会有重复的人。果然是AC了。

     1 class Solution {
     2 public:
     3     vector<vector<int> > permuteUnique(vector<int> &num) 
     4     {
     5         vector<vector<int> > ans;
     6         if (num.size() == 1)
     7             {ans.push_back(num);return ans;}
     8         
     9         vector<vector<int> > post;
    10         vector<int> tmp;
    11         vector<int> cur;
    12         sort(num.begin(), num.end()); // 排好序
    13         for (int i = 0; i < num.size(); ++i)
    14         {
    15             tmp = num;
    16             if (i - 1 >= 0 && tmp[i] == tmp[i - 1]) continue; //如果重复数字就跳过
    17             tmp.erase(tmp.begin() + i);
    18             post = permuteUnique(tmp);
    19             for (int j = 0; j < post.size(); ++j)
    20             {
    21                 cur = post[j];
    22                 cur.insert(cur.begin(), num[i]);
    23                 ans.push_back(cur);
    24             }
    25         }
    26         return ans;   
    27     }
    28 };

    这个人的也是递归。这个人的好几种。有时间了再好好看看。

  • 相关阅读:
    MessageFormat理解,MessageFormat.format(Object obj)方法
    正则表达式
    数字处理类
    包装类
    遍历Map的4种方法(来自网络)
    集合类
    数组
    字符串
    语言基础
    Linux下使用openssl加解密
  • 原文地址:https://www.cnblogs.com/higerzhang/p/4058394.html
Copyright © 2011-2022 走看看