0

对于作业,我应该使用递归找到句子中最长的单词。我写了一个方法,它取句子的前两个词,比较它们,然后取两者中较长的一个,并将其与句子其余部分的下一个词进行比较。我的逻辑检查出来了,但该方法无法正常工作。我认为有一种侥幸会占用空间,这就是它不起作用的原因。

public static String longestWord(String sentence)
{
    if (sentence.indexOf(' ') == -1) {      // IF sentence only has one word
        return sentence;
    }

    String word1 =(sentence.indexOf(" ") != -1)? sentence.substring(0, sentence.indexOf(" ")):
                                                    sentence.substring(0);
    String temp = sentence.substring(sentence.indexOf(" ")+1);


    String word2 = null;
    String rest = null;
    if (sentence.indexOf(" ") != -1)    {
     word2 = (temp.indexOf(" ") != -1)? temp.substring(0, temp.indexOf(" ")+1):
                                            temp.substring(0);
     rest = temp.substring(temp.indexOf(" ")+1);
    }


    if (word1.length() > word2.length()) {
        return longestWord(word1 + rest);

    }

    if (word2.length() > word1.length())  {
        return longestWord(word2 + rest);

    }

    return sentence;


}
4

6 回答 6

1

有一种简单的递归方法:将字符串拆分为单词,然后递归调用此方法,如下所示:

string longest(string sentence) {
    return longestRecursive(sentence.Split(' '), 0, "");
}

string longestRecursive(string[] words, int index, string longestSoFar) {
    // This should be very easy to implement:
    // If index == words.Length, longestSoFar is your answer
    // Check words[0] against longestSoFar, and make a recursive invocation
}
于 2012-12-10T03:33:14.943 回答
1

您在这里遇到了几个问题,但我认为吸引您的是您设置rest为在开头没有空格,但随后您将一个单词连接到它的开头。所以“一只快速的棕色狐狸”->“快速棕色的狐狸”。

但是,除此之外,如果这两个词的长度相同,那么您将返回整个句子 - 相反,您应该将最后一个if语句作为else语句并删除最后一个return语句。

编辑:虽然你可能不想扔掉你所拥有的东西,但如果你反转焦点,你可能会发现递归解决方案更简单:不要每次都取前两个单词,而是只取第一个单词并将其与余数中最长的词:

longestWord(String sentence) {
    if (sentence.indexOf(' ') == -1) {      // IF sentence only has one word
        return sentence;
    }
    String firstWord = getFirstWord(sentence);//how you're doing it now
    String rest = getRest(sentence);//Just the sentence without the first word (and first space...)
    String secondWord = longestWord(rest);
    return firstWord.length >= secondWord.length ? firstWord : secondWord;
}
于 2012-12-10T03:34:55.170 回答
0

如果句子是字符串。

使用 String.Split(" "); 拆分句子

将结果存储在字符串数组中。

使用 .length 使用递归查找最长的字符串。

于 2012-12-10T03:35:50.263 回答
0

我会这样做

public static String longestWord(String sentence) {
        return longestWord(sentence, "");
    }

    private static String longestWord(String sentence, String longestWord) {
        int i = sentence.indexOf(' ');
        if (i == -1) {
            return sentence.length() > longestWord.length() ? sentence : longestWord;
        }
        longestWord = i > longestWord.length() ? sentence.substring(0, i) : longestWord;
        sentence = sentence.substring(i + 1);
        return longestWord(sentence, longestWord);
    }
于 2012-12-10T03:48:32.990 回答
0

Java 不做尾调用优化,所以这很容易破坏堆栈。但是您要求递归(并且在具有 TCO 的语言中,它是堆栈中立的)。请注意,它只构造一个字符串。

public static String longestWord(String sentence) {
    return longestWordHelper(sentence, 0, 0, 0, 0);
}

String longestWordHelper(String sentence,
                         int best_len, int best_end,
                         int cur_len, int cur) {
  if (cur == sentence.length()) 
    if (cur_len > best_len)
      return sentence.substring(cur_end - cur_len, cur_len);
    else
      return sentence.substring(best_end - best_len, best_len);
  if (isSpace(sentence.charAt(cur)))
    if (cur_len > best_len)
      return longestWordHelper(sentence, cur_len, cur, 0, cur + 1);
    else
      return longestWordHelper(sentence, best_len, best_end, 0, cur + 1);
  else
    return longestWordHelper(sentence, best_len, best_end, cur_len + 1, cur + 1);
}
于 2012-12-10T04:43:55.923 回答
0
package MujeebWorkspace.sampleprograms;

class javaMujeeb{       
static String actualstring= "Today is a very good day";
static String[] splitstring = actualstring.split(" ");

public static void main(String [] args){
    LongWord();
    ShortWord();    }

public static void LongWord() {     
    String longword = "";
    for (int i=0; i<=splitstring.length-1; i++){    
    if (longword.length()<splitstring[i].length())
        longword = splitstring[i];  }
    System.out.println(longword);   }

    public static void ShortWord(){
        String shortword = "  ";
        for (int i=0; i<=splitstring.length-1; i++){        
        if (splitstring[i].length()<shortword.length())
            shortword = splitstring[i];         }       
        System.out.println(shortword);      }

}

于 2013-12-01T05:01:19.450 回答