js代码:输入一个数组,和一个位数,生成他的组合数,不算重复值,顺序颠倒只算一个
* useage: combine([1,2,3,4,5,6,7,8,9], 3);
*/
function combine(arr, num) {
var r = [];
(function f(t, a, n) {
if (n == 0) return r.push(t);
for (var i = 0, l = a.length; i <= l - n; i++) {
f(t.concat(a[i]), a.slice(i + 1), n - 1);
}
})([], arr, num);
return r;
}
一个js方法转换为java代码
答案:1 悬赏:80 手机版
解决时间 2021-02-24 03:01
- 提问者网友:别再叽里呱啦
- 2021-02-23 09:54
最佳答案
- 五星知识达人网友:詩光轨車
- 2021-02-23 10:20
package p1;
import java.util.Arrays;
import java.util.LinkedList;
public class Guy
{
public static void recursionSub ( LinkedList<int[]> list, int count, int[] array, int ind, int start, int... indexs )
{
start++;
if (start > count - 1)
{
return;
}
if (start == 0)
{
indexs = new int[array.length];
}
for ( indexs[start] = ind; indexs[start] < array.length; indexs[start]++ )
{
recursionSub (list, count, array, indexs[start] + 1, start, indexs);
if (start == count - 1)
{
int[] temp = new int[count];
for ( int i = count - 1; i >= 0; i-- )
{
temp[start - i] = array[indexs[start - i]];
}
list.add (temp);
}
}
}
public static void main ( String[] args )
{
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
LinkedList<int[]> list = new LinkedList<int[]> ();
recursionSub (list, 3, array, 0, -1);
for ( int[] strings : list )
{
System.out.println (Arrays.toString (strings));
}
}
}
import java.util.Arrays;
import java.util.LinkedList;
public class Guy
{
public static void recursionSub ( LinkedList<int[]> list, int count, int[] array, int ind, int start, int... indexs )
{
start++;
if (start > count - 1)
{
return;
}
if (start == 0)
{
indexs = new int[array.length];
}
for ( indexs[start] = ind; indexs[start] < array.length; indexs[start]++ )
{
recursionSub (list, count, array, indexs[start] + 1, start, indexs);
if (start == count - 1)
{
int[] temp = new int[count];
for ( int i = count - 1; i >= 0; i-- )
{
temp[start - i] = array[indexs[start - i]];
}
list.add (temp);
}
}
}
public static void main ( String[] args )
{
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
LinkedList<int[]> list = new LinkedList<int[]> ();
recursionSub (list, 3, array, 0, -1);
for ( int[] strings : list )
{
System.out.println (Arrays.toString (strings));
}
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