递归计算java中的字符

时间:2014-05-10 15:29:24

标签: java recursion

我需要一点帮助。 我需要递归地编写这个方法: public static int howManyChar(String s,char c) 并且不使用substring方法和循环..

任何想法? 我被困在这一点上。

public static int howManyChar (String s, char c)
{
    int count = 0;
    int lastChar = s.length()-1;
    if (s.length()== 0)
        return 0;
    if (s.charAt(lastChar) == c)
    {
       count++;
    }

我成功使用了子串方法:

    int count =0;
    if (s.charAt(0)==c)
        count++;
    if (s.length()>1)
    {
        count+=howManyChar(s.substring(1), c);
    }
    return count;

但我想在不使用子字符串的情况下编写代码。

4 个答案:

答案 0 :(得分:0)

我在这里看了一些答案,它们对你的问题来说太复杂了,使用子字符串,或者不使用基本的递归

你的解决方案应该是这样的。

<?php 

namespace App\Custom;     
 class Maths{


         public  function add($number1,$number2) 
          { 
                   $result =$number1 + $number2; 
                    return $result; 
           } 

          public  function subtract($number1,$number2) 
          { 
                   $result =$number1 - $number2; 
                    return $result;
           } 

          public  function divide($number1,$number2) 
          { 
                   $result =$number1 / $number2; 
                    return $result; 
           } 

          public  function multiply($number1,$number2) 
          { 
                   $result =$number1 * $number2; 
                    return $result;
           } 
} 

这部分代码是循环。

<!-- experiments with table & button-input  vegaseat  2/13/02 -->
<!-- table, border, cellpadding, cellspacing, input, onClick -->
<!-- uses eval() to do the final calculation -->
<!-- note: eval() traps divide by zero error -->
<html>
<head>
    <title>A Simple Four-Banger</title>

</head>
<body>

<form name="calculator" method="post" action="/result">
<!-- form the display to match the keypad -->
<input type="hidden" name="_token" value="{{ csrf_token() }}">

<table border="4" cellpadding="1" bordercolor="#FFFFFF" bgcolor="#73B27B" cellspacing="2" width="222">
<tr>
<td>


<input type="text" size="25" length="25" value="<?php if(isset($data)){
     echo $data['answer'];
}
?>" id="ans" name="ans" style="background:beige;color:black;">
</td>
</tr>
</table>
<!-- form the keypad with buttons in a table -->
<table border="4" cellpadding="2" bordercolor="#FFFFFF" cellspacing="2" width="150" bgcolor="#73B27B">
<tr>
<td align="center">
<input type="button" value="  7  " name="seven" onClick="document.calculator.ans.value+='7'">
</td>
<td align="center">
<input type="button" value="  8  " name="eight" onClick="document.calculator.ans.value+='8'">
</td>
<td align="center">
<input type="button" value="  9  " name="nine" onClick="document.calculator.ans.value+='9'">
</td>
<td align="center">
<input type="button" value="  /  " name="divide" onClick="document.calculator.ans.value+='/'">
</td>
</tr>
<tr>
<td align="center">
<input type="button" value="  4  " name="four" onClick="document.calculator.ans.value+='4'">
</td>
<td align="center">
<input type="button" value="  5  " name="five" onClick="document.calculator.ans.value+='5'">
</td>
<td align="center">
<input type="button" value="  6  " name="six" onClick="document.calculator.ans.value+='6'">
</td>
<td align="center">
<input type="button" value="  *  " name="multiply" onClick="document.calculator.ans.value+='*'">
</td>
</tr>
<tr>
<td align="center">
<input type="button" value="  1  " name="one" onClick="document.calculator.ans.value+='1'">
</td>
<td align="center">
<input type="button" value="  2  " name="two" onClick="document.calculator.ans.value+='2'">
</td>
<td align="center">
<input type="button" value="  3  " name="three" onClick="document.calculator.ans.value+='3'">
</td>
<td align="center">
<input type="button" value="  -  " name="subtract" onClick="document.calculator.ans.value+='-'">
</td>
</tr>
<tr>
<td align="center">
<input type="button" value="  C  " name="clear" onClick="document.calculator.ans.value=''">
</td>
<td align="center">
<input type="button" value="  0  " name="zero" onClick="document.calculator.ans.value+='0'">
</td>
<td align="center">
<input type="submit" value="  =  " name="equal" >
</td>
<td align="center">
<input type="button" value="  +  " name="add" onClick="document.calculator.ans.value+='+'">
</td>
</tr>
<tr>

<a href="/logs">History</a>

</tr>
</table>
</form>
</body>
</html>

了解循环。你应该使用charAt(i)substring不是必要的..

为了满足要求,您可以将方法包装在另一个递归方法中。

function(String a, char b, int i)
if(i==a.length)
return 0
else if(b ==a.charAt(i))
return 1 + function(a, b, i+1)
else
return function(a,b,i+1)

答案 1 :(得分:-1)

试试这个

public static int howManyChar (String s, char c) { if (s.length()==0) return 0; return s.charAt(0) == c ? 1 : 0 + howManyChar(s.substring(1), c); }

如果你不能使用子串方法

public static int howManyChar (String s, char c, int pos) { if (pos >= s.length()) return 0; return s.charAt(pos) == c ? 1 : 0 + howManyChar(s, c, ++pos); }

答案 2 :(得分:-1)

public static int howManyChar (String s, char c)
{
    index++;
        if (s.length() == index)
            return 0;
        else if(s.charAt(index) == c)
           return 1 + howManyChar(s, c);
        else
            return 0 +  howManyChar(s, c);

  }

现在这是最好的解决方案,我有一个类级变量(索引)保存字符串中的位置。但是,如果不使用子字符串,我就无法想到更好的方法。

答案 3 :(得分:-1)

我有一个没有下标的好解决方案......

public class test{

   int counter=0;int last;
    public static void main(String[] args){
        test t1=new test(); //object of test
        String line="This is a working code"; //line in which char to be counted
        char c='q'; // char to be counted
        t1.last=line.length()-1;  //setting last
        System.out.println("In sentence: "+line);
        if(t1.count(line, c)!=-1)
        System.out.println("The character "+c+" is "+t1.counter+" times"); //printing result
        else
        System.out.println("The character "+c+" is not found");
    }
    public int count (String line, char c){
        if (last<0){   // case for empty string or nil char.
            return counter;                     // recursion ends here and return counted
        }
        else if(line.charAt(last)==c)
        {
            counter++;  //counting
        }
        last--;  //shitin last;
        count(line, c);
        return -1;  //if even a single times is not found then return -1;
      }
}

它可以帮助你...你可以从命令提示符中搜索作为输入的行和字符......谢谢....