vector<vector<int> > permuteUnique(vector<int> &num) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<vector<int> > res; vector<int> cur; hash_map<int,int> hm; for(int i=0;i<num.size();i++){ if(!hm.count(num[i])) hm.insert(pair<int,int>(num[i],1)); else hm[num[i]]++; } createPermutation(hm,cur,res); return res; }
void createPermutation(hash_map<int,int> hm,vector<int> cur,vector<vector<int> >& res){ if(hm.size()){ for(hash_map<int,int>::iterator it=hm.begin();it!=hm.end();it++){ vector<int> tVec(cur); hash_map<int,int> tMap(hm); tVec.push_back(it->first); if(tMap[it->first]==1) tMap.erase(it->first); else tMap[it->first]--; createPermutation(tMap,tVec,res); } } else res.push_back(cur); }
// ************************************************************ void testIndexScan (void) { RID insert[] = { {1,1}, {2,3}, {1,2}, {3,5}, {4,4}, {3,2}, }; int numInserts = 6; Value **keys; char *stringKeys[] = { "i1", "i11", "i13", "i17", "i23", "i52" }; testName = "random insertion order and scan"; int i, testint, iter, rc; BTreeHandle *tree = NULL; BT_ScanHandle *sc = NULL; RID rid; keys = createValues(stringKeys, numInserts); // init TEST_CHECK(initIndexManager(NULL)); for(iter = 0; iter < 50; iter++) { int *permute; // create permutation permute = createPermutation(numInserts); // create B-tree TEST_CHECK(createBtree("testidx", DT_INT, 2)); TEST_CHECK(openBtree(&tree, "testidx")); // insert keys for(i = 0; i < numInserts; i++) TEST_CHECK(insertKey(tree, keys[permute[i]], insert[permute[i]])); // check index stats TEST_CHECK(getNumEntries(tree, &testint)); ASSERT_EQUALS_INT(testint, numInserts, "number of entries in btree"); // execute scan, we should see tuples in sort order openTreeScan(tree, &sc); i = 0; while((rc = nextEntry(sc, &rid)) == RC_OK) { RID expRid = insert[i++]; ASSERT_EQUALS_RID(expRid, rid, "did we find the correct RID?"); } ASSERT_EQUALS_INT(RC_IM_NO_MORE_ENTRIES, rc, "no error returned by scan"); ASSERT_EQUALS_INT(numInserts, i, "have seen all entries"); closeTreeScan(sc); // cleanup TEST_CHECK(closeBtree(tree)); TEST_CHECK(deleteBtree("testidx")); free(permute); } TEST_CHECK(shutdownIndexManager()); freeValues(keys, numInserts); TEST_DONE(); }