繁体   English   中英

递归计算字符串中的字符出现次数

[英]Recursively counting character occurrences in a string

我正在制作一个程序来计算在字符串中找到字符的次数。 这是我的方法的样子:

public static int count (String line, char c)
{
    int charOccurences = 0; //= 0;

    for (int x = 0 ; x < line.length () ; x++)
    {
        if (line.charAt (x) == c)
        {
            charOccurences++;
            line = line.substring (0, x) + line.substring (x + 1);
            return count (line, c);
        }
        else
            return charOccurences;
    }
    return charOccurences;
}

它总是返回0,因为一旦该方法调用自身它将charOccurences设置回0.但我需要声明该变量以使该方法起作用。 我无法解决这个问题。 任何帮助,将不胜感激。

您在增加后立即忽略了charOccurences。

charOccurences++;
line = line.substring (0, x) + line.substring (x + 1);
return charOccurences + count (line, c); // Fixed for you.

其他人提到你根本不需要for循环。 如果你想纯粹递归地执行此操作,则只需丢失循环,并按照以下步骤操作:

  • 基本情况:
    • 第一个字符不存在(长度为零)
      • return 0;
  • 递归案例:
    • 第一个角色确实存在
      • 如果匹配,则增加出现次数
      • 别无所求
      • return(occurrence)(用子串递归的结果);

是的,它很容易递归:)

public static void main(String[] args) {
    String text = "This is my text. Life is great";
    System.out.println(count(text,'i',0));
}

public static int count(String line, char c, int pos) {
    if (pos >= line.length()){
        return 0;
    }

    return compare(line.charAt(pos), c) + count(line, c, pos+1);
}

public static int compare(char a, char b){
    if (a == b){
        return 1;
    } else {
        return 0;
    }
}

请注意,由于每次都没有子串,时间复杂度是O(n)而不是你的O(n ^ 2)

这是一种为递归方法编写递归方法的一般方法,这些方法实际上不应该是递归的,但必须是因为你正在学习类中的递归:

找到一种方法将问题分解为一个较小的问题。

在这里,您的问题是计算字符串中字符c的出现次数。 好吧,假设你将你的字符串分解成“第一个字符”和“所有其他字符”的子字符串。 你可以判断第一个字符是否等于c 然后你看看“所有其他字符”,如果那不是空的(基本情况),那么这只是同一问题的一个较小版本 所以你可以使用递归。 因此假装递归已经发生,那么你知道:(1)是第一个等于c字符,(2)在较小的字符串中有多少个c出现。 一旦你知道这两个数据,你应该能够弄清楚整个字符串中有多少c出现。

对于此问题,您的解决方案不应该有一个循环。

你永远不会增加计数。 你只是继续回头数。 在递归堆栈的最后,count将返回0,因为这是在每次方法调用开始时初始化count的内容,并且它将一直返回零,直到它到达堆栈的底部,然后返回0。你需要这样做:

charOccurences += count (line, c);
return charOccurences;

所以charOccurences将在第一次出现时从1开始,然后向上传播。

尽管递归不需要它(让我们这样做是为了好玩)。 你差不多完成了。 只要确保有一个停止递归的条件:这里是if (len == 0)…语句。

public static int count (String line, char c)
{
    int len = line.length();
    if ((len == 0) || (c == '\0'))   // obvious case for empty string or nil char.
       return 0;                     // Your recursion always ends here

    String rest = line.substring(1);
    if (line.charAt(0) == c)     
    {
       return count(rest, c) + 1;   // recurse on substring
    }
    else
    {
       return count(rest, c);   // recurse on substring
    }
}

我认为你做得比它需要的要困难得多吗?

public static int count(String line, char c) {
    int orig = line.length();
    int repl = line.replaceAll("" + c, "").length();
    return orig - repl;
}

我有同样的问题,你可以随时做到这一点我做了一个单词同样适用于一个句子

private static int count(String word, String letter) {
    int count = 0;
 return occurrence(word, letter, count);
}

private static int occurrence(String word, String letter, int count) {
if () 
base case 
else 
// compare and increment if it matches..
return occurrence(word.substring(0, word.length() - 1), letter,count)

} 

另一种方法出现的是递归方法,现在重复你的代码已经定义了count,你可以增加而不会有任何问题! :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM