Java CodingBat中maxBlock的替代解决方案
从codingBat求解 给定一个字符串,返回字符串中最大“块”的长度 一串块是相同的相邻字符的运行Java CodingBat中maxBlock的替代解决方案,java,Java,从codingBat求解 给定一个字符串,返回字符串中最大“块”的长度 一串块是相同的相邻字符的运行 maxBlock(“hoopla”)→ 2 maxBlock(“abbccddbbbxx”)→ 3 maxBlock(“”)→ 0 我尝试使用一个for循环来解决它,如下所示: public int maxBlock(String str) { int maxCounter=1; int counter=1; if(str.length()==0) { return 0;
maxBlock(“hoopla”)→ 2
maxBlock(“abbccddbbbxx”)→ 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;
}
public int maxBlock(字符串str){
int maxCounter=1;
int计数器=1;
如果(str.length()==0)
{
返回0;
}
for(int i=0;imaxCounter)
{
最大计数器=计数器;
计数器=0;
}
}
返回最大计数器;
}
除了一个,它胜过所有的案例。有人能用一个for循环来展示解决方案吗
很抱歉这么晚才提到,但您不能使用正则表达式或集合框架中的任何内容。您可以使用模式匹配器
“()(\\1)*”
查找字符串中的重复字符,以下是代码:
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;
}
您可以使用模式匹配器查找字符串中的重复字符,下面是代码:
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;
}
这里有一个大致基于您的解决方案。请注意使用
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);
}
publicstaticintmaxblock(stringstr){
int maxCounter=1;
int计数器=1;
如果(str.length()==0){
返回0;
}
对于(int i=1;i最大计数器){
最大计数器=计数器;
}
计数器=1;
}
}
返回Math.max(最大计数器,计数器);
}
这里有一个大致基于您的解决方案。请注意使用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);
}
publicstaticintmaxblock(stringstr){
int maxCounter=1;
int计数器=1;
如果(str.length()==0){
返回0;
}
对于(int i=1;i最大计数器){
最大计数器=计数器;
}
计数器=1;
}
}
返回Math.max(最大计数器,计数器);
}
我认为在某些边缘情况下,您可能会弄错:
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 : ""));
}
}
我认为在某些边缘情况下,您可能会弄错:
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 : ""));
}
}
我来晚了一点,但我的解决方案是:
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;
}
public int maxBlock(字符串str){
int max=0;
整数计数=1;
字符o='';
对于(int i=0;imax){max=count;}
}否则{
计数=1;
如果(count>max){max=count;}
}
o=c;
}
返回最大值;
}
我来晚了一点,但我的编码解决方案如下:
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;
}
public int maxBlock(字符串str){
int max=0;
整数计数=1;
字符o='';
对于(int i=0;imax){max=count;}
}否则{
计数=1;
如果(count>max){max=count;}
}
o=c;
}
返回最大值;
}
只需为循环使用一个。我认为这是另一种方式
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;
}
public int maxBlock(字符串str){
int len=str.length();
内部温度=(透镜>0)?1:0;
int r=0;
对于(int i=1;ir)?温度:r;
温度=1;
}
}
r=(温度>r)?温度:r;
返回r;
}
只需为循环使用一个。我认为这是另一种方式
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;
}
public int maxBlock(字符串str){
int len=str.length();
内部温度=(透镜>0)?1:0;
int r=0;
对于(int i=1;ir)?温度:r;
温度=1;
}
}
r=(温度>r)?温度:r;
返回r;
}
public int maxBlock(字符串str){
int max=0;
对于(int i=0;i
public int maxBlock(字符串str){
int max=0;
对于(int i=0;i
inti=0;
int j=0;
而(i外部[j]){
j++;
}else if(内部[i]<外部[j]){
返回false;
}否则{
i++;
}
}
如果(i!=内部长度)
返回false;
返回true;
}
inti=0;
int j=0;
而(i外部[j]){
j++;
}else if(内部[i]<外部[j]){
返回false;
}否则{
i++;
}
}
如果(i!=内部长度)
返回false;
返回true;
}
这是我的解决方案。这比你想象的要简单
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;
}
public int maxBlock(字符串str)
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;
}
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;
}