来自CodingBat的maxBlock替代解决方案

时间:2014-05-29 15:27:36

标签: java

从codingBat解析this problem

  

给定一个字符串,返回最大“块”的长度   串。块是一系列相邻的字符。

     

maxBlock("hoopla") → 2
  maxBlock("abbCCCddBBBxx") → 3
  maxBlock("") → 0

我试图使用一个for循环来解决它,如下所示:

public int maxBlock(String str) {
  int maxCounter=1;
  int counter=1;
  if(str.length()==0)
  {
    return 0;
  }  
  for(int i=0;i<str.length()-1;i++)
  {
    if(str.substring(i,i+1).equals(str.substring(i+1,i+2)))
    {
      counter++;

    }   
    if(counter>maxCounter)
    {
      maxCounter=counter;
      counter=0;
    }            
  }  


  return maxCounter;          
}
除了一个以外,它击败了所有案件。任何人都可以通过一个for循环显示解决方案吗?

很抱歉提及但是你不能使用REGEX或集合框架中的任何东西。

12 个答案:

答案 0 :(得分:2)

您可以使用Pattern匹配器"(.)(\\1)*"在字符串中查找重复的char,这是代码:

public int maxBlock(String str) {
        Pattern pattern = Pattern.compile("(.)(\\1)*");
        Matcher matcher = pattern.matcher(str);
        int max = 0;
        while (matcher.find()) {
            max = Math.max(max, matcher.group().length());
        }
        return max;
    }

答案 1 :(得分:2)

我认为你在某些边缘情况下弄错了:

public int yourMaxBlock(String str) {
    int maxCounter = 1;
    int counter = 1;
    if (str.length() == 0) {
        return 0;
    }
    for (int i = 0; i < str.length() - 1; i++) {
        if (str.substring(i, i + 1).equals(str.substring(i + 1, i + 2))) {
            counter++;

        }
        if (counter > maxCounter) {
            maxCounter = counter;
            counter = 0;
        }
    }

    return maxCounter;
}

public int myMaxBlock(String str) {
    int maxCounter = 1;
    int counter = 1;
    if (str.isEmpty()) {
        return 0;
    }
    for (int i = 1; i < str.length(); i++) {
        if (str.charAt(i - 1) == str.charAt(i)) {
            if (++counter > maxCounter) {
                maxCounter = counter;
            }
        } else {
            counter = 1;
        }
    }

    return maxCounter;
}

public void test() {
    String[] tests = new String[]{
        "", "+", "++", "+++,++,++,+", "+,++,+++,++,", "+,++,+++,++++", "+++++,++,+++,++++"
    };
    for (String s : tests) {
        int myMax = myMaxBlock(s);
        int yourMax = yourMaxBlock(s);
        System.out.println("myMaxBlock(" + s + ") = " + myMax + (myMax != yourMax ? " WRONG! you have " + yourMax : ""));
    }
}

打印

myMaxBlock() = 0
myMaxBlock(+) = 1
myMaxBlock(++) = 2
myMaxBlock(+++,++,++,+) = 3
myMaxBlock(+,++,+++,++,) = 3
myMaxBlock(+,++,+++,++++) = 4 WRONG! you have 3
myMaxBlock(+++++,++,+++,++++) = 5 WRONG! you have 4

答案 2 :(得分:2)

我参加派对有点晚了,但这是我的CodingBat解决方案:

public int maxBlock(String str) {
    int max = 0;
    int count = 1;
    char o = ' ';

    for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        if (c == o) {
            count++;
            if (count > max) { max = count; }
        } else {
            count = 1;
            if (count > max) { max = count; }
        }
        o = c;     
    }

    return max;
}

答案 3 :(得分:1)

这是一个基于你的解决方案。请注意使用charAt来查找更整洁的代码示例。

这从字符串的第二个字符开始,向后看,看看我们是否仍然遇到相同的字符。如果是这样,计数器会增加。当我们完成一串相同的字符时,我们会比较到目前为止找到的最大长度,并在必要时进行更新。

