查找相同字符的最长连续子序列的长度
Find length of the longest contiguous subsequence of the same character
我正在尝试从字符串中查找连续字符的最长重复子序列。
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 的新手。有些代码我刚刚写的,may/may 没有被使用。
尝试:
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.
在 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
我正在尝试从字符串中查找连续字符的最长重复子序列。
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 的新手。有些代码我刚刚写的,may/may 没有被使用。
尝试:
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.
在 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