计数字母递归法
Count Letter recursive method
因此程序必须对字符串中的字母进行计数。除了递归循环,我不允许使用循环。
该方法必须如下所示:
static int numberOf(String text, char characterToCount)
输入:
abcbabcba (String) and b (char)
输出:
4
这就是我的代码到目前为止的样子(我得到了 Whosebug):
static int numberOf(String text, char characterToCount) {
int i = 0;
int erg = 0;
if (text.length() != 0) {
if (i != text.length()) {
if (text.charAt(i) == characterToCount) {
i++;
erg++;
numberOf(text, characterToCount);
} else {
i++;
numberOf(text, characterToCount);
}
} else {
return erg;
}
}
return 0;
}
编辑
我只能使用 String.charAt
和 String.length
问题是您在调用该方法时没有减少文本,因此长度永远不会减少到 0。这是您应该做的。请注意,您不需要将索引 传递给该方法。只需每次将文本减 1,然后检查第一个字符是否与目标字符相等。
public static void main(String[] args) {
System.out.println(numberOf("ksjssjkksjssss", 's'));
}
static int numberOf(String text, char characterToCount) {
if (text.isEmpty()) {
return 0;
}
if (text.charAt(0) == characterToCount) {
// call method and add 1 since you found a character
return numberOf(text.substring(1), characterToCount) + 1;
}
// just call the method.
return numberOf(text.substring(1), characterToCount);
}
以上打印
8
好的,这是我的修改版本,以满足您只使用 String.length
和 String.charAt
的要求。 char 实际上是 16 位,所以我使用高位字节来存储当前索引。我为每个递归调用增加该索引以保持搜索的当前位置。当我将 256
添加到字符时,我实际上是将 1
添加到高位字节。
static int numberOf(String text, char ch) {
// stop when index exceeds text length
if (ch >> 8 >= text.length()) {
return 0;
}
if (text.charAt((ch >> 8)) == (ch & 0xff)) {
return numberOf(text, (char)(ch + 256)) + 1;
}
return numberOf(text, (char)(ch + 256));
}
这在某些宽度超过 8 位的字符集上无法正常工作。
如果到达结尾returns 0,则可以使用索引变量。否则,return如果是字母或0则为1。
public class Main {
public static void main(String[] args) {
System.out.println(numberOf("Hello World-1234", 'o'));
}
private static int numberOf(String text, char characterToCount) {
if (!text.isEmpty()) {
return numberOf(text.substring(1), characterToCount) + (text.charAt(0) == characterToCount ? 1 : 0);
}
return 0;
}
}
编辑:没有 substring
的实施
public class Main {
public static void main(String[] args) {
System.out.println(numberOf("Hello World-1234", 'o'));
}
private static int numberOf(String text, char characterToCount) {
if (text.isEmpty()) {
return 0;
}
char[] chars = text.toCharArray();
char[] newChars = new char[chars.length - 1];
System.arraycopy(chars, 1, newChars, 0, newChars.length);
return numberOf(new String(newChars), characterToCount) + (chars[0] == characterToCount ? 1 : 0);
}
}
递归的思想是在某些条件下多次调用相同的 function/method。一个好的方法是调用相同的函数,但减少每次检查的字符串。
Class
public class StringUtils {
public int numberOf(String text, char characterToCount) {
int count = 0;
if (text.length() != 0) {
if(text.charAt(0) == characterToCount) { //Only increment when is the same character
count++;
}
count = count + numberOf(text.substring(1, text.length()), characterToCount); //Do a substring but remove the first character
}
return count;
}
}
测试
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class StringUtilsTest {
@Test
public void should_count_all_the_ocurrences() {
//Given
StringUtils utils = new StringUtils();
String sentence = "</www></palabraRandom></www></palabraRandom></palabraRandom></www>";
//When
int output = utils.numberOf(sentence, '>');
//Then
assertEquals(6, output);
}
}
WJS 的答案看起来不错,但如果您想要更简单的解决方案,这也可能有所帮助。
您的解决方案中的问题是您在一个调用堆栈中更新 i
和 erg
不会被下一个递归调用堆栈更新 seen/used,因为它们是局部变量并且每个堆栈都有自己的 i 和 erg 副本。在每次调用 numberOf
方法时,它们总是初始化为 0。
如果不允许使用子字符串,那么一种方法是使用一个额外的变量来保存您正在比较的文本中的位置索引。
但是在这样做时,您可能必须修改方法的签名(如果您不想使用 class 级别的静态变量)。并且由于您已经提到您的方法必须只有两个参数(文本,charToCount),因此轻松实现此目的的一种方法是使用辅助方法(包含额外的 index
参数)并且您的方法可以叫它。
static int numberOf(String text, char characterToCount) {
return helper(text, characterToCount, 0);
}
static int helper(String text, char charToCount, int index) {
if (text.isEmpty() || index == text.length()) return 0;
int countCharOnRight = helper(text, charToCount, index+1);
return (text.charAt(index) == charToCount) ? 1 + countCharOnRight : countCharOnRight;
}
什么
static int numberOf(String text, char characterToCount) {
return numberOfRecursive(text, characterToCount, 0);
}
// Recursive helper function
static int numberOfRecursive(String text, char characterToCount, int index) {
if (index == text.length()) // Abort recursion
return 0;
if (text.charAt(index) == characterToCount) // check char at index, then check next recursively
return numberOfRecursive(text, characterToCount, index + 1) + 1;
else
return numberOfRecursive(text, characterToCount, index + 1);
}
为什么
大多数递归问题都需要一个辅助函数,它实际上执行递归部分。它将从具有初始值的原始函数调用,这里使用我们的文本、字符和起始位置 0。
然后,递归函数需要一个中止条件,这是我通过绑定检查提供的。如果我们的递归到达字符串的末尾,我们将终止。
最后,递归函数根据递归调用进行一些计算。如果我们索引位置的字符是要计数的字符,那么我们将结果加 1。如果不是,我们继续计数而不加 1。
希望能帮到你
所以我想我找到了解决方案。
它可能不是那么好,但它有效。感谢您的帮助:)
public class CountLetters {
public static void main(String[] args) {
print("Bitte geben Sie den Text ein: ");
String text = readString();
text = toLowerCase(text, 0);
print("Bitte geben Sie ein Zeichen ein: ");
String zeich = readString();
zeich = toLowerCase(zeich, 0);
if (zeich.length() > 1) {
throw new PR1Exception("Bitte nur einen Buchstaben eingeben. ");
}
char zeichen = zeich.charAt(0);
if (zeichen > 0 && zeichen < 65 && zeichen > 90 && zeichen < 97 && zeichen > 123) {
throw new PR1Exception("Bitte nur Buchstaben eingeben.");
}
int anzahl = numberOf(text, zeichen);
println("-> " + anzahl);
}
static String toLowerCase(String text, int i) {
String lowerText = "";
if (i == text.length()) {
return lowerText;
} else if (text.charAt(i) < 'a') {
return lowerText += (char) (text.charAt(i) - 'A' + 'a') + toLowerCase(text, i + 1);
} else {
return lowerText += text.charAt(i) + toLowerCase(text, i + 1);
}
}
static int numberOf(String text, char characterToCount) {
return hilfe(text, characterToCount, 0, 0);
}
static int hilfe(String t, char ch, int i, int a) {
if (t.length() == a) {
return i;
} else if (t.charAt(a) == ch) {
return hilfe(t, ch, i + 1, a + 1);
} else {
return hilfe(t, ch, i, a + 1);
}
}
因此程序必须对字符串中的字母进行计数。除了递归循环,我不允许使用循环。
该方法必须如下所示:
static int numberOf(String text, char characterToCount)
输入:
abcbabcba (String) and b (char)
输出:
4
这就是我的代码到目前为止的样子(我得到了 Whosebug):
static int numberOf(String text, char characterToCount) {
int i = 0;
int erg = 0;
if (text.length() != 0) {
if (i != text.length()) {
if (text.charAt(i) == characterToCount) {
i++;
erg++;
numberOf(text, characterToCount);
} else {
i++;
numberOf(text, characterToCount);
}
} else {
return erg;
}
}
return 0;
}
编辑
我只能使用 String.charAt
和 String.length
问题是您在调用该方法时没有减少文本,因此长度永远不会减少到 0。这是您应该做的。请注意,您不需要将索引 传递给该方法。只需每次将文本减 1,然后检查第一个字符是否与目标字符相等。
public static void main(String[] args) {
System.out.println(numberOf("ksjssjkksjssss", 's'));
}
static int numberOf(String text, char characterToCount) {
if (text.isEmpty()) {
return 0;
}
if (text.charAt(0) == characterToCount) {
// call method and add 1 since you found a character
return numberOf(text.substring(1), characterToCount) + 1;
}
// just call the method.
return numberOf(text.substring(1), characterToCount);
}
以上打印
8
好的,这是我的修改版本,以满足您只使用 String.length
和 String.charAt
的要求。 char 实际上是 16 位,所以我使用高位字节来存储当前索引。我为每个递归调用增加该索引以保持搜索的当前位置。当我将 256
添加到字符时,我实际上是将 1
添加到高位字节。
static int numberOf(String text, char ch) {
// stop when index exceeds text length
if (ch >> 8 >= text.length()) {
return 0;
}
if (text.charAt((ch >> 8)) == (ch & 0xff)) {
return numberOf(text, (char)(ch + 256)) + 1;
}
return numberOf(text, (char)(ch + 256));
}
这在某些宽度超过 8 位的字符集上无法正常工作。
如果到达结尾returns 0,则可以使用索引变量。否则,return如果是字母或0则为1。
public class Main {
public static void main(String[] args) {
System.out.println(numberOf("Hello World-1234", 'o'));
}
private static int numberOf(String text, char characterToCount) {
if (!text.isEmpty()) {
return numberOf(text.substring(1), characterToCount) + (text.charAt(0) == characterToCount ? 1 : 0);
}
return 0;
}
}
编辑:没有 substring
public class Main {
public static void main(String[] args) {
System.out.println(numberOf("Hello World-1234", 'o'));
}
private static int numberOf(String text, char characterToCount) {
if (text.isEmpty()) {
return 0;
}
char[] chars = text.toCharArray();
char[] newChars = new char[chars.length - 1];
System.arraycopy(chars, 1, newChars, 0, newChars.length);
return numberOf(new String(newChars), characterToCount) + (chars[0] == characterToCount ? 1 : 0);
}
}
递归的思想是在某些条件下多次调用相同的 function/method。一个好的方法是调用相同的函数,但减少每次检查的字符串。
Class
public class StringUtils {
public int numberOf(String text, char characterToCount) {
int count = 0;
if (text.length() != 0) {
if(text.charAt(0) == characterToCount) { //Only increment when is the same character
count++;
}
count = count + numberOf(text.substring(1, text.length()), characterToCount); //Do a substring but remove the first character
}
return count;
}
}
测试
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class StringUtilsTest {
@Test
public void should_count_all_the_ocurrences() {
//Given
StringUtils utils = new StringUtils();
String sentence = "</www></palabraRandom></www></palabraRandom></palabraRandom></www>";
//When
int output = utils.numberOf(sentence, '>');
//Then
assertEquals(6, output);
}
}
WJS 的答案看起来不错,但如果您想要更简单的解决方案,这也可能有所帮助。
您的解决方案中的问题是您在一个调用堆栈中更新 i
和 erg
不会被下一个递归调用堆栈更新 seen/used,因为它们是局部变量并且每个堆栈都有自己的 i 和 erg 副本。在每次调用 numberOf
方法时,它们总是初始化为 0。
如果不允许使用子字符串,那么一种方法是使用一个额外的变量来保存您正在比较的文本中的位置索引。
但是在这样做时,您可能必须修改方法的签名(如果您不想使用 class 级别的静态变量)。并且由于您已经提到您的方法必须只有两个参数(文本,charToCount),因此轻松实现此目的的一种方法是使用辅助方法(包含额外的 index
参数)并且您的方法可以叫它。
static int numberOf(String text, char characterToCount) {
return helper(text, characterToCount, 0);
}
static int helper(String text, char charToCount, int index) {
if (text.isEmpty() || index == text.length()) return 0;
int countCharOnRight = helper(text, charToCount, index+1);
return (text.charAt(index) == charToCount) ? 1 + countCharOnRight : countCharOnRight;
}
什么
static int numberOf(String text, char characterToCount) {
return numberOfRecursive(text, characterToCount, 0);
}
// Recursive helper function
static int numberOfRecursive(String text, char characterToCount, int index) {
if (index == text.length()) // Abort recursion
return 0;
if (text.charAt(index) == characterToCount) // check char at index, then check next recursively
return numberOfRecursive(text, characterToCount, index + 1) + 1;
else
return numberOfRecursive(text, characterToCount, index + 1);
}
为什么
大多数递归问题都需要一个辅助函数,它实际上执行递归部分。它将从具有初始值的原始函数调用,这里使用我们的文本、字符和起始位置 0。
然后,递归函数需要一个中止条件,这是我通过绑定检查提供的。如果我们的递归到达字符串的末尾,我们将终止。
最后,递归函数根据递归调用进行一些计算。如果我们索引位置的字符是要计数的字符,那么我们将结果加 1。如果不是,我们继续计数而不加 1。
希望能帮到你
所以我想我找到了解决方案。 它可能不是那么好,但它有效。感谢您的帮助:)
public class CountLetters {
public static void main(String[] args) {
print("Bitte geben Sie den Text ein: ");
String text = readString();
text = toLowerCase(text, 0);
print("Bitte geben Sie ein Zeichen ein: ");
String zeich = readString();
zeich = toLowerCase(zeich, 0);
if (zeich.length() > 1) {
throw new PR1Exception("Bitte nur einen Buchstaben eingeben. ");
}
char zeichen = zeich.charAt(0);
if (zeichen > 0 && zeichen < 65 && zeichen > 90 && zeichen < 97 && zeichen > 123) {
throw new PR1Exception("Bitte nur Buchstaben eingeben.");
}
int anzahl = numberOf(text, zeichen);
println("-> " + anzahl);
}
static String toLowerCase(String text, int i) {
String lowerText = "";
if (i == text.length()) {
return lowerText;
} else if (text.charAt(i) < 'a') {
return lowerText += (char) (text.charAt(i) - 'A' + 'a') + toLowerCase(text, i + 1);
} else {
return lowerText += text.charAt(i) + toLowerCase(text, i + 1);
}
}
static int numberOf(String text, char characterToCount) {
return hilfe(text, characterToCount, 0, 0);
}
static int hilfe(String t, char ch, int i, int a) {
if (t.length() == a) {
return i;
} else if (t.charAt(a) == ch) {
return hilfe(t, ch, i + 1, a + 1);
} else {
return hilfe(t, ch, i, a + 1);
}
}