public static int maxBlock(String str) {
    int maxCounter = 1;
    int counter = 1;

    if (str.length() == 0) {
        return 0;
    }
    for (int i = 1; i < str.length(); i++) {
        if (str.charAt(i - 1) == str.charAt(i)) {
            counter++;
        } else {
            // end of a run
            if (counter > maxCounter) {
                maxCounter = counter;
            }

            counter = 1;
        }

    }

    return Math.max(maxCounter, counter);
}

答案 4 :(得分:0)

只需使用一个for循环。我认为这是另一种方式。

public int maxBlock(String str) {
  int len = str.length();
  int temp=(len>0)?1:0;
  int r =0;

  for(int i=1; i<len; i++){   
    if(str.charAt(i) == str.charAt(i-1)){
      temp++;   
    }
    else{
      r = (temp>r)?temp:r;
      temp=1;
    }
  }
  r = (temp>r)?temp:r;
  return r;
}

答案 5 :(得分:0)

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

答案 6 :(得分:0)

这是我的解决方案。这比你想象的要简单。

public int maxBlock(String str) 
{
    //create a counter to return
    int counter = 0;
    //create a temporary variable to store a running total.
    int temp = 1;
    //Start on the first character and test to see if the second
    //character equals the first.
    for (int i = 1; i < str.length(); i++)
    {
      //If it does, we increment the temp variable.
      if (str.charAt(i) == str.charAt(i-1))
      {
        temp++;
      }
      //If it doesn't, we wipe the temp variable and start from one.
      else
      {
        temp = 1;
      }
      //If the temporary variable exceeds the counter amount, we make
      //the counter variable equal to the temp variable.
      if (temp > counter)
      {
        counter = temp;
      }
    }
    //Return the counter.
    return counter;
  }

答案 7 :(得分:0)

public int maxBlock(String str) {
int maxLength = 0;
Map<String,Integer> map = new HashMap<String,Integer>();
for(int i = 0; i< str.length(); i++){
String key = str.substring(i,i+1);
if(i!=0 && str.charAt(i) == str.charAt(i-1) && map.containsKey(key)){
map.put(key, map.get(key)+1);
}
else{
map.put(key,1);
}
}

for(Map.Entry<String,Integer> entry : map.entrySet()){
if(maxLength <entry.getValue()){
maxLength = entry.getValue();
}
}

return maxLength;
}

答案 8 :(得分:0)

public int maxBlock(String str) {

    int count = 0;
    int i = 0;
    int maxcount = 0;
    if (str.length() == 0) return 0;

    while (i < str.length() - 1) {

        if (str.charAt(i) == str.charAt(i + 1)) {

            count++;

            if (count > maxcount) {
                maxcount = count;
            }
        } else count = 0;

        i++;
    }

    return maxcount + 1;
}

答案 9 :(得分:0)

public int maxBlock(String str) {
  int tcount = 0;
  if (str.length() < 1) {
    return 0;
  }
  for (int i = 0; i < str.length(); i++) {
    int count = 0;
    for (int j = i; j < str.length(); j++) {
      if (str.charAt(i) == str.charAt(j)) {
        count++;
      } else
        break;
    }
    if (count > tcount) {
      tcount = count;
    }
    i += count - 1;

  }
  return tcount;
}

这很容易,它完全可以正常工作。

答案 10 :(得分:-1)

int i = 0;
int j = 0;

while(i < inner.length && j < outer.length) {
        if(inner[i] > outer[j]) {
            j++;
        } else if(inner[i] < outer[j]) {
            return false;
        } else {
            i++;
        }
    }

    if(i != inner.length)
        return false;

    return true;
}

答案 11 :(得分:-1)

这是我的代码

 public int maxBlock(String str) {

  int count = 1;
  for(int i=0;i<str.length()-1;i++){
     int count2 = 0;

     if(str.substring(i,i+1).equals(str.substring(i+1,i+2) )){
       for(int j = i ; j < str.length();j++){
          if(str.substring(i,i+1).equals(str.substring(j,j+1))){
            ++count2;
          }
          else{
            break;
          }
       }
     }

     if(count2 > count){
       count = count2;
     }


  }

  if(str.isEmpty())return 0;
  return count;
}