一个js方法转换为java代码
js代码:输入一个数组,和一个位数,生成他的组合数,不算重复值,顺序颠倒只算一个*useage:combine([1,2,3,4,5,6,7,8,9],3);*/func...
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;
} 展开
* 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;
} 展开
1个回答
展开全部
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));
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询