由买买提看人间百态

topics

全部话题 - 话题: permut
1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
k***t
发帖数: 276
1
S=max score
individual points p1,p2,...,pn
how many permutations of p1,...,pn (multiple occurences of px allowed) give
us total score S.
This is essentially based on American football, where each drive can end in
2 points (safety), 3 points (field goal), 6 points (td) or 7 points (td+fg).
Example:
S=47
p1=2,p2=4,p3=7
Answer: 226234 permutations
3,3,3,3,7,7,7,7,7,:(126 permutations)
3,3,3,3,3,3,3,3,3,3,3,7,7,:(78 permutations)
2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,:(16 permutations)
2,2,2,2,3,3,3,3,3,3... 阅读全帖
k***t
发帖数: 276
2
S=max score
individual points p1,p2,...,pn
how many permutations of p1,...,pn (multiple occurences of px allowed) give
us total score S.
This is essentially based on American football, where each drive can end in
2 points (safety), 3 points (field goal), 6 points (td) or 7 points (td+fg).
Example:
S=47
p1=2,p2=4,p3=7
Answer: 226234 permutations
3,3,3,3,7,7,7,7,7,:(126 permutations)
3,3,3,3,3,3,3,3,3,3,3,7,7,:(78 permutations)
2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,:(16 permutations)
2,2,2,2,3,3,3,3,3,3... 阅读全帖
L**********u
发帖数: 194
3
来自主题: JobHunting版 - Exposed上一道string permutation的题
I solved this problem several days ago. hehe
My algorithm is following:
//print out all permutations of a given string.
//A string with n characters has n! permutations
//The permutations can be obtained by the following method.
//n=1, only one permutation 1
//n=2, permutations are obtained by inserting 2 to every possible positions
of each string obtained when n=1
// there are two possible positions and one string, therefore
permutations are
// 21 12
//n=3, permutations are o... 阅读全帖
g**********y
发帖数: 14569
4
先把字符排序,然后对每个不同的字符(个数是len)来递归。相当于把len个字符插进前面的生成串里,总共有C(n+len, len)种方式。
这是java code
好象这也是G喜欢问的一道题,面试时写出来还是很吃力的。程序不难,但是那些递归关系和边界条件想清楚,很费脑力。我觉得谁要不用debugger, 一遍把它写对,差不多就可以把G拿下了。
public class RepetitionPermute {
public void permute(String a) {
char[] c = a.toCharArray();
Arrays.sort(c);
permute("", c, 0);
}

private void permute(String s, char[] c, int begin) {
if (begin == c.length) {
System.out.println(s);
return;
}
... 阅读全帖
g**********y
发帖数: 14569
5
先把字符排序,然后对每个不同的字符(个数是len)来递归。相当于把len个字符插进前面的生成串里,总共有C(n+len, len)种方式。
这是java code
好象这也是G喜欢问的一道题,面试时写出来还是很吃力的。程序不难,但是那些递归关系和边界条件想清楚,很费脑力。我觉得谁要不用debugger, 一遍把它写对,差不多就可以把G拿下了。
public class RepetitionPermute {
public void permute(String a) {
char[] c = a.toCharArray();
Arrays.sort(c);
permute("", c, 0);
}

private void permute(String s, char[] c, int begin) {
if (begin == c.length) {
System.out.println(s);
return;
}
... 阅读全帖
a***e
发帖数: 413
6
【 以下文字转载自 JobHunting 讨论区 】
发信人: abcee (abcee), 信区: JobHunting
标 题: Re: 请问大牛们leetcode上的Permutations II
发信站: BBS 未名空间站 (Fri Jun 13 18:00:00 2014, 美东)
再问一下 , 对于无重复的permutation, 这个答案好理解。但怎么改来处理有重复数
的问题呢?
Given a collection of numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
vector > permute(vector& num) {
sort(num.begin(), num.end());
vector> result;
vector pat... 阅读全帖
j******s
发帖数: 48
7
来自主题: JobHunting版 - 请教leetcode Permutations II 解法和code
public ArrayList> permuteUnique(int[] num) {
Arrays.sort(num);
ArrayList> output
= new ArrayList>();
ArrayList list = new ArrayList();
boolean col[] = new boolean[num.length];
permute(output,list,col,num,0);
return output;
}

public void permute(ArrayList> output,
ArrayList list,boolean col[]... 阅读全帖
r**u
发帖数: 1567
8
来自主题: JobHunting版 - Non-recursive permutation
解释一下next_permute,其实就是按字符顺序输出,比如12345
先在的permutation是12345,下一个就是12354。
算法思路,怎么找下一个呢:
比如先在:13542,下一个是14235,
首先要从后往前找第一个pair,满足A[i] 被变大的。上面这例子里这pair是35, 542已经是最大的permutation了。
所以要找到3。如果找不到这中pair,那么已经是最大的permutation了,比如54321。
然后从后往前找一个比3大的数,swap(it, 3)。这例子里是swap(4, 3)。
这样的话permutation变大,但是不会变得太大, swap(5, 3)就不对了。
先在变成14532了,然后reserve 532,得到14235。
f****4
发帖数: 1359
9
重复字符permutation怎么算啊?比如
“aa”,permutation是2个还是1个?
我的理解是如果要输出所有permutation的结果,那么next_permutation对输入字符集是默认排序的(就是说,对任意字符集输入,得先排序,才能使用类next_permutation算法);重复字符permutation不算重复的
g++,stl
input:
1, 2, 2, 3
output:
1 2 2 3
1 2 3 2
1 3 2 2
2 1 2 3
2 1 3 2
2 2 1 3
2 2 3 1
2 3 1 2
2 3 2 1
3 1 2 2
3 2 1 2
3 2 2 1
input:
1, 4, 3, 2
output:
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1
第二个明显就是错的。。。
f****4
发帖数: 1359
10
重复字符permutation怎么算啊?比如
“aa”,permutation是2个还是1个?
我的理解是如果要输出所有permutation的结果,那么next_permutation对输入字符集是默认排序的(就是说,对任意字符集输入,得先排序,才能使用类next_permutation算法);重复字符permutation不算重复的
g++,stl
input:
1, 2, 2, 3
output:
1 2 2 3
1 2 3 2
1 3 2 2
2 1 2 3
2 1 3 2
2 2 1 3
2 2 3 1
2 3 1 2
2 3 2 1
3 1 2 2
3 2 1 2
3 2 2 1
input:
1, 4, 3, 2
output:
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1
第二个明显就是错的。。。
c**********e
发帖数: 2007
11
If you use 2,3,7, your answer is not complete.
You can also have 2,3,7,7,7,7,7,7.

3,3,3,3,7,7,7,7,7,:(126 permutations)
3,3,3,3,3,3,3,3,3,3,3,7,7,:(78 permutations)
2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,:(16 permutations)
2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,:(2380 permutations)
c**********e
发帖数: 2007
12
If you use 2,3,7, your answer is not complete.
You can also have 2,3,7,7,7,7,7,7.

3,3,3,3,7,7,7,7,7,:(126 permutations)
3,3,3,3,3,3,3,3,3,3,3,7,7,:(78 permutations)
2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,:(16 permutations)
2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,:(2380 permutations)
f*********m
发帖数: 726
13
谢谢。
Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note: Given n will be between 1 and 9 inclusive.
c********t
发帖数: 5706
14
能不能和下面解法对比一下优缺点?
public static void permutation(String str) {
permutation("", str);
}
private static void permutation(String prefix, String str) {
int n = str.length();
if (n == 0) System.out.println(prefix);
else {
for (int i = 0; i < n; i++)
permutation(prefix + str.charAt(i), str.substring(0, i) + str.
substring(i+1, n));
}
}
c********t
发帖数: 5706
15
能不能和下面解法对比一下优缺点?
public static void permutation(String str) {
permutation("", str);
}
private static void permutation(String prefix, String str) {
int n = str.length();
if (n == 0) System.out.println(prefix);
else {
for (int i = 0; i < n; i++)
permutation(prefix + str.charAt(i), str.substring(0, i) + str.
substring(i+1, n));
}
}
a***e
发帖数: 413
16
来自主题: JobHunting版 - 请问大牛们leetcode上的Permutations II
再问一下 , 对于无重复的permutation, 这个答案好理解。但怎么改来处理有重复数
的问题呢?
Given a collection of numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
vector > permute(vector& num) {
sort(num.begin(), num.end());
vector> result;
vector path;
dfs(num, path, result);
return result;
}
private:
void dfs(const vector& num, vector &path,vector > &
result) {
if (path.siz... 阅读全帖
d****n
发帖数: 233
17
来自主题: JobHunting版 - Non-recursive permutation
First, not sure if this is the fatest one. I did it for fun a while ago.
Hope no interviewer will ask such kind of question.
Hints:
Think about # of permutations for n size array is 1*2*3*...*(n-1)*n.
if you have permutations for 1,2,3,...(n-1). what you need to do is to
switch one of the element with the nth and repeat the permutation for length
(n-1). all the elements after (n-1)th will be untouched.
j*******r
发帖数: 52
18
试贴一个C++代码,循环+递归,用一个bitset代表当前字符是否已经在之前位置被使用。
1 #include
2 #include
3 #include
4
5 using namespace std;
6
7 void permutation(const char* str, bitset<4> used, string r){
8 if(r.size() == strlen(str)){
9 cout< 10 }
11 for(int i = 0; i < strlen(str); ++i){
12 if(used[i])
13 continue;
14 used.set(i);
15 r += str[i];
16 permutation(str, used, r);
17... 阅读全帖
g**********y
发帖数: 14569
19
aa的permutation当然只有一个。

集是默认排序的(就是说,对任意字符集输入,得先排序,才能使用类next_
permutation算法);重复字符permutation不算重复的
j*******r
发帖数: 52
20
试贴一个C++代码,循环+递归,用一个bitset代表当前字符是否已经在之前位置被使用。
1 #include
2 #include
3 #include
4
5 using namespace std;
6
7 void permutation(const char* str, bitset<4> used, string r){
8 if(r.size() == strlen(str)){
9 cout< 10 }
11 for(int i = 0; i < strlen(str); ++i){
12 if(used[i])
13 continue;
14 used.set(i);
15 r += str[i];
16 permutation(str, used, r);
17... 阅读全帖
g**********y
发帖数: 14569
21
aa的permutation当然只有一个。

集是默认排序的(就是说,对任意字符集输入,得先排序,才能使用类next_
permutation算法);重复字符permutation不算重复的
f*******t
发帖数: 7549
22
没那么复杂,只要交换就好了
#include
#include
#include
void swap(char *arr, int idx1, int idx2) {
char temp = arr[idx1];
arr[idx1] = arr[idx2];
arr[idx2] = temp;
}
void permutation(char* arr, int index, int size) {
if (index == size) {
printf("%s\n", arr);
}
else {
for (int i=index; i swap(arr, i, index);
permutation(arr, index+1, size);
swap(arr, i, index);
}
}
}
int main()
{
... 阅读全帖
f*******t
发帖数: 7549
23
来自主题: JobHunting版 - Exposed上一道string permutation的题
#include
#include
#include
void swap(char *arr, int idx1, int idx2) {
char temp = arr[idx1];
arr[idx1] = arr[idx2];
arr[idx2] = temp;
}
void permutation(char* arr, int index, int size) {
if (index == size) {
printf("%s\n", arr);
}
else {
for (int i=index; i if(i != index && arr[i] == arr[index])
continue;
swap(arr, i, index);
permutation(arr, index+1, size);
... 阅读全帖
f*******t
发帖数: 7549
24
来自主题: JobHunting版 - Exposed上一道string permutation的题
我改进了一下,这回貌似行了,不过输出没能按照字典序:
#include
#include
#include
using namespace std;
void swap(string& s, int idx1, int idx2) {
if(idx1 == idx2)
return;
char temp = s[idx1];
s[idx1] = s[idx2];
s[idx2] = temp;
}
void permutation(string& s, int index, int size) {
if (index == size)
cout << s << endl;
else {
char last = 0;
for (int i=index; i if(s[i] == last)
continue;
... 阅读全帖
m****i
发帖数: 650
25
It is still a solution without sorting or hashing.
public void permutationRecursive(int[] arr, int index) {
if (arr.length == index) {
for (int i = 0; i < arr.length; ++i) {
System.out.printf("%d", arr[i]);
}
System.out.println();
return;
}
int lastSwap = 0;
for (int i = index; i < arr.length; ++i) {
if (arr[i] == arr[index] && i != index) continue;
if (arr[i] == lastSwap) c... 阅读全帖
h*****3
发帖数: 1391
26
来自主题: JobHunting版 - 请教leetcode Permutations II 解法和code
好像carreer cup150上有这题?我写的最后一个test case没通过,我觉的好像他的
test case有问题,但我的思路应该没问题。
//这个子程序可以忽略不计
void swap (int &i,int &j){
int tmp;
tmp = i;
i = j;
j = tmp;
}
void permitUniq(vector &num,int res[],int lev,vector > &
list,int start)
{//res- buffer一个solution, lev - res 中的index, list- keep all
solutions,start- num中的起始位置
int prev;
vector tmp;
if(lev == num.size()){//找到一个solution, 存起来啊
tmp.assign(res,res+lev);
list.push... 阅读全帖
h*****3
发帖数: 1391
27
来自主题: JobHunting版 - 请教leetcode Permutations II 解法和code
好像carreer cup150上有这题?我写的最后一个test case没通过,我觉的好像他的
test case有问题,但我的思路应该没问题。
//这个子程序可以忽略不计
void swap (int &i,int &j){
int tmp;
tmp = i;
i = j;
j = tmp;
}
void permitUniq(vector &num,int res[],int lev,vector > &
list,int start)
{//res- buffer一个solution, lev - res 中的index, list- keep all
solutions,start- num中的起始位置
int prev;
vector tmp;
if(lev == num.size()){//找到一个solution, 存起来啊
tmp.assign(res,res+lev);
list.push... 阅读全帖
e****e
发帖数: 418
28
多谢lolhaha大牛指点,non-duplicate修订版如下。
client invoke: permute( new char[]{'a', 'b', 'c'}, 0 );
void permute(char[] perm, int level){
if ( level == perm.length ) {
System.out.println( new String( perm ) );
for ( int i = level, i < perm.length; i++ ) {
if ( level != i )
swap( perm, level, i );
permute( perm, level + 1 );
if ( level != i )
swap( perm, level, i );
}
}
void swap(char[] arr, int i, int j) {...}
e****e
发帖数: 418
29
多谢lolhaha大牛指点,non-duplicate修订版如下。
client invoke: permute( new char[]{'a', 'b', 'c'}, 0 );
void permute(char[] perm, int level){
if ( level == perm.length )
System.out.println( new String( perm ) );
for ( int i = level; i < perm.length; i++ ) {
if ( level != i )
swap( perm, level, i );
permute( perm, level + 1 );
if ( level != i )
swap( perm, level, i );
}
}
void swap(char[] arr, int i, int j) {...}
n******r
发帖数: 869
30
实在太落后了。看了解法还是不会写。
Design an algorithm to print all permutations of a string. For simplicity,
assume all characters are unique.
Test String: abcdefg
Case “a” --> {a}
Case “ab” --> {ab, ba}
Case “abc” --> ?
This is the first “interesting” case. If we had the answer to P(“ab”),
how could we generate P(“abc”). Well, the additional letter is “c”, so
we can just stick c in at every possible point. That is:
merge(c, ab) --> cab, acb, abc
merge(c, ba) --> cba, bca, bac
Algorithm: Use a recursive algorit... 阅读全帖
a**********0
发帖数: 422
31
来自主题: JobHunting版 - 如何避免permutation中的重复计数
忘了哪个网友发过一个帖子 如果备选数字中有重复则不容易避免重复计数(在不使用
hashset一类的额外数据结构的时候)
我觉得可以这样 有个算法 是求下一个permutation 然后你从第一个permutation开始
生成 不断求下一个permutation 则此序列不会有重复
但是这个不是针对combination的
l****e
发帖数: 9
32
【 以下文字转载自 Mathematics 讨论区,原文如下 】
发信人: lustre (紫色的花,young troubled soul), 信区: Mathematics
标 题: any procedures to compute and show the permutations?
发信站: The unknown SPACE (Tue Jun 11 16:18:35 2002) WWW-POST
Like pick n1 items from n items.
Is there any procedure to find out all the permutations and show them?
Since I need the sum for each permutation.
Thanks!
o******6
发帖数: 538
33
☆─────────────────────────────────────☆
davfox121 (davfox) 于 (Wed Mar 4 20:55:52 2009) 提到:
For permutation like AABBCC, or AAABBCCC, which have same characters, how
could I write SAS to get complete permutation without repeat?
It seems for Proc Plan, it only work for series like ABCDEF without same
characters.
☆─────────────────────────────────────☆
oloolo (似人非兽) 于 (Wed Mar 4 21:07:24 2009) 提到:
把第一个A和第二个A调换一下位置,新的字符串跟老的有啥区别?算一个permutation
么?

☆─────────────────────────────────────☆
d****n
发帖数: 233
34
来自主题: JobHunting版 - Non-recursive permutation
// test1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
void PrintArray(int *A, unsigned int N)
{
for (unsigned int i = 0; i < N; i++)
{
printf("%d ", A[i]);
}
printf("\r\n");
}
// This method print out an array iteratively.
// The idea behind this is:
// 1. Suppose we have a way to print out permutation for A[n-1].
// 2. For every permutation, insert the nth element between any of
// the n-1 elements will construct a new permuation
// F
x*****m
发帖数: 29
35
来自主题: JobHunting版 - 一个容易记忆的permutation算法
看过很多permutation算法 都是看了就忘忘了再看看了还是忘
即使是Dijkstra爷爷的那个很简洁的...毕竟不是自己想的...觉得还是不容易记..
于是刚才就想写一个容易理解容易记忆的,跟大家分享一下,轻拍...是recursive的 所
以空间代价不是那么理想 但是容易
记...
受那道经典的洗牌算法的启发, 基本就是n个位置,从s[0]到s[n-1]
先是排定第一个位置,依次拿s[0]~s[n-1]跟s[0]交换
每交换一次,对剩下的字符串再进行递归的操作
直到n个位置排定了,输出排列结果
(语言能力日益下降,不知道讲清楚没)
current是现在正在排定的位置, len是要排的字符的长度,从s的尾部开始数len个
swap是随便一个实现交换char的函数
code如下:
void Permutate(std::string s, int len) {
size_t i;

if (len == 1) {
std::cout << s << std::endl;
return;
}

int c
v********w
发帖数: 136
36
来自主题: JobHunting版 - 一个容易记忆的permutation算法
这样行不
void Permutate(string &s, int len) {
size_t i;
hashmap hm;

for (i=0;i<26;i++) hash[i]=0;
if (len == 1) {
cout << s << endl;

return;
}

int current = s.size() - len;

for (i=s.size()-len; i if(!hm.contains(s[i]))
{ hm.add(s[i]);
Swap(s[current], s[i]);
Permutate(s, len - 1);
Swap(s[current], s[i]);
}
}

}
Z*****Z
发帖数: 723
37
来自主题: JobHunting版 - Permutation一问
给一个数组和一个random number generator,写一个函数,返回这个数组的一个permut
ation, 要求任何可能的permutation都被等概率返回。
怎么做?
Z*****Z
发帖数: 723
38
来自主题: JobHunting版 - Permutation一问
然后呢?
我的想法是,比如数组大小为N,random number i的范围应该是1到N!,然后返回第i个
permutation
只是这第i个permutation不太好直接做吧?
k****n
发帖数: 369
39
permutation generation有很多种方法,字典序最直观,邻位对换的有几种
真要搞明白,自己拿纸笔画一画推一推才是真的,别人给代码不如这个
p*****u
发帖数: 310
40
不是生成所有permutation。 我自己写了一下,觉得不是很漂亮。
g**********y
发帖数: 14569
41
public class PermuteK {
private int[] P;

public String permute(String input, int k) {
calculatePermuteNumber(input.length());
return find(input, k);
}
private void calculatePermuteNumber(int N) {
P = new int[N];
P[0] = 1;
for (int i=1; i P[i] = P[i-1]*i;
}
}

private String find(String input, int k) {
int N = input.length();
if (N == 1) return input;
int i = k/P[N-1];
... 阅读全帖
s*******y
发帖数: 105
42
来自主题: JobHunting版 - Exposed上一道string permutation的题
求一个string的所有的permutation. C code如下:
int permutations(char *str)
{
int length, i, *used;
char *out;
length = strlen(str);
out = (char *)malloc(length+1);
if(!out)
return 0;
out[length] = '\0';
used = (int*)malloc(sizeof(int)*length);
if(!used)
return 0;
for(i=0;i used[i]=0;
DoPermute(str, out, used, length, 0);
free(out);
free(used);
return 1;
}
void DoPermute(char *str, char* out, int* used, int length, int recursLev)
{
int i;
if(recursLev==l... 阅读全帖
s*******y
发帖数: 105
43
来自主题: JobHunting版 - Exposed上一道string permutation的题
不是很理解这里的recursion.
举例来说:
With input "hat",
1. value of i =0, out[0] = 'h', used[0]=1, recursive call (I assume i=0,
recursLev =0, is pushed on to the stack)
2. i =0 continue, i=1 out[1]='a', used[1] = 1, recursive call (i =1,
recurseLev =1 on stack)
3. i=0,1 continue, i=1 out[2] = 't', used[2] = 1, recursive call(i=2,
recurseLev=2 on stack)
4. Line 31 is executed, "hat" is printed out and it returns.
It is from here that I am confused. This is what I think happens:
5. Return to line 45, pop i fr... 阅读全帖
s*****n
发帖数: 162
44
来自主题: JobHunting版 - Exposed上一道string permutation的题
Following is a solution based on the very interesting method called Knuth’s
Permutations. This has the duplicate permutation avoidance logic built-in.
The first call to the method is StringPermutation(str, 0).
void StringPermutation(char* str, int k) {
if (!str || (k < 0))
return;
if (str[k] == '\0') {
cout << str << endl;
return;
}
StringPermutation(str, k + 1);
int i;
for (i = k;(i > 0) && (str[i] != str[i - 1]);i--) {
char t = str[i];
... 阅读全帖
f*********m
发帖数: 726
45
来自主题: JobHunting版 - 请教leetcode Permutations II 解法和code
题目:
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].
请教不用set的方法。
谢谢。
f*********m
发帖数: 726
46
来自主题: JobHunting版 - 请教leetcode Permutations II 解法和code
题目:
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].
请教不用set的方法。
谢谢。
z*******8
发帖数: 30
47
来自主题: JobHunting版 - 请教leetcode Permutations II 解法和code
http://blog.csdn.net/zxzxy1988/article/details/8579357
基本思路就是用"next permutation"的概念,也就是下一个字典序的permutation,无
论有没有dup,都可以解决
I*****8
发帖数: 37
48
这道题和next permutation 差不多把,我觉得的话,主要的思想就是swap,数学上来
说就是证明这些充要条件:
http://www.tsechung.com/wordpress/2012/07/16/permutation-sequen
g**********t
发帖数: 475
49
Isn't it very slow to swap one array k times? Remember that k could be very
large! I attached my code using a different idea.Its complexity is O(n^2).
#include
#include
#include
#include
using namespace std;
int permutation(int n){
int p = 1;
for(int i = 1; i <= n; i ++){
p *= i;
}
return p;
}
string perSeq(int n, int k){
string output;
bool flag[10];
fill_n(flag, 10, false);
--k;
for (int i = 1; ... 阅读全帖
g**u
发帖数: 583
50
请问大家, 如何有效的实现permute vector of vectors.
例如:
输入为{ {2,1}, {4,3}}
期待的输出为:
{2,1,4,3}
{2,1,3,4}
{1,2,3,4}
{1,2,4,3}
希望使用 next_permutation来实现,也尝试将数值map到index,然后permute index,
在输出的时候再返回;但是当有3个或者以上的vectors的时候发现卡住了
请各位大牛指点,谢谢
1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)