Tag: 递归

这是否会导致堆栈溢出错误?

递增对象的实例变量会导致堆栈溢出错误吗? 例如: 此方法(java)将导致堆栈溢出错误: class StackOverflow { public static void StackOverflow (int x) { System.out.println (x) ; StackOverflow(x+1) ; } public static void main (String[]arg) { StackOverflow (0) ; } 但这会是吗?:( …..是我为缩短代码而设置的差距。它的长度足够长。) import java.util.*; class Dice { String name ; int x ; int[] sum ; …. public Dice (String name) { this.name = name ; […]

在字符串中生成字符组合并不完全有效,为什么?

我正在尝试生成字符串中所有字符的组合。 所以第一个参数是给定的字符串,第二个参数是字母数。 所以combinations(“ab”,2)应该给我aa, ab, ba, bb和combinations(“abc”,2)应该给我aa, ab, ac, ba, bb, bc, ca, cb, cc等。 在第一种情况下,我当前的代码给了我aa, ab, bb (所以它跳过ba )。 这是我的代码: public static void combinations(String s, int n) { combinations(s,””,n); } public static void combinations(String s, String prfx, int n) { if(n == 0) { System.out.println(prfx); } else { for(int i = 0; i < s.length(); […]

简单实现迷宫生成方法(随机DFS)

在一次采访中,我的采访者问我这个问题: 开发一个生成随机迷宫的function 如果你以前没有解决过这个问题,这是一个很难在30分钟内解决的问题。 在互联网上有很多解决方案,但似乎没有一个容易。 该方法应该遵循这个约束: 它应该接受迷宫的大小(方形迷宫NxN) 它应该只包含Walls和Path 为简单起见,该算法不需要设置入口和出口 我将用解决方案发布答案​​,以便其他人能够找到这个问题。 生成的迷宫示例: . # # . # . . . . . . . . . . . # # # . # . # # # # . . . . . # . . . . # . # . . # . # # […]

组合和置换算法(递归)

我正在从事Java任务,我绝对难过。 问题是: 使用Recursion编写一个函数来执行以下操作:您有X个不同的卡。 你只有Y信封。 Y小于或等于X.对于任何给定的X和Y值, 显示所有可能的方式,您可以在订单不重要时填写Y信封并且不允许重复。 hint: X! / (( XY)! * Y!) 显示所有可能的方法,您可以在订单重要时填写Y信封,并允许重复hint: X^Y 显示订单重要时可以填写Y信封的所有可能方式,并且不允许重复提示: X! / (X – Y)! X! / (X – Y)! 当订单不重要时,显示所有可能的填充Y信封的方法,并允许重复提示: (X + Y – 1)! / (Y! * (X – 1)!) (X + Y – 1)! / (Y! * (X – 1)!) 例如,在情况(1)下, if X = {J, Q, […]

如何水平打印?

嘿大家,如果你用给定的输入运行这个代码你会得到一个垂直标尺我试图得到一个水平标尺使用给定的递归函数任何想法如何到达那里或提示??? public class Ruler { // draw a tick with no label public static void drawOneTick(int tickLength) { drawOneTick(tickLength, -1); } // draw one tick public static void drawOneTick(int tickLength, int tickLabel) { for (int i = 0; i = 0) System.out.print(” ” + tickLabel); System.out.print(“\n”); } public static void drawTicks(int tickLength) { if (tickLength […]

显示Splay树的方法

我已经构建了一个展开树,我正在尝试按顺序将其打印出来,以便当您将头转向左侧时,您可以正常方式看到树。 我编写了以下代码,它输出树有点正确,但它在最右边的节点上添加了额外的空格,并没有为应该放在根节点下面的所有子节点添加空格: public void printReverseInOrder() { if (root != null) { reverseInOrder(root, 0); } else { System.out.println(); } } public void reverseInOrder(BSTnode h, int indent) { if (h != null) { for (int i = 0; i < indent; i++) { System.out.print(" "); } indent++; reverseInOrder(h.right, indent); reverseInOrder(h.left, indent); System.out.println(h.data); indent–; } } 我觉得这可能是我的递归或我的缩进添加和减少的位置的错误。

使用Java和递归的二级树遍历级别顺序遍历

我在使用递归时遇到二叉树的级别顺序遍历问题。 我输入以下值:50,60,70,30,20,10这是我正在使用的代码: public void levelOrder(Node localRoot){ if(localRoot != null){ if(localRoot.leftChild != null && localRoot.rightChild != null){ System.out.print(localRoot.iData + ” “); System.out.print(localRoot.leftChild.iData + ” “); System.out.print(localRoot.rightChild.iData + ” “); levelOrder(localRoot.leftChild); levelOrder(localRoot.rightChild); } else if(localRoot.rightChild == null && localRoot.leftChild == null){ ; } else if(localRoot.rightChild == null){ System.out.print(localRoot.leftChild.iData + ” “); //levelOrder(localRoot.leftChild); } else{ System.out.print(localRoot.rightChild.iData + ” […]

递归调用Main

public class Demo { static int i=0; public static void main(String args[]) { System.out.println(“Hello”+(i++)); main(args); } } 在这个程序中,我用实例变量调用main。 它可以正常运行到某些点但是在一些Hello打印之后它会产生StackOverFlow Exception。 所以我把int放到它打印的次数。 我运行这个程序,它在i=4158之后给出了Exception。 但我运行了几次,它给出了不同值的exception,如4155,4124,4154等。 正如我所知, StackOverFlow是由于错误或无条件的递归调用而生成的。 我试图找出它,但不知道到底发生了什么。 我想知道为什么在4158之后(或其他值)? 它是依赖于我的系统还是依赖于我的程序?

找到所有哈密顿循环

我正在尝试实现一种方法,使用递归将所有可能的哈密顿循环添加到列表中。 到目前为止,我的停止条件还不够,我在向列表中添加顶点的行中得到“OutOfMemoryError:Java堆空间”: private boolean getHamiltonianCycles(int first, int v, int[] parent, boolean[] isVisited, List<List> cycles) { isVisited[v] = true; if (allVisited(isVisited) && neighbors.get(v).contains(new Integer(first))) { ArrayList cycle = new ArrayList(); int vertex = v; while (vertex != -1) { cycle.add(vertex); vertex = parent[vertex]; } cycles.add(cycle); return true; } else if (allVisited(isVisited)) { isVisited[v] = false; return […]

等待递归的Thread-Producer

我有一个收集者,在游戏中搜索动作。 我在一个递归搜索中搜索,以从游戏中获得所有可能的移动。 出于性能原因,我使用了一个Threadpool,并且每个找到的移动都会向池中添加一个新的Thread,以延长旧的移动。 这是一些代码: protected static List threads; private static ExecutorService threadPool; protected final synchronized void hookThread(Runnable thread) { if (threadPool == null) { threadPool = Executors.newFixedThreadPool(15); threads = new ArrayList(); } threadPool.execute(thread); threads.add(thread); } protected abstract class GathererRunnable implements Runnable { @Override public final void run() { onRun(); threads.remove(this); } public abstract void onRun(); […]