如何解决递归函数中的varialble参数总是在递归堆栈结束后输出相同的值
static void getAllpossible(List<Integer> list,List<List<Integer>> finalseq,int index,int currentsub ) {
if (index==list.size()) {
System.out.println();
System.out.println("final"+finalseq);
return;
}
currentsub=finalseq.size()-1;
System.out.println("sub"+currentsub);
int curr=list.get(index);
finalseq.get(currentsub).add(curr);
System.out.println("sub"+finalseq);
getAllpossible(list,finalseq,index+1,currentsub);
System.out.println("after "+finalseq);
}
输出为
之后
final [[1,2,3,4]]
在[[1、2、3、4]]之后
在[[1、2、3、4]]之后
在[[1、2、3、4]]之后
但应该是
final [[1,4]]
在[[1、2、3、4]]之后
在[[1、2、3]]之后
在[[1,2]]之后
在[[1]]
首次调用该函数时,finalseq为[[1]]; 我已经尝试了2天,请帮助
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。