java - 带有辅助方法的 Java 排列递归
问题描述
我在使用该permutationHelper
方法的作业作业中遇到错误。
名为zybooks的自动评分系统并没有告诉我我做错了什么。
但是,它给了我一个错误,上面写着:
Test feedback : permutation("123") incorrectly returned
我的输出显示:
Your output : 123 132 213 231 312 321
其中,imo 看起来完全像它应该的那样。
(教师在注释代码中留下示例)。
我非常想了解为什么会出现此错误,即使代码似乎运行正常。或者如果有更好的方法来完成任务。
笔记
我不允许更改方法的参数或标题。
下面是我的代码。
/*
* The following method is given to you, and you will be responsible for completing the permutationHelper method it calls.
* Sometimes, helper methods are used for recursive methods when another parameter is needed to recursively call a method repeatedly, but passing that parameter initially doesn't make sense.
*/
public static String permutation(String word){
return permutationHelper(" ", word);
}
/* permutationHelper()
* This method is called by the permutation method.
* Given a string, return a string that lists all possible permutations of the letters in the string, with spaces preceding each permutation.
* For example, "123" would give "123 132 213 231 312 321".
* The perm parameter keeps track of the current permutation you are creating.
* Consider using the a for loop to call the method recursively a certain number of times with different parameters, so you cover all permutations.
*/
public static String permutationHelper(String perm, String word) {
if (word.isEmpty()) return perm;
String a = "";
for (int i = 0; i < word.length(); i++)
{
a += permutationHelper(perm.trim() + word.charAt(i) + " ", word.substring(0, i) + word.substring(i+1, word.length()));
}
return a;
}
任何指导将不胜感激。
解决方案
/*
* The following method is given to you, and you will be responsible for completing the permutationHelper method it calls.
* Sometimes, helper methods are used for recursive methods when another parameter is needed to recursively call a method repeatedly, but passing that parameter initially doesn't make sense.
*/
public static String permutation(String word){
return permutationHelper(" ", word);
}
/* permutationHelper()
* This method is called by the permutation method.
* Given a string, return a string that lists all possible permutations of the letters in the string, with spaces preceding each permutation.
* For example, "123" would give "123 132 213 231 312 321".
* The perm parameter keeps track of the current permutation you are creating.
* Consider using the a for loop to call the method recursively a certain number of times with different parameters, so you cover all permutations.
*/
public static String permutationHelper(String perm, String word) {
if (word.isEmpty()) return perm;
String a = "";
for (int i = 0; i < word.length(); i++) a += permutationHelper(perm + word.charAt(i), word.substring(0, i) + word.substring(i+1, word.length()));
return a;
}
错误在说明中。教授们说忽略评论的例子,并确保在单词之前有烫发。
推荐阅读
- c# - 通过 GeckoDriver Selenium 和 C# 加载现有 FirefoxProfile 时,“System.IO.Compression.ZipStorer”的类型初始化程序引发异常
- javascript - 如何使用多个按钮使jquery计数器上下移动?
- javascript - 如何在动态加载内容中的链接上绑定“点击”?
- django - 如何从 django rest 中的单个类返回列表或检索对象?
- solidity - 如何获取与已发送事务对应的已发出事件
- reactjs - 单击复选框时如何添加行?因为我是新来的反应
- etl - 如何使用 talend 将 API 数据加载到 MYSQL 数据库中
- ios - FFMPEG 创建模糊视频
- ios - 错误 ITMS-90174:“缺少配置文件 - 应用程序必须在名为 embedded.mobileprovision 的文件中包含配置文件。”
- java - 如何将 Optional.ofNullable 下的空指针作为嵌套处理?