【问题标题】:Find length of the longest contiguous subsequence of the same character [closed]查找相同字符的最长连续子序列的长度[关闭]
【发布时间】:2015-03-27 02:04:40
【问题描述】:

我正在尝试从字符串中找到最长的连续字符重复子序列。

public int longestRep(String str) {

}

当你调用方法时

longestRep("ccbbbaaaaddaa"); //Should return 4

到目前为止我使用的代码是;

public static int longestRep(String str)
{
    int currLen = 1; // Current length of contiguous chars being held in str
    char currLet = ' '; // Current Letter *NOT NEEDED FOR CODINGBAT
    char maxLet = ' '; // Maximum length letter *NOT NEEDED FOR CODINGBAT
    int maxLen = 0; // Maximum length of contiguous chars being held in str
    //int maxCount = 0; // Highest count of contiguous chars being held in str
    int currPos = 0; // Track where in str we are at
    int strLen = str.length(); // Length of str;
    for(currPos = 0; currPos < strLen -1 ; currPos++)
    {
        currLet = str.charAt(currPos);
        //System.out.println("Curr char: "+currLet+"  Next Char: "+str.charAt(currPos+1));
        if(currLet == str.charAt(currPos+1))
        {
            currLen++;
        }
        if(currLen > maxLen)
        {
            maxLen = currLen;
            //System.out.println("Max len: "+maxLen+"  Curr Len: "+currLen);
            //maxLet = currLet;
            currLen = 1;
        }
        boolean atBeginning = true;
        if(currPos == 0)
        {
            atBeginning = true;
        }
        else if(currPos != 0)
        {
            atBeginning = false;
        }
        if(atBeginning == false) //if not at the beginning of the string
        {
            if(currLet != str.charAt(currPos+1) && currLet == str.charAt(currPos-1))
            {
                currLen++;
            }
        }
        if(currLen > maxLen)
        {
            maxLen = currLen;
            currLen = 1;
        }
    }

    return maxLen;
  }
public static void main(String args[])
{
    int result = longestRep("abcdeeefeeeefppppppp");
    System.out.println(result);
}

但是,我收到无效的回复,并且不确定我做错了什么。 我是Java的新手。我刚刚编写的一些代码可能/可能不会被使用。

【问题讨论】:

  • 请定义"invalid responses"
  • 你可以像这样初始化 atBeginning: boolean atBeginning = (currPos == 0);或者更好的是你可以完全摆脱它并使用 if(currPos != 0) 而不是 if(atBeginning == false)
  • boolean atBeginning = true; 之后的所有东西似乎都没有必要

标签: java debugging response


【解决方案1】:

boolean atBeginning = true; 之后的任何事情我都无法确定,坦率地说,这似乎不是必需的......

你的基本逻辑应该遵循...

if character_at_current_position is equal to next_character then
    currLen = currLen + 1
else if currLen is greater then maxLen then
    maxLen = currLen
    currLen = 1
else currLen = 1

您还需要在循环外再次检查currLen is greater then maxLen,以说明最后一个字符可能做出的任何更改

更像...

for (currPos = 0; currPos < strLen - 1; currPos++) {
    currLet = str.charAt(currPos);
    //System.out.println("Curr char: "+currLet+"  Next Char: "+str.charAt(currPos+1));
    if (currLet == str.charAt(currPos + 1)) {
        currLen++;
    } else if (currLen > maxLen) {
        maxLen = currLen;
        currLen = 1;
    } else {
        currLen = 1;
    }
}
if (currLen > maxLen) {
    maxLen = currLen;
}

所以使用

System.out.println(longestRep("ccccccaaaabbbbb")); // 6 c's
System.out.println(longestRep("ccbbbaaaaddaa")); // 4 a's
System.out.println(longestRep("abcdeeefeeeeeeeefppppppp")); // 8 e's

我明白了……

6
4
8

【讨论】:

    【解决方案2】:

    试试:

    public int longestRep(String s){
        char[] c = s.toCharArray();
        int amt = 0;
        char current;
        int  max=0;
    
        for(int i = 0; i < c.length; i ++){
             if(current == c[i])
                 amt ++;
             else{
    
                 amt = 1;
                 current = c[i];
             }
             if (max < amt) 
                  max=amt;
        }
    
        return max;
    }
    

    这将迭代您的字符串并检查i 处的字符是否与当前字符相同,如果是,它将向amt 添加一个,否则它将当前设置为char 并设置amt 到 1。

    【讨论】:

    • 您现在想提供一个可以编译和工作的版本吗...
    猜你喜欢
    • 2016-07-17
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多