【发布时间】:2015-03-28 05:39:57
【问题描述】:
recursiveSum(Node currentNode) {
if (currentNode == null){
System.out.println("done " );
}else{ recursiveSum(currentNode.next);
}
}
这里是节点类和递归方法。我已经尝试了所有我能想到的方法来返回所有可能的子集......如果我将数字 {`1,2,3} 添加到列表中,递归方法应该打印:{1,2,3} {1 ,3} {1,2} {1} {1,3} {2} {3} {0}
private static class Node {
Node next;
int number;
public Node(int numberValue) {
next = null;
number = numberValue;
}
public int getNumber() {
return number;
}
public void setData(int numberValue) {
number = numberValue;
}
public Node getNext() {
return next;
}
public void setNext(Node nextValue) {
next = nextValue;
}
}
【问题讨论】:
-
显示一小段代码,让您清楚自己在做什么。计算机科学一般不讲授 Stack Overflow,但会解决具体的技术问题或调试问题。
-
public static void recursiveSum(Node currentNode) { if (currentNode == null){ System.out.println("done" ); }else{ recursiveSum(currentNode.next); } }
-
私有静态类 Node { Node next;整数;公共节点(int numberValue){下一个=空;数字=数字值; } public int getNumber() { 返回号码; } public void setData(int numberValue) { number = numberValue; } 公共节点 getNext() { 返回下一个; } public void setNext(Node nextValue) { next = nextValue; } }