算法题
public class Test6 { public int judge(String s,int length){ int sum=0,i=0; for(i=0;i<length-2;i++){ if(s.charAt(i)==s.charAt(i+1) && s.charAt(i+1)==s.charAt(i+2)){ sum++; i++; }else if(s.charAt(i)==s.charAt(i+1)){ sum++; i++; }else if(s.charAt(i+1)==s.charAt(i+2)){ sum+=judge(s.substring(i+1),length-i-1); break; }else{ i++; } } if(i==length-2){ if(s.charAt(i)==s.charAt(i+1)) sum++; } return sum; } public static void main(String[] args){ System.out.println(new Test6().judge("BBBYYYYYY", 9)); System.out.println(new Test6().judge("RBRRRR", 6)); } }
望共同研讨