又一个卧底暴露了# Joke - 肚皮舞运动
x*5
1 楼
请教各位大牛一个问题:
Problem: Output all subsets of size of m from an array
Solution Hints: String permutation Problem variant
Python Codes:
def Subset_m(v,l,m,output):
"Find all subsets of an array v of size m, need fixing"
if l==m:
print output
return
for i in range(l,len(v)):
output[l]=v[i]
Subset_m(v,l+1,m,output)
C++ Codes:
/*Description: output all the subset of size m
Input:vector v, int m,int l, vector& output
Output: void
*/
void Pearl::SubArrayS_size_m(vector v, int m,int l, vector& output
){
if(l==m){
for(int i=0;i cout<cout<
return;
}
else{
for(int i=l;i output[l]=v[i];
SubArrayS_size_m(v,m,l+1,output);
}
}
}
Test Case:
v=[1,2,3]
output=[0 for i in range(2)]
Pearl.Subset_m(v, 0, 2, output)
Output:
[1, 2]
[1, 3]
[2, 2]
[2, 3]
[3, 2]
[3, 3]
问题:总是输出[2,2] ,[3,3]这种不对的答案,目前不知道如何修改是好,请教各位大
牛
Problem: Output all subsets of size of m from an array
Solution Hints: String permutation Problem variant
Python Codes:
def Subset_m(v,l,m,output):
"Find all subsets of an array v of size m, need fixing"
if l==m:
print output
return
for i in range(l,len(v)):
output[l]=v[i]
Subset_m(v,l+1,m,output)
C++ Codes:
/*Description: output all the subset of size m
Input:vector
Output: void
*/
void Pearl::SubArrayS_size_m(vector
){
if(l==m){
for(int i=0;i
return;
}
else{
for(int i=l;i
SubArrayS_size_m(v,m,l+1,output);
}
}
}
Test Case:
v=[1,2,3]
output=[0 for i in range(2)]
Pearl.Subset_m(v, 0, 2, output)
Output:
[1, 2]
[1, 3]
[2, 2]
[2, 3]
[3, 2]
[3, 3]
问题:总是输出[2,2] ,[3,3]这种不对的答案,目前不知道如何修改是好,请教各位大
牛