从方法中删除尾递归(Java)

时间:2016-11-23 01:44:03

标签: java optimization tail-recursion

我有这个方法:

<?php

// Create connection
$con=mysqli_connect("localhost","gaethrco_travis","Energy=mc^2","gaethrco_data");

// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT * FROM Locations";

// Check if there are results
if ($result = mysqli_query($con, $sql))
{
    // If so, then create a results array and a temporary one
    // to hold the data
    $resultArray = array();
    $tempArray = array();

    // Loop through each row in the result set
    while($row = $result->fetch_object())
    {
        // Add each row into our results array
        $tempArray = $row;
        array_push($resultArray, $tempArray);
    }

    // Finally, encode the array to JSON and output the results
    echo json_encode($resultArray);
}

// Close connections
mysqli_close($con);
?>

它计算由单个广度优先搜索遍历引起的排列。我用它来解决Post's correspondence problem。但是,我怀疑它是尾递归的,并且似乎在某些问题实例上会产生丑陋的开销。

如何在保留方法行为的同时删除尾递归?

1 个答案:

答案 0 :(得分:-1)

这就是你所追求的(大小 n 字母表的排列):

private static String computePerm(int iteration) {
    return Integer.toString(iteration, n);
}