GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/algorithms/cpp/permutations/permutations.II.cpp at master · koala-good/leetcode Expand file tree/ Copy pathpermutations.II.cppMore file actions 100 lines (85 loc) · 2.22 KB / Copy pathpermutations.II.cppFile metadata and controls100 lines (85 loc) · 2.22 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 // Source : https://oj.leetcode.com/problems/permutations-ii/ // Author : Hao Chen // Date : 2014-06-21 /********************************************************************************** * * 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]. * * **********************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <time.h> #include <iostream> #include <vector> #include <algorithm> using namespace std; // To deal with the duplication number, we need do those modifications: // 1) sort the array [pos..n]. // 2) skip the same number. vector<vector<int> > permute(vector<int> &num) { vector<vector<int> > vv; vv.push_back(num); if (num.size() <2){ return vv; } int pos=0; while(pos<num.size()-1){ int size = vv.size(); for(int i=0; i<size; i++){ //sort the array, so that the same number will be together sort(vv[i].begin()+pos, vv[i].end()); //take each number to the first for (int j=pos+1; j<vv[i].size(); j++) { vector<int> v = vv[i]; //skip the same number if (j>0 && v[j]==v[j-1]){ continue; } int t = v[j]; v[j] = v[pos]; v[pos] = t; vv.push_back(v); } } pos++; } return vv; } void printVector( vector<int>& pt) { cout << "{ "; for(int j=0; j<pt.size(); j++){ cout << pt[j] << " "; } cout << "} " << endl; } int main(int argc, char** argv) { int n = 3; if (argc>1){ n = atoi(argv[1]); } srand(time(NULL)); vector<int> v; for (int i=0; i<n; i++) { v.push_back(random()%n+1); } /*v[0] =0; v[1] =1; v[2] =0; v[3] =0; v[4] =9;*/ printVector(v); cout << "-----------------" << endl; vector<vector<int> > vv; vv = permute(v); for(int i=0; i<vv.size(); i++) { printVector(vv[i]); } return 0; } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |