对于作业,我应该使用递归找到句子中最长的单词。我写了一个方法,它取出句子的前两个单词,比较它们,然后取两个中较长的一个,并将其与句子其余部分的下一个单词进行比较。我的逻辑检出,但方法无法正常工作。我认为有一个侥幸会占用空间,这就是为什么它不起作用。
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;
}
答案 0 :(得分: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
}
答案 1 :(得分: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;
}
答案 2 :(得分:0)
如果句子是字符串。
使用String.Split(“”);
拆分句子将结果存储在字符串数组中。
使用递归查找使用.length。
的最长字符串答案 3 :(得分: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);
}
答案 4 :(得分: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);
}
答案 5 :(得分: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); }
}