当前位置: 首页 > 知识库问答 >
问题:

查找整数中存在数字的次数

张高义
2023-03-14

我正在尝试创建一个递归方法来查找整数中特定数字出现的次数。例如,如果数字为 13563,数字为 3,则该方法应返回 2,因为 3 在数字中出现两次。然而,我对我的基本情况应该是什么感到困惑。

public static int digit(int number, int digit){
if(number.indexOf(0) == digit){
    return 1;
}
else{
    return 1 + digit(number, digit);
}
}

共有3个答案

蒋永宁
2023-03-14

使用modulos可以很容易地做到这一点,这可以避免制作字符串的成本。

public static int digit(int number, int digit)
{
    if ( number <= 0 ) {
    return 0;
    }
    else {
    if ( number % 10 == digit ) {
        return 1 + digit (number / 10, digit);
    }
    else {
        return digit (number / 10, digit);
    }
    }
}

现在这段代码不适用于负数或空数,它不是尾递归的,所以我们可以让它变得更好。

public static int digit_aux (int number, int digit, int result)
{
    if ( number == 0 ) {
        return result;
    }
    else {
    return digit_aux (number / 10, digit,
                      result + ( ( number % 10 == digit ) ? 1 : 0) );
    }
}


public static int digit (int number, int digit)
{
  if ( number == 0 ) {
   return  (digit == 0) ? 1 : 0;
  }
  else {
   return digit_aux ( Math.abs(number), digit, 0);
  }
}

这是进行递归的好方法(即使我不确定Java是否开发了尾调用优化)。

顾曾笑
2023-03-14

将其转换为字符串,然后使用这里描述的方法:计算字符串中字符出现次数的简单方法

String s = "...";
int counter = 0;
for( int i=0; i<s.length(); i++ ) {
    if( s.charAt(i) == '$' ) {
        counter++;
    }  
} 

要转换为字符串:

String.valueOf(number);
徐卓
2023-03-14

如果你想使用递归,下面是解决方案:

public static int recurseCountDigits(final int number, final int digit){
    if(number < 10) {
        // if the number is less than 10, return if the digit is present, and don't recurse any deeper
        return number == digit ? 1 : 0;
    } else {
        // if the right-most digit is correct
        if(number%10 == digit) {
            // return 1 + the value from the remaining digits (recursion)
            return 1 + recurseCountDigits(number/10, digit);
        } else {
            // else just return the value from the remaining digits (recursion)
            return recurseCountDigits(number/10, digit);
        }
    }
}

此解决方案适用于非负数字和非负数字。其他组合可能有效,也可能无效。

 类似资料:
  • 这里有一些示例来展示您的函数应该如何工作。 我已经两天没睡了,我能写的最好的代码是

  • 问题内容: 给你一个整数数组。除一次外,所有数字出现偶数次。您需要找到出现奇数次的数字。你需要用 o(n) 时间复杂度和 o(1) 空间复杂度来解决它。 例如: 问题答案: 解决方案 1:使用两个 for 循环并比较元素: 这是这个问题的蛮力解决方案,但它需要 o(n*n) 时间复杂度。 解决方案 2:使用Hashing 您可以将 key 用作数字并将 count 用作值,每当 key 重复时,您

  • 问题内容: 改写: 在我的项目中,我有图像。每个图像有5个标签,范围为[1,10]。我用Elasticsearch上传了这些标签: 我将这些文件加载​​到类型为“ img”的索引“ my_project”中的elasticsearch中: 我上传的其他示例文件: 在我的应用程序中,向量要长得多,但是具有固定数量的唯一元素。我喜欢这些文件中的20M。 现在,我想找到给定向量的相似文档。向量具有更多公

  • 问题内容: 好吧,假设我有一个用{“ tube”,“ are”,“fun”}填充的数组,然后我有一个JTextField,如果我键入其中一个命令来执行某项操作,或者如果输入的不是这样,则会显示一条消息,说“没有找到指令”。 我尝试查看Java文档,但是得到的只是我不想要的东西,例如问题和东西……所以,这是怎么做的?我知道有一个“数组中的”功能,但是我不能很好地将两者结合在一起。 谢谢。 这是我到目

  • 我需要编写一个递归方法,将int作为输入,并以int(而不是字符串)的形式返回其中最长的相同数字序列。计数序列并不是最难的部分,但当给定一个包含几个序列的数字时,我不知道如何返回正确的值,而不计算所有的序列,而只计算最长的序列。目前,我编写了一段只计算序列长度的代码: 我真的很难完成剩下的事情。

  • 问题内容: 我有一个特定的字符串,例如“ 123abcd”,但我不知道表的名称,甚至不知道SQL Server数据库中表内的列的名称。我想通过选择找到它并显示相关字符串的所有列,所以我想知道以下内容: 出于显而易见的原因,它不起作用,但是有一种简单的方法可以创建一个select语句来执行类似的操作? 问题答案: 这将起作用: 不过,有几点警告。首先, 这是极其缓慢且未优化的 。所有值都将被简单地转